Spring容器中的Bean

时间:2023-03-09 05:27:40
Spring容器中的Bean

一,配置合作者的Bean

Bean设置的属性值是容器中的另一个Bean实力,使用<ref.../>元素,可制定一个bean属性,该属性用于指定容器中其他Bean实例的id属性

 <bean id="steelAxe" class="org.com.service.impl.SteelAxe"></bean>
<bean id="chinese" class="org.com.service.impl.Chinese">
<property name="axe">
<!-- 指定使用个容器中的id为steelAxe的Bean作为调用setAxe()方法的参数 -->
<ref bean="steelAxe"/>
</property>
</bean>

也可以简写成

 <bean id="steelAxe" class="org.com.service.impl.SteelAxe"></bean>
<bean id="chinese" class="org.com.service.impl.Chinese">
<!-- 指定使用个容器中的id为steelAxe的Bean作为调用setAxe()方法的参数 -->
<property name="axe" ref="steelAxe"/>
</bean>

二,使用自动装配注入合作者的Bean

1,自动装配可以通<beans.../>的default-autowire指定,该元素对所有的<bean.../>起作用

2,也可以通过<bean>的autowire指定

default-autowire,autowire可接受如下属性值

no:默认此

byName

  <!-- 指定使用byName,根据setter的方法名与Bean的id进行匹配 -->
<bean id="chinese" class="org.com.service.impl.Chinese" autowire="byName">
</bean>
<bean id="gunDog" class="org.com.service.impl.GunDog">
<property name="name" value="wanggang"></property>
</bean>

setter方法名为setGunDog(),spring容器会找id为gunDog的Bean,若找到了,该Bean就会调用setGunDog方法的参数

     public void setGunDog(Dog dog){
this.dog=dog;
}

byType:根据参数类型,进行匹配,不多做例子,估计用不到

三,注入集合值

使用集合元素标签<list../><set../><map../><props../>

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- 资源国际化 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 驱动spring调用messageSource Bean的setBasenames()的方法,改方法需要一个参数组 -->
<property name="basenames">
<list>
<value>message</value>
<!-- 如果有多个资源文件,全部列在这儿 -->
</list>
</property>
</bean>
<bean id="steelAxe" class="org.com.service.impl.SteelAxe"></bean>
<bean id="chinese" class="org.com.service.impl.Chinese">
<property name="axe">
<!-- 指定使用个容器中的id为steelAxe的Bean作为调用setAxe()方法的参数 -->
<ref bean="steelAxe"/>
</property>
</bean> <bean id="steelAxe" class="org.com.service.impl.SteelAxe"></bean>
<bean id="chinese" class="org.com.service.impl.Chinese">
<!-- 指定使用个容器中的id为steelAxe的Bean作为调用setAxe()方法的参数 -->
<property name="axe" ref="steelAxe"/>
</bean> <!-- 指定使用byName,根据setter的方法名与Bean的id进行匹配 -->
<bean id="chinese" class="org.com.service.impl.Chinese" autowire="byName">
</bean>
<bean id="gunDog" class="org.com.service.impl.GunDog">
<property name="name" value="wanggang"></property>
</bean> <bean id="chinese" class="org.com.service.impl.Chinese">
<!-- 为调用setSchools()方法配置List集合作为参数值 和数组一样 -->
<property name="schools">
<list>
<!-- 每个value,ref,bean..都配置一个List元素 -->
<value>小学</value>
<value>中学</value>
<value>大学</value>
</list>
</property>
<property name="scores">
<!-- 为调用setScores()方法配置Map集合作为参数值 -->
<map>
<entry key="语文" value="80"/>
<entry key="数学" value="80"/>
<entry key="外语" value="80"/>
</map>
</property>
</bean>
</beans>

四,组合属性
除了最后一个属性可以为NULL,其他都不行

<bean id="" class="">

<property name="person.name" value="zhangliang"/>相当于执行getPerson().setName(String name);

</bean>