星空网 > 软件开发 > Java

Spring基础——在Spring Config 文件中配置 Bean

一、基于

1.通过属性注入

即通过 setXxx() 方法注入 Bean 的属性值或依赖的对象。

使用 <property name="" value="" ref=""/> 元素,其中 name 值为对应 Bean 属性名称,value 指定对应属性值,ref 引用其他 Bean,value 属性和 ref 属性不可以同时存在。

也可以通过 <value></value> 子节点的方式指定属性值。

e:

<bean id="helloWorld" class="com.nucsoft.spring.helloworld.HelloWorld">  <property name="hello" value="spring"/>  <property name="world">    <value>spring</value>  </property></bean>

注意:在此种方式下,要配置的 Bean 必须存在空参构造器。

2.通过构造器注入

通过构造方法注入属性值或依赖的对象。

使用 <constructor-arg value="" index="" name="" ref="" type=""/> 元素,value 属性为对应的参数指定值,ref 引用其他 Bean。不可同时存在。

通过 name 属性、index 属性、type属性提供了三种注入方式:

(1)name:按照构造器参数名匹配入参

<bean id="car1" class="com.nucsoft.spring.helloworld.Car">  <constructor-arg name="brand" value="奥迪"/>  <constructor-arg name="corp" value="上海"/>  <constructor-arg name="price" value="400000"/></bean>

(2)index:按照索引匹配入参

<bean id="car2" class="com.nucsoft.spring.helloworld.Car">  <constructor-arg index="0" value="大众"/>  <constructor-arg index="1" value="10000"/></bean>

(3)type:按照类型匹配入参

<bean id="car3" class="com.nucsoft.spring.helloworld.Car">  <constructor-arg type="java.lang.String" value="宝马"/>  <constructor-arg type="double" value="200000"/></bean>

注意:Spring 虽然提供了通过构造器的方式进行注入,但是并不推荐使用此种方式。

3.细节问题

(1)若注入的属性值包含特殊字符,可以使用 <![CDATA[]]> 包含该属性值。

如:

<bean id="car4" class="com.nucsoft.spring.Car">  <property name="brand">    <value><![CDATA[<BM>]]> </value>  </property>  <property name="corp" value="德国"/>  <property name="maxSpeed" value="230"/>  <property name="price" value="20000000"/></bean>

(2)可以通过 <ref> 元素或 ref 属性为 Bean 的属性或构造器指定对 Bean 的引用。

 如:

e1:

<bean id="service" class="com.nucsoft.spring.Service"/><bean id="action" class="com.nucsoft.spring.Action">  <property name="service" ref="service"/></bean>

e2:

<bean id="feather" class="com.nucsoft.spring.Feather">  <property name="length" value="13"/></bean><bean id="bird" class="com.nucsoft.spring.Bird">  <constructor-arg name="birdName" value="smallBird"/>  <constructor-arg name="feather" ref="feather"/></bean>

(3)内部Bean:可以在属性或构造器里包含 Bean 的声明,内部 Bean 不需要指定 id,同时在别的地方也无法引用。

 如:

e1:

<bean id="action" class="com.nucsoft.spring.Action">  <property name="service">  <bean class="com.nucsoft.spring.Service"/>  </property></bean>

e2:

<bean id="bird" class="com.nucsoft.spring.Bird">  <constructor-arg name="birdName" value="smallBird"/>  <constructor-arg name="feather">    <bean class="com.nucsoft.spring.Feather">      <property name="length" value="13"/>    </bean>  </constructor-arg></bean>

4.为引用类型(字符串或对象)注入 Null 值

<bean id="bird" class="com.nucsoft.spring.Bird">  <constructor-arg name="birdName" value="smallBird"/>  <constructor-arg name="feather">    <null/>   </constructor-arg></bean>

5.为级联属性注入

(1)通过属性

<bean id="feather" class="com.nucsoft.spring.Feather">  <property name="length" value="44"/></bean><bean id="bird" class="com.nucsoft.spring.Bird">  <property name="birdName" value="smallBird"/>  <property name="feather" ref="feather"/>  <property name="feather.length" value="23"/></bean>

(2)通过构造器

<bean id="feather" class="com.nucsoft.spring.Feather">  <property name="length" value="44"/></bean><bean id="bird2" class="com.nucsoft.spring.Bird">  <constructor-arg name="birdName" value="bigBird"/>  <constructor-arg name="feather" ref="feather"/>  <property name="feather.length" value="33"/></bean>

注意:设置级联属性的前提是,级联属性所属对象已经被注入到容器中。同时需要为级联属性所属对象提供 getXxx() 方法。

6.集合属性

(1)集合性质的类型:List,Set,Map 和 数组

(2)使用 <list>,<set>,<map> 来配置集合属性,其中数组和 List 都使用 <list>

(3)List:通过引用外部 Bean 或 创建内部 Bean 的方式

<bean id="department" class="com.nucsoft.spring.Department">  <property name="deptName" value="dept01"/></bean><bean id="company" class="com.nucsoft.spring.Company">  <property name="companyName" value="ICBC"/>  <property name="departments">    <list>      <ref bean="department"/>      <bean class="com.nucsoft.spring.Department">        <property name="deptName" value="dept02"/>      </bean>    </list>  </property></bean>

测试:

private ApplicationContext ctx = null;@Beforepublic void init() {  ctx = new ClassPath);}@Testpublic void test01() {  Company com = ctx.getBean(Company.class);  System.out.println(com);}

输出:

Company{companyName='ICBC', departments=[Department{deptName='dept01'}, Department{deptName='dept02'}]}

(4)Set:与 List 类似。不做重复说明。

(5)Map: 在<map> 标签中使用多个 <entry> 子标签:简单字面值使用属性 key 和 value 来定义,Bean 的引用通过 key-ref 和 value-ref 属性定义。

如:

<bean id="emp02" class="com.nucsoft.spring.Employee">  <property name="employeeName" value="emp02"/>  <property name="age" value="23"/></bean><bean id="department" class="com.nucsoft.spring.Department">  <property name="deptName" value="dept01"/></bean><bean id="department02" class="com.nucsoft.spring.Department">  <property name="deptName" value="dept02"/></bean><bean id="company" class="com.nucsoft.spring.Company">  <property name="companyName" value="ICBC"/>  <property name="departments">    <map>      <entry key-ref="department" value-ref="emp01"/>      <entry key-ref="department02" value-ref="emp02"/>    </map>  </property>  <property name="strs">    <map>      <entry key="key01" value="value01"/>      <entry key="key02" value="value02"/>    </map>  </property></bean>

测试:

private ApplicationContext ctx = null;@Beforepublic void init() {  ctx = new ClassPath);}@Testpublic void test01() {  Company com = ctx.getBean(Company.class);  System.out.println(com);}

输出:

Company{companyName='ICBC', departments={Department{deptName='dept01'}=Employee{employeeName='emp01', age=12}, Department{deptName='dept02'}=Employee{employeeName='emp02', age=23}}}

注意:不能在 <entry> 标签中定义 Bean 标签,不支持。

(6)Properties 类型的属性:和 Map 类似,在 <props> 标签中使用 <prop> 子标签, 每个 <prop> 子标签必须定义 key 属性。

<bean id="DataSourceId" class="com.nucsoft.spring.DataSourceBean">  <property name="propertiesInfo">    <props>      <prop key="user">root</prop>      <prop key="password"></prop>      <prop key="jdbcUrl">jdbc:mysql:///test</prop>      <prop key="driverClass">com.mysql.jdbc.Driver</prop>    </props>  </property></bean>

7.定义单独的集合 Bean

使用基本的集合标签定义集合时,不能将集合作为独立的 Bean 定义,导致其他 Bean 无法引用该集合,所以无法在不同的 Bean 之间共用。

可以使用 util schema 里的集合标签定义独立的集合 Bean。需要注意的是,定义的集合 Bean 与 <bean> 标签是同级别的。

<bean id="company" class="com.nucsoft.spring.Company">  <property name="companyName" value="ICBC"/>  <property name="departments" ref="departments"/></bean><util:list id="departments">  <bean class="com.nucsoft.spring.Department">    <property name="deptName" value="dept01"/>  </bean>  <ref bean="department02"/></util:list>

可以定义到外部的集合 Bean 有:

Spring基础——在Spring Config 文件中配置 Bean

8.使用 p 命名空间,简化

<bean id="emp" class="com.nucsoft.spring.Employee" p:employeeName="emp" p:age="23" />

二、基于注解的 Bean 的配置

 

 

 

 

未完,待续。




原标题:Spring基础——在Spring Config 文件中配置 Bean

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流