• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
斑駁_光影
                              Keep Hunger     Keep Foolish
博客园    首页    新随笔    联系   管理    订阅  订阅

spring IoC学习 ------IoC容器

     spring IoC容器分为两种:BeanFactory和ApplicationContext

  1. BeanFactory: 基础类型的IoC容器,提供完整的IoC服务支持,如果没有特殊指定,默认采用延迟初始化策略(lazy-load)。因此容器初始化速度较快,所需要的资源有限,对于资源有限,并且功能要求不是很严格的场景, BeanFactory是比较何时的IoC容器选择
  2. ApplicationContext:在BeanFactory的基础上构建,除了拥有BeanFactory的所有支持,还提供了高级特性。如事件发布,国际化信息指挥,该类型容器启动之后,默认全部初始化并绑定完成,因此,容器初始化事件较长,所需资源比较多

  BeanFactory容器的管理方式:

  1. 直接编码方式
  2. 使用xml的管理方式
  3. 使用java5的注解管理方式
 1 //测试XML配置注入
 2     @Test
 3     public void TestBeanFactoryForXML() {
 4         DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
 5     BeanFactory container = (BeanFactory)SpringUtil.bindViaXMLFile(beanFactory,"classpath:config.xml");
 6         Calculator calculator = (Calculator) container
 7                 .getBean(Calculator.class);
 8         calculator.operationPlus(1, 1);
 9         calculator.operationSubtract(2, 1);
10     }
11     //测试直接编码管理容器
12     @Test
13     public void TestBeanFactoryForCode() {
14         DefaultListableBeanFactory beanRegistery = new DefaultListableBeanFactory();
15         BeanFactory container = (BeanFactory) SpringUtil.bindViaCode(beanRegistery);
16         Calculator calculator = container.getBean(Calculator.class);
17         calculator.operationPlus(1, 1);
18         calculator.operationSubtract(2, 1);
19     }
20     @Test
21     public void TestBeanFactoryForAnnonation(){
22         ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:config.xml");
23         Calculator calculator = (Calculator)ctx.getBean(Calculator.class);
24         calculator.operationPlus(1, 1);
25         calculator.operationSubtract(2, 1);
26     }
27 @SuppressWarnings("deprecation")
28     public static BeanFactory bindViaCode(BeanDefinitionRegistry registry) {// definition                                                                        // 定义
29         // 定义bean
30         AbstractBeanDefinition calculator = new RootBeanDefinition(
31                 Calculator.class, true);
32         AbstractBeanDefinition mathUtil = new RootBeanDefinition(
33                 MathUtil.class, true);
34         // 注册到容器中
35         registry.registerBeanDefinition("calculator", calculator);
36         registry.registerBeanDefinition("mathUtil", mathUtil);
37         // 通过构造器的注入方式 
38          ConstructorArgumentValues argValues = new  ConstructorArgumentValues();
39          argValues.addIndexedArgumentValue(0, calculator); 
40          //通过set方法注入
41         MutablePropertyValues propertyValues = new MutablePropertyValues();
42         // propertyValues.addPropertyValue(new PropertyValue("calculator",
43         // calculator));
44         propertyValues.addPropertyValue("mathUtil", mathUtil);
45 
46         calculator.setPropertyValues(propertyValues);
47 
48         return (BeanFactory) registry;
49 
50     }
51 
52     public static BeanFactory bindViaXMLFile(BeanDefinitionRegistry registry, String xmlpath) {
53         XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry);
54        
55         reader.loadBeanDefinitions(xmlpath);
56         return (BeanFactory) registry;
57     }

BeanFactory的xml配置文件

  xml配置文件的基本元素 

<beans><!--根元素-->
         <description>  <!-- 零个至一个-->
          <import>
          <alias>
          <bean><!-- 以上为零个到多个-->
</beans>

  使用setter、构造器注入依赖,:

<bean id="calculator" class="org.qbrightwork.studysource.springstudyIoc.Calculator">
        <property name="mathUtil" ref="mathUtil"></property>  <!--setter-->
        <constructor-arg ref="mathUtil"></constructor-arg><!--constructor-->
        <constructor-arg type="String" value="nimei"></constructor-arg>
    </bean>
    <bean id="mathUtil" class="org.qbrightwork.studysource.springstudyIoc.MathUtil"></bean>

  注入集合类List,Set,Map  

<bean id="calculator" class="org.qbrightwork.studysource.springstudyIoc.Calculator">
        <!-- <property name="mathUtil" ref="mathUtil"></property> -->
        <!-- <constructor-arg ref="mathUtil"></constructor-arg> -->
        <constructor-arg type="List" name="list">
                <list>
                    <value>list-arg-one</value>
                    <value>list-arg-two</value>
                    <value>list-arg-three</value>
                </list>
        </constructor-arg>
        <constructor-arg ref="mathUtil" />
        
    </bean>
    <bean id="mathUtil" class="org.qbrightwork.studysource.springstudyIoc.MathUtil"></bean>
    <bean id="calculator" class="org.qbrightwork.studysource.springstudyIoc.Calculator">
        <!-- <property name="mathUtil" ref="mathUtil"></property> -->
        <!-- <constructor-arg ref="mathUtil"></constructor-arg> -->
        <constructor-arg type="Set" name="set">
                <set>
                    <value>set-arg-one</value>
                    <value>set-arg-two</value>
                    <value>set-arg-three</value>
                </set>
        </constructor-arg>
        <constructor-arg ref="mathUtil" />        
    </bean>
    <bean id="mathUtil" class="org.qbrightwork.studysource.springstudyIoc.MathUtil"></bean>
<bean id="calculator" class="org.qbrightwork.studysource.springstudyIoc.Calculator">
        <!-- <property name="mathUtil" ref="mathUtil"></property> -->
        <!-- <constructor-arg ref="mathUtil"></constructor-arg> -->
        <constructor-arg type="Map" name="map">
                <map>
                    <entry key='key-one'>
                        <value>value-one</value>
                    </entry>
                    <entry key='key-two'>
                        <value>value-two</value>
                    </entry>
                    <entry key="key-three">
                        <value>value-three</value>
                    </entry>
                </map>                
        </constructor-arg>
        <constructor-arg ref="mathUtil" />
    </bean>
    <bean id="mathUtil" class="org.qbrightwork.studysource.springstudyIoc.MathUtil"></bean>

 如果要向一个属性注入null值是,应该使用标签<null /> 而不是 <value></value>

  autowire属性:让IoC容器自己识别需要注入那些类到bean中去。可以选择的参数有:byType、byName、no、default、constructor五个属性,其中比较常用的是byName属性,就是根据类中的属性名称进行注入。

  parent属性,当类存在继承关系时,可以使用parent属性指定其父类

  bean的scope属性:

  1. singleton:在Bean容器中只有一个实例,说有对象共享一个实例(默认配置)
  2. prototype:容器在接到该类型对象的请求的hih会每次都重新生成一个新的对象实例给请求方,当将实例交给请求方之后,便不再管理实例的生命周期。
  3. request:用在web情境中,向每个请求返回全新的实例,可以看做是prototype的一种特例
  4. session:用在web情景中,比scope为request的bean具有更长的生命周期
  5. global session :用于protlet的web程序6
  6. 自定义scpoe,需要实现scope接口
posted @ 2012-07-17 10:16  斑驳_光影  阅读(352)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3