Spring使用spring-beans.dtd文件来定义BeanFactory的XML配置规范。可以在http://www.springframework.org/dtd/spring-beans.dtd找到该dtd文件,当然,Spring的下载文件中也已经包含了该dtd文件。它被放在dist文件夹中。
配置文件的根元素是beans,每个组件使用bean元素来定义,bean元素可以有许多属性,其中有两个是必须的:id和class(这里说的id是必须的并不意味着在配置文件中必须指定id,后面会详细说明)。id表示组件的默认名称,class表示组件的类型。
如果使用设值注入,则需要使用property子标签,来指定组件的属性。
Java代码
<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>
//我们不仅可以构造单个BeanFactory,而且可以建立有继承关系的多个BeanFactory。只需要将父BeanFactory作为参数传给子BeanFactory的构造函数即可。
BeanFactory parent =
new XmlBeanFactory(new FileSystemResource("./ch4/src/conf/parent.xml"));
BeanFactory child =
new XmlBeanFactory(new FileSystemResource("./ch4/src/conf/beans.xml"), parent);
//如果子BeanFactory和父BeanFactory中含有名称相同的Bean,那么在子BeanFactory中使用
<ref bean="sameNameBean"/>//引用的将是子BeanFactory中的bean,为了引用父BeanFactory中的bean,我们需要使用ref标签的parent属性,<ref parent="sameNameBean"/>。
为了注入集合属性,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>
分享到:
相关推荐
<constructor-arg type="java.lang.String"> <value>jxg/Qr4VbxU= </constructor-arg> <!-- password --> <constructor-arg type="java.lang.String"> <value>jxg/Qr4VbxU= </constructor-arg> <!--...
第一种:使用构造函数提供(创建对象时,必须提供数据,否则无法创建成功) 使用的标签:constructor-arg 标签出现的位置:bean标签的内部 标签中的属性: type:用于指定要注入的数据的数据类型,该数据类型也是...
我们在springmvc中使用json经常出现乱码格式 如下图: 我们可以在@RequestMapping()中配置,produces = "application/json;charset=utf-8",这样就解决了我们乱码, ... <constructor-arg value="UTF-8"/>
项目说明 : 这是一个spring-mybatis整合的项目..... <constructor-arg index="0" ref="sqlSessionFactory" /> ⑶UserSupportDao方式 方式 : 直接继承该类...然后调用getSqlSession()再去调用相应的类就行
如果使用constructor自动装配策略,就不能混合使用constructor-arg 注解方式可以实现更细粒度的自动装配,Spring容器默认禁用注解装配,要在配置文件中开启 配置自动扫描 在基于Java的配置中使用@Configura
·工厂方法如果需要参数,都使用constructor-arg属性确定参数值。 ·其他依赖注入属性,都使用property 元素确定参数值。 Person.java PersonFactory.java Chinese.java American.java bean.xml SpringTest.java
<constructor-arg name="name" value="SHAREJSESSIONID" /> <!-- jsessionId的path为 / 用于多个系统共享jsessionId --> <property name="path" value="/" /> <property name="httpOnly" value="true"/> ...
<constructor-arg index="0"> <bean class="net.spy.memcached.spring.MemcachedClientFactoryBean" p:servers="127.0.0.1:11211" p:protocol="BINARY" p:locatorType="ARRAY_MOD" p:failureMode=...
<constructor-arg name="zookeeper" value="${zookeeper.address}"/> <constructor-arg name="serverAddress" value="${server.address}"/> </beans>2. 服务接口public interface UserService { ...
<constructor-arg index="1" value="127.0.0.1" name="host" type="java.lang.String"/> <!-- 这种 arguments 构造的方式,之前配置有缺点。 这里之前的配置有问题,因为参数类型不一致,有时候jar和环境...
编年史集合 Spring Utils 编年史地图 可以通过在 Spring 中设置属性来配置 Chronicle Maps。 这些属性与 ChronicleMapBuilder 类用来创建和配置 ChronicleMap 的属性相对应。... constructor-arg name = " valueCla