zoukankan      html  css  js  c++  java
  • Spring-IOC总结

    1. Spring ,一站式解决方案
      核心:IOC(DI) AOP
      7个模块,自由选用,也可以使用其他框架

    2. IOC容器
      Spring提供两种不同类型的容器:BeanFactory接口和ApplicationContext接口
      IOC容器负责创建和管理所有bean
      IOC容器通过加载配置文件生成,最常用接口为ApplicationContext,两个实现类
      1. ClassPathXmlApplicationContext    从类加载路径下寻找配置文件,根据配置文件生成Spring容器
      2. FileSystemXmlApplicationContext    从文件系统的相对路径或绝对路径下搜索配置文件..............
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    3. bean的配置文件中实现依赖注入
      一个bean的属性可能是普通类型的,也可能是引用类型,添加set或者构造函数  ,容器反射机制调用这些方法 就可以生成bean实例并初始化这个属性值
      bean的自动装配,5个:no    byName    byType   constructor    autodetect不需要显示指定ref
      • default-autowire  beans的属性;autowire  bean的属性
      • <!--通过设置可以将Bean排除在自动装配之外-->
        <bean id="" autowire-candidate="false"/>
        <!--除此之外,还可以在beans元素中指定,支持模式字符串,如下所有以abc结尾的Bean都被排除在自动装配之外-->
        <beans default-autowire-candidates="*abc"/>
    4. bean的作用域
      1. singleton  单例
      2. prototype  每次getBean()都会获取新的
      3. request  同一次HTTP请求中,多次获取bean都只会是同一个实例
      4. session  同一次HTTP会话中,多次获取bean都只会是同一个实例
      5. global session  每个全局的HTTP会话对应一个实例
    5. 工厂方法创建bean
      1. 静态工厂方法:class指定方法所在的类,factory-method指定是哪个静态方法
      2. 实例工厂方法:无须class属性,但是要factory-bean指定是哪个工厂实例,factory-method指定哪个方法
    6. lookup方法注入
      单例的beanA依赖非单例的beanB  ,容器初始化所有bean时只创建了一个beanA,beanA里面的beanB不会更新了
      Spring通过cglib在运行期间动态的操作class字节码,能为bean动态的创建子类或实现类

      如图,用lookup-method为MagicBoss的getCar()方法提供了动态的实现,每次都会返回一个新的car
      Spring会采用运行时动态增强的方式来实现<lookup-method.../>元素所指定的抽象方法,如果目标抽象类实现过接口,Spring会采用JDK动态代理来实现该抽象类,并为之实现抽象方法;如果目标抽象类没有实现过接口,Spring会采用cglib实现该抽象类,并为之实现抽象方法。Spring4.0的spring-core-xxx.jar包中已经集成了cglib类库

    7. 注解实现零配置
      • @Component :标注一个普通的spring bean类
      • @Controller:控制器组建类
      • @Service:业务逻辑组件类
      • @Repository:Dao组件类
        配置自动扫描的包:
      • @Resource配置依赖,可修饰setter方法或实例变量
      • @PostConstruct和@PreDestory用于定制bean的生命周期,修饰方法,前者是bean的初始化方法,后者是销毁之前的方法
      • @AutoWired实现自动装配,@Qualifier实现精确装配(根据bean的id实现精确装配)
  • 相关阅读:
    WPF之感触
    C# WinForm 给DataTable中指定位置添加列
    MyEclipse 8.6 download 官方下载地址
    将博客搬至CSDN
    Building Microservices with Spring Cloud
    Building Microservices with Spring Cloud
    Building Microservices with Spring Cloud
    Building Microservices with Spring Cloud
    Building Microservices with Spring Cloud
    Building Microservices with Spring Cloud
  • 原文地址:https://www.cnblogs.com/gucl/p/8147970.html
Copyright © 2011-2022 走看看