xml方式装载bean

bean的属性配置

一直用注解,避免不了读一些代码时别人用配置方式,这里备份下一些用法:

bean元素可以有许多属性,其中有两个是必须的:id和class  。如果使用设值注入,则需要使用property子标签,来指定组件的属性。 

<bean id="renderer" class="com.apress.prospring.ch2.StandardOutMessageRenderer">   
    <property name="messageProvider">   
        <ref local="provider"/>   
    </property>   
</bean>   
<!-- 使用构造子注入时,则使用constructor-arg子标签,来指定构造函数的参数。   -->
<bean id="provider" class="com.apress.prospring.ch4.ConfigurableMessageProvider">   
    <constructor-arg>   
        <value>This is a configurable message</value>   
    </constructor-arg>   
</bean>   
<!--当构造函数有多个参数时,可以使用constructor-arg标签的index属性,index属性的值从0开始。-->
<bean id="provider" class="com.apress.prospring.ch4.ConfigurableMessageProvider">   
    <constructor-arg index="0">   
        <value>first parameter</value>   
    </constructor-arg>   
    <constructor-arg index="1">   
        <value>second parameter</value>   
    </constructor-arg>   
  
</bean>   
<!-- 在使用构造子注入时,需要注意的问题是要避免构造子冲突的情况发生。考虑下面的情况:-->   
public class ConstructorConfusion {   
    public ConstructorConfusion(String someValue) {   
        System.out.println("ConstructorConfusion(String) called");   
    }   
    public ConstructorConfusion(int someValue) {   
        System.out.println("ConstructorConfusion(int) called");   
    }   
}   
<!-- 使用如下配置文件-->   
<bean id="constructorConfusion" class="com.apress.prospring.ch4.ConstructorConfusion">   
    <constructor-arg>   
        <value>90</value>   
    </constructor-arg>   
</bean>   
<!-- 那么,当实例化组件constructorConfusion时,将输出ConstructorConfusion(String) called,也就是说参数类型为String的构造函数被调用了,这显然不符合我们的要求。为了让Spring调用参数为int的构造函数来实例化组件constructorConfusion,我们需要在配置文件中明确的告诉Spring,需要使用哪个构造函数,这需要使用constructor-arg的type属性。-->   
<bean id="constructorConfusion" class="com.apress.prospring.ch4.ConstructorConfusion">   
    <constructor-arg type="int">   
        <value>90</value>   
    </constructor-arg>   
</bean>   

 
<!-- 为了注入集合属性,Spring提供了list,map,set和props标签,分别对应List,Map,Set和Properties,我们甚至可以嵌套的使用它们(List of Maps of Sets of Lists)。-->   
<bean id="injectCollection" class="com.apress.prospring.ch4.CollectionInjection">   
    <property name="map">   
        <map>   
            <entry key="someValue">   
                <value>Hello World!</value>   
            </entry>   
            <entry key="someBean">   
                <ref local="oracle"/>   
             </entry>   
        </map>   
    </property>   
    <property name="props">   
        <props>   
            <prop key="firstName">   
                Rob   
            </prop>   
            <prop key="secondName">   
                Harrop   
            </prop>   
        </props>   
    </property>   
    <property name="set">   
        <set>   
            <value>Hello World!</value>   
            <ref local="oracle"/>   
        </set>   
    </property>   
    <property name="list">   
        <list>   
            <value>Hello World!</value>   
            <ref local="oracle"/>   
         </list>   
    </property>   
</bean>  

常用配置记录

<context:component-scan use-default-filters=false/>

<context:component-scan>有一个use-default-filters属性,该属性默认为true,会扫描指定包下的全部的标有@Component的类(子注解@Service,@Repository等)并注册成bean,但这样粒度有点大,将该属性改为false,这样只会扫描指定包下的指定注解。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值