什么是设计模式,它的作用是什么?
所谓设计模式,就是一套被反复使用的代码设计经验的总结(情境中一个问题经
过证实的一个解决方案)。使用设计模式是为了可重用代码、让代码更容易被他
人理解、保证代码可靠性。设计模式使人们可以更加简单方便的复用成功的设计
和体系结构。将已证实的技术表述成设计模式也会使新系统开发者更加容易理解
其设计思路。
image.png

Spring的核心容器:

spring code,spring context,spring exception language,spring bean

什么是spring IOC

IOC又叫依赖注入,主要的作用就是将对象的创建交给spring容器,容器通过读取配置文件,来创建bean对象并实例化。

ApplicationContext和BeanFactory的区别

  1. //方式一:
  2. BeanFactory bf=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
  3. Students student1=ac.getBean("student");
  4. //方式二:
  5. ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
  6. Students student1=ac.getBean("student");
  • applicationContext在加载配置文件时就将配置文件中配置的bean全部创建,而beanFactory只有在调用getbean()方法时才会创建对应的bean。
  • 这样的机制造成了applicationContext启动较慢,而且耗费资源。beanFactory第一次请求获取对应对象较慢。

    Bean的装配方式

    三种:xml配置,注解,自动装配

    xml配置

    spring提供了两种装配方式:设值注入、构造注入
    设值注入要求Bean必须满足一下要求:

  • Bean类必须提供一个默认的无参构造方法;

  • Bean类必须为需要注入的属性提供对应的setter方法;

    设值注入

    image.png

image.png
image.png

构造注入

底层使用带参构造器,创建对象和给属性赋值
image.png

常用的注入属性值

image.png
image.png
image.png
image.png

基于Annotation的装配

Spring常用注解如下:

Component 描述Spring中的Bean,但是它是一个泛化的概念,仅仅表示一个组件(Bean)并且可以作用在任何层次;
Repository 用于将数据访问层(DAO层)的类标识为Spring中的Bean
Service 作用在业务层(service层),用于将业务层的类标识为Spring中的Bean
Controller 作用在控制层(如Spring MVC 的 controller层),用于将控制层的类标识为Spring中的Bean
Autowired 用于对Bean的属性变量、属性setter方法及构造方法进行标注,配合对应的注解处理器完成Bean的自动配置工作
Resourse 作用与Autowired一样。Resourse两个重要的属性:name和type。Spring将name解析为Bean实例名称,type解析为Bean实例类型

image.png
image.png

自动装配

就是将一个bean自动地注入到其他Bean的property中。可以按类型自动注入或者按名称自动注入。
image.png

srpingBean的生命周期

Spring知识点 - 图13
参考地址:http://www.cnblogs.com/zrtqsk/p/3735273.html

Bean的作用域

image.png

什么是AOP?

AOP: (Aspect Oriented Programming) 面向切面编程。是目前软件开发中的一个热点。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。主要的功能是:日志记录,性能统计,安全控制,事务处理,异常处理等等。

动态代理和静态代理

静态代理(装配者模式)

  1. package com.woniuxy.wrapper;
  2. class Coffee{
  3. public String getCoffee() {
  4. return "coffee";
  5. }
  6. }
  7. //是你还有你,一切拜托你
  8. ////是你
  9. class MilkCoffee extends Coffee{
  10. //还有你
  11. Coffee coffee;
  12. public MilkCoffee(Coffee coffee) {
  13. this.coffee = coffee;
  14. }
  15. public String getCoffee() {
  16. //一切拜托你
  17. return "milk"+coffee.getCoffee();
  18. }
  19. }
  20. class SugarCoffee extends Coffee{
  21. Coffee coffee;
  22. public SugarCoffee(Coffee coffee) {
  23. this.coffee = coffee;
  24. }
  25. public String getCoffee() {
  26. return "sugar"+coffee.getCoffee();
  27. }
  28. }
  29. public class MyTest {
  30. public static void main(String[] args) throws Exception {
  31. Coffee coffee=new Coffee();
  32. System.out.println(coffee.getCoffee());
  33. MilkCoffee milkCoffee=new MilkCoffee(coffee);
  34. System.out.println(milkCoffee.getCoffee());
  35. //SugarCoffee sugarCoffee=new SugarCoffee(coffee);
  36. SugarCoffee sugarCoffee=new SugarCoffee(milkCoffee);
  37. System.out.println(sugarCoffee.getCoffee());
  38. }
  39. }

动态代理

  1. package com.woniuxy.dynamicproxy;
  2. import java.lang.reflect.InvocationHandler;
  3. import java.lang.reflect.Method;
  4. import java.lang.reflect.Proxy;
  5. public class MyTest {
  6. //动态代理有两种实现:
  7. //Proxy:JDK内置,要求目标类,必须要有接口
  8. //cglib:第三方jar包,有没有接口都可以增强
  9. public static void main(String[] args) {
  10. //目标对象
  11. SomeService someService=new SomeServiceImpl();
  12. //返回值是一个代理对象:目标对象增强以后的对象
  13. // ClassLoader loader,类加载器
  14. // Class<?>[] interfaces,目标类的所有接口
  15. // InvocationHandler h,接口,需要用这个接口的实现类完成对目标对象的增强
  16. SomeService obj = (SomeService) Proxy.newProxyInstance(
  17. someService.getClass().getClassLoader(),
  18. someService.getClass().getInterfaces(),
  19. new InvocationHandler() {
  20. //Object proxy, 代理对象
  21. //Method method, 目标方法
  22. //Object[] args,目标方法的参数
  23. @Override
  24. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  25. //切面---织入--前置通知
  26. System.out.println("前置增强:记录日志");
  27. //调用目标对象的方法
  28. Object result = method.invoke(someService, args);
  29. //切面---织入--后置通知
  30. System.out.println("后置增强:记录日志");
  31. return result;
  32. }
  33. });
  34. obj.doSome();
  35. }
  36. }