spring - property和constructor-arg的使用
2016-07-04 10:28
429 查看
http://jingyan.baidu.com/article/11c17a2c749a61f446e39d94.html
配置文件的根元素是beans,每个组件使用bean元素来定义,bean元素可以有许多属性,其中有两个是必须的:id和class。id表示组件的默认名称,class表示组件的类型。
方法/步骤
1
依赖注入的方式: constructor-arg:通过构造函数注入。 property:通过setxx方法注入。
2
设值注入,使用property子标签:
<bean id="renderer" class="com.apress.prospring.ch2.StandardOutMessageRenderer"> <property name="messageProvider">
<ref local="provider"/>
</property>
</bean>
3
构造子注入,使用constructor-arg子标签:
<bean id="provider"class="com.apress.prospring.ch4.ConfigurableMessageProvider"> <constructor-arg>
<value>This is a configurable message</value>
</constructor-arg>
</bean>
4
使用constructor-arg标签的index属性,对应于构造函数的多个参数,index属性的值从0开始。
<bean id="provider" lass="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>
5
使用constructor-arg的type属性,避免构造子冲突:
<bean id="constructorConfusion" class="com.apress.prospring.ch4.ConstructorConfusion">
<constructor-arg type="int">
<value>90</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");
}
}
6
注入集合属性,使用list,map,set和props标签,分别对应List,Map,Set和Properties:
<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>
配置文件的根元素是beans,每个组件使用bean元素来定义,bean元素可以有许多属性,其中有两个是必须的:id和class。id表示组件的默认名称,class表示组件的类型。
方法/步骤
1
依赖注入的方式: constructor-arg:通过构造函数注入。 property:通过setxx方法注入。
2
设值注入,使用property子标签:
<bean id="renderer" class="com.apress.prospring.ch2.StandardOutMessageRenderer"> <property name="messageProvider">
<ref local="provider"/>
</property>
</bean>
3
构造子注入,使用constructor-arg子标签:
<bean id="provider"class="com.apress.prospring.ch4.ConfigurableMessageProvider"> <constructor-arg>
<value>This is a configurable message</value>
</constructor-arg>
</bean>
4
使用constructor-arg标签的index属性,对应于构造函数的多个参数,index属性的值从0开始。
<bean id="provider" lass="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>
5
使用constructor-arg的type属性,避免构造子冲突:
<bean id="constructorConfusion" class="com.apress.prospring.ch4.ConstructorConfusion">
<constructor-arg type="int">
<value>90</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");
}
}
6
注入集合属性,使用list,map,set和props标签,分别对应List,Map,Set和Properties:
<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>
相关文章推荐
- spring - property和constructor-arg的使用
- property和constructor-arg的使用
- property和constructor-arg的使用
- property和constructor-arg设值注入
- 语言-代码相同,在Java和C下面的执行结果不同-++的应用
- Caused by: java.net.UnknownHostException
- org.springframework.beans.factory.CannotLoadBeanClassException:
- java中的原子操作
- spring四种依赖注入方式
- SpringMVC源码总结(十一)mvc:interceptors拦截器介绍
- java冒泡排序Bubble sort
- Struts2 的Action中取得请求参数值的几种方法
- 二分查找法 java
- java基础语言写简单计算器
- spring AOP例子
- SpringMVC 自定义类型转换器
- MyEclipse创建maven项目
- Java基础——clone()方法浅析
- Spring3.2新注解@ControllerAdvice
- SpringMVC 拦截器