SpringAction学习一、装配bean:一些补充

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MASORL/article/details/82629408

补充1:bean的命名

(1)没有明确命名bean的id,则id为类名首字母小写。如CDPlayer的bean id就是cDplayer

(2)自定义ID

方式一:

@Component("cDplayerName")

方式二:@Name注解来自Java依赖注入规范(Java Dependency Injection),与@Component只有细微差别,大多数情况下可以互换【个人觉得知道就好,没必要用】

@Name("cDplayerName")

补充二:组件扫描包基础包设置

方式一:类所在的包就是基础包

@ComponentScan

方式二:指定基础包(String 形式)

@ComponentScan(“packageName”)

方式三:指定基础包更清晰版(String形式)

@ComponentScan(basePackages = “packageName”)

方式四:指定多个基础包(String 形式)

@ComponentScan(backPackages = {“packageName1”, ”packageName2”})

方式五:通过类来设置类的所在的包为基础包

@ComponentScan(basePackageClasses={DemoClass.class, DemoClass2.class})

为什么要特地表明2、3、4是string 形式:因为String是类型不安全的(not type-safe),而5更加安全。5设置可以添加一个空标记接口,只用来设置基础包,这是有利于代码重构的

补充三:配置文件的加载

方式一:自动加载当前类名+context.xml后缀的配置文件。如CDPlayerContext.xml

@ContextConfiguration

方式二:加载配置类

@ContextConfiguration(classes = JavaConfig.class)

方式三:加载多个配置类

@ContextConfiguration(classes = {JavaConfig.class, JavaConfig2.class})

方式四:加载指定的xml文件

@ContextConfiguration(locations=”classpath:xxx.xml”)

方式五:加载多个指定的xml文件

@ContextConfiguration(locations={“classpath:xxx.xml” , “classpath:xxx2.xml”})

注:用idea开发需要把xml文件放到resources中,idea不会读src下的静态资源文件~~~

补充四:利用构造器XML声明bean的各种方式

有<constructor-arg>标签就表示:它借助的是类的构造器。

方式一:通过<constructor-arg>借助构造器注入初始化bean,name为参数名

 <bean id="cdPlayer" class="com.spring.action.xmlconfig.CDPlayer">
        <constructor-arg name="compactDisc" ref="sgtPeppers"/>
    </bean>

方式二:通过c命名空间借助构造器注入初始化bean

 <bean id="cdPlayer" class="com.spring.action.xmlconfig.CDPlayer" c:compactDisc-ref="sgtPeppers"/>

方式三:c命名空间索引写法

0代表构造器的第一个参数,为什么要"_"呢,因为XML是不允许属性第一位是数字的

<bean id="cdPlayer" class="com.spring.action.xmlconfig.CDPlayer" c:_0-ref="sgtPeppers"/>

补充五:XML注入字面量(String、基本数据类型这种)

方式一:

 <bean id="cdPlayer" class="com.spring.action.xmlconfig2.CDPlayer">
        <constructor-arg name="compactDisc" ref="sgtPeppers"/>
        <constructor-arg name="name" value="字符串数据"/>
        <constructor-arg name="happy" value="String Data"/>
    </bean>

方式二:C命名空间注册

    <bean id="cdPlayer" class="com.spring.action.xmlconfig2.CDPlayer"
          c:name="字符串数据"
          c:happy="String Data"
          c:compactDisc-ref="sgtPeppers">
    </bean>

方式三:C命名空间索引

    <bean id="cdPlayer" class="com.spring.action.xmlconfig2.CDPlayer"
          c:_0="字符串数据"
          c:_1="String Data"
          c:_2-ref="sgtPeppers">
    </bean>

方式四:装配集合

没有使用name,则按构造器参数顺序注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="sgtPeppers" class="com.spring.action.xmlconfig3.SgtPeppers"/>


    <bean id="otherCDPlayer" class="com.spring.action.xmlconfig3.OtherCDPlay">
        <!--String title-->
        <constructor-arg value="title"/>
        <!--CompactDisc compactDisc;-->
        <constructor-arg ref="sgtPeppers"/>
        <!--List<String> stringList;-->
        <constructor-arg>
            <list>
                <value>a</value>
                <value>b</value>
                <value>c</value>
                <value>d</value>
            </list>
        </constructor-arg>
        <!--List<CompactDisc> compactDiscList;-->
        <constructor-arg>
            <list>
                <ref bean="sgtPeppers"/>
                <ref bean="sgtPeppers"/>
                <ref bean="sgtPeppers"/>
                <ref bean="sgtPeppers"/>
            </list>
        </constructor-arg>
        <!--Set<String> compactDiscs;-->
        <constructor-arg>
            <set>
                <value>a</value>
                <value>b</value>
                <value>c</value>
                <value>d</value>
            </set>
        </constructor-arg>
        <!--Set<CompactDisc> compactDiscSet;-->
        <constructor-arg>
            <set>
                <ref bean="sgtPeppers"/>
            </set>
        </constructor-arg>
    </bean>

</beans>

至此的注入,都是通过构造器进行注入。

补充六:属性注入

通过属性注入的CDPlayer类只能有一个无参构造器。需要提供属性的setter方法。name就是参数名

方式一:<property> 注入

    <bean id="cdPlayer" class="com.spring.action.xmlconfig4.CDPlayer">
        <property name="name" value="name"/>
        <property name="happy" value="happy"/>
        <property name="compactDisc" ref="sgtPeppers"/>
    </bean>

方式二:p命名空间法注入

    <bean id="cdPlayer" class="com.spring.action.xmlconfig4.CDPlayer"
          p:name="name"
          p:happy="happy"
          p:compactDisc-ref="sgtPeppers"
    />

方式三:属性注入集合

    
    <bean id="otherCDPlayer" class="com.spring.action.xmlconfig4.OtherCDPlay">
        <property name="title" value="title"/>
        <property name="compactDisc" ref="sgtPeppers"/>
        <property name="stringList">
            <list>
                <value>a</value>
                <value>b</value>
                <value>c</value>
                <value>d</value>
            </list>
        </property>
        <property name="compactDiscList" >
            <list>
                <ref bean="sgtPeppers"/>
                <ref bean="sgtPeppers"/>
                <ref bean="sgtPeppers"/>
                <ref bean="sgtPeppers"/>
            </list>
        </property>
        <property name="compactDiscs">
            <set>
                <value>a</value>
                <value>b</value>
                <value>c</value>
                <value>d</value>
            </set>
        </property>
        <property name="compactDiscSet">
            <set>
                <ref bean="sgtPeppers"/>
            </set>
        </property>
    </bean>

方式四:utli空间装配集合

p命名空间无法装配集合,只能装配值或者bean引用

    <util:list id="stringList">
        <value>a</value>
        <value>b</value>
        <value>c</value>
        <value>d</value>
    </util:list>

    <util:list id="compactDiscList">
        <ref bean="sgtPeppers"/>
        <ref bean="sgtPeppers"/>
        <ref bean="sgtPeppers"/>
        <ref bean="sgtPeppers"/>
    </util:list>

    <util:set id="compactDiscs">
        <value>a</value>
        <value>b</value>
        <value>c</value>
        <value>d</value>
    </util:set>

    <util:set id="compactDiscSet">
        <ref bean="sgtPeppers"/>
    </util:set>

    <bean id="otherCDPlayer" class="com.spring.action.xmlconfig4.OtherCDPlay"
          p:title="title"
          p:compactDisc-ref="sgtPeppers"
          p:stringList-ref="stringList"
          p:compactDiscList-ref="compactDiscList"
          p:compactDiscs-ref="compactDiscs"
          p:compactDiscSet-ref="compactDiscSet"
    />
Spring util-命名空间中的元素
元素 描 述
util:constant 引用某个类型的public static域,并将其暴露为bean
util:list 创建一个java.util.List类型的bean,其中包含值或引用
util:map 创建一个java.util.Map类型的bean,其中包含值或引用
util:properties 创建一个java.util.Properties类型的bean
util:property-path 引用一个bean的属性(或内嵌属性),并将其暴露为bean
util:set 创建一个java.util.Set类型的bean,其中包含值或引用

猜你喜欢

转载自blog.csdn.net/MASORL/article/details/82629408