zoukankan      html  css  js  c++  java
  • Spring 梳理-bean配置与装配

     

    1       bean配置与装配

    1.1      bean在XML文件中进行显示配置并装配

    1.2      bean在JavaConfig中显示配置并装配

    1.2.1  优点:类型是安全的,编译期可检查。(相对于xml配置而言,编辑时不能校验)

    1.2.2  通过java代码创建bean:略,使用@bean注解

    1.2.3  通过java代码装配bean:略

    1.3      bean自动扫描和自动装配

    1.3.1  自动组件扫描(component scanning)

    1.3.1.1 组件扫描默认是不启用的

    1.3.1.2 组件扫描启用方法:

    1.3.1.2.1 在XML配置文件中:

    <?xml version="1.0" encoding="UTF-8">

    <beans>

          <context:component-scan base-package="xxx"/>

      </beans>

    1.3.1.2.2 在javaConfig类中使用@ComponentScan注解

    1.3.1.2.2.1   默认会扫描与配置类相同的包。

    1.3.1.2.2.2   好处是:不会因为使用注解而污染代码

    1.3.1.2.2.3   扫描其他包的方法:@ComponentScan(basePackages=”org.apache.xxx“)

    1.3.1.2.2.4   扫描多个包的方法:@ComponentScan(basePackages={"xx","yy"})

    1.3.1.2.2.5   扫描指定的类:       @ComponentScan(ClassA.class,ClassB.class})

    1.3.1.3 定义组件使用 Spring的@Component注解

    1.3.1.4 组件bean命名

    1.3.1.4.1 默认ID为类名,并将第一个字母变为小写

    1.3.1.4.2 手动设置@Component("xx")

    1.3.1.4.3 使用Java注解@Name(“XX“)

    1.3.2  自动装配(autowiring)

    1.3.2.1 使用Spring的@Autowired注解进行装配

    1.3.2.2 ,或者java注解@Inject注解进行装配

    1.3.2.3 通过JavaConfig进行装配

    1.3.2.4 @Component  @Inject可以用在构造器上、Setter方法上、普通方法、属性(可以是Private)上

    1.3.2.5 自动装配的前提是,本事是一个bean,该bean内部的某些构造函数、属性、方法参数需要自动装配

    1.3.2.6 如果自动装配时,没有找到对应的bean,则自动抛出异常,或者@Autowired(required=false)

    1.3.2.7 如果自动装配时,找到多个bean,则自动抛出异常

    1.4      通过XML定义并装配bean

    1.4.1  定义一个简单的bean

    1.4.1.1 自动生成ID

    <bean class=”org.apache.xxx”>

    此时bean的ID为”org.apache.xxx#0”

    1.4.1.2 手动指定ID

    <bean id=”xxx” class=”org.apache.xxx”>

    1.4.2  使用构造器装配

    <bean id=”xx” class=”org.apache.yyy”>

             <constructor-arg ref=”yyy” />

             <constructor-arg value=”stringValue”/>

             <constructor-arg><null/></constructor-arg>

             <constructor-arg>

                    <list>

                           <value>aaa</value>

                           <value>bbb</value>

                    </list>

                   </constructor-arg>

             <constructor-arg>

                    <list>

                           <ref bean=”id1”/>

                           <ref bean=”id2”/>

                    </list>

                   </constructor-arg>

             <constructor-arg>

                    <set>

                           <ref bean=”id1”/>

                           <ref bean=”id2”/>

                    </set>

                   </constructor-arg>

    </bean>

    1.4.3  使用属性装配

    <bean id=”xxx” class=”org.apache.xxx”>

           <property name=”age” value=”15”/>

           <property name=”person” ref=”person_bean_id”/>

           <property name=”personList”>

                  <list>

                         <value>str1</value>

                         <value>str2</value>

                  </list>

           </property>

    </bean>

    1.5      混合装配

    1.5.1  在JavaConfig中引用xml配置

    @Configuration

    @Import(OtherJavaConfig.class)

    @ImportResource(“classpath:xxx.xml”)

    1.5.2  在xml配置中引用JavaConfig

    <beans>

           <import resource=”xxx.xml”/>

           <bean class=”OtherJavaConfig”/>

    注:JavaConfig引入到xml配置文件中,方法只是相当于创建一个普通的bean

    </beans>

     

     

  • 相关阅读:
    eclipse中文乱码问题解决方案
    修改Tomcat的JDK目录
    Tomcat 5.5 修改服务器的侦听端口
    HTML DOM教程 27HTML DOM Button 对象
    HTML DOM教程 24HTML DOM Frameset 对象
    Navicat for MySQL v8.0.27 的注册码
    HTML DOM教程 25HTML DOM IFrame 对象
    Tomcat 5.5 的下载和安装
    android manifest相关属性
    ubuntu10.04 下 eclipse 小结
  • 原文地址:https://www.cnblogs.com/jiangtao1218/p/9690011.html
Copyright © 2011-2022 走看看