| Spring Xml 文件的配置 参数 属性 说明 摘自:blog.csdn.net/futurehuhu/…1、value元素<value/>元素通过字符串来指定属性或构造器参数的值。<bean id="myDataSource" detroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </proerpty> <property name="url"> <value>jdbc:mysql://localhost:3306/mydb</value> </property> <property name="username"> <vlaue>root</value> </property> </bean>2、idref元素idref元素用来将容器内其它bean的id传给<constructor-arg/>或<property/>元素,同时提供错误难功能。<bean id="theTargetBean" class="..."/> <bean id="theClientBean" class="..."> <property name="targetName"> <idref bean="theTargetBean" /> </property> </bean>等同于:<bean id="theTargetBean" class="..." /> <bean id="theClientBean" class="..."> <property name="targetName"> <value>theTargetBean</value> </property> </bean>使用idref标记允许容器在部署时验证所被引用的bean是否存在。此外,如果被引用的bean在同一XML文件内,且bean名字就是bean id,那么可以使用local属性。 此属性允许XML解析器在解析XML文件时来对引用的bean进行验证。<property name="targetName"> <idref local="theTargetBean" /> </property>3、ref元素形式一:<ref bean="someBean"> 这是最常见的形式是通过使用ref标记指定bean属性的目标bean,通过该标签可以引用同一容器或父容器内的任何bean(无论是否在同一XML文件中)。 XML‘bean’元素的值即可以是指定的bean的id值也可以是其name值。形式二:<ref local="someBean"> 使用ref的local属性指定目标bean,它可以利用XML解析器来难所引用的bean是否存在同一文件中。local属性值必须是目标bean的id属性值。形式三:<bean parent="someBean"> 通过使用ref的parent属性来引用当前窗口的父容器中的bean。parent属性值即可以是目标bean的id值,也可以是name属性值。4、内部 bean:所 谓内部bean(inner bean)是指在一个bean的<property/>或<constructor-arg/>中使用< bean/>元素定义的bean.内部bean不需要有id或name属性,即使有也会被窗口忽略. 内部bean总是匿名的且它们总是prototype模式的.同时将内部bean注入到包含该内部bean之外的bean是不可能的. <bean id="outer" class="..."> <property name="target"> <bean class="com.mycoompany.Person"> <property name="name" value="Fiona Apple"/> <property name="age" value="25"/> </bean> </property> </bean>5、集合合并:从Spring2.0开始,Spring IoC容器将支持集合的合并。父子集合元素合并后的值就是子集合中的最终结果,而且子集合中的元素值将覆盖父集合中的对应的值。 <beans> <bean id="parent" abstract="true" class="example.ComplexObject"> <property name="adminEmails"> <props> <prop key="administrator">administrator@somecompany.com</prop> <prop key="support">support@somecompany.com</prop> </props> </property> </bean> <bean id="child" parent="parent"> <property name="adminEmails"> <props merge="trur"> <prop key="sales">sales@somecompany.com</prop> <prop key="support">support@somecompany.co.uk</prop> </props> </property> </bean> </beans>合并后内容:administrator=administrator@somecompany.com sales=sales@somecompany.com support=support@somecompany.co.uklist集合有排序功能,父bean的列表内容将排在子bean列表内容的前面; merge属性必须在继承的子bean中定义。6、Nulls<null/>用于处理null值。Spring会把属性的空参数当作空字符串处理。<bean class="ExampleBean"> <property name="email"> <value></value> </property> </bean>等同于excapleBean.setEamil("");而null值则可以使用<null/>元素来表示:<bean class="ExampleBean"> <property name="email"><null/></property> </bean>7、简写:针对常见的value值或bean的引用,Spring提供了简化格式用于替代<value/>和<ref/>元素。 <property/>、<constructor-arg/>、<entry/>元素都支持value属性,它可以用来替代内嵌的<value/>元素。<property name="myProperty"> <value>hello</value> ===== <property name="myProperty" value="helo" /> </property><constructor-arg> <value>hello</value> ===== <constructor-arg value="hello" /> </construtctor-arg><entry key="myKey"> <value>hello</value> ===== <entry key="myKey" value="hello" /> </entry><property/>和<constructor-arg/>支持类似的简写属性ref,它可以替找整个内嵌的</ref>元素。<property name="myProperty"> <ref bean="myBean"> ===== <property name="myProperty" ref="myBean" /> </property><constructor-arg> <ref bean="myBean"> ===== <constructor-arg ref="myBean" /> </constructor-arg>切记:尽管存在等同于<ref bean="xxx" >元素的简写形式,但并没有<ref local="xxx">的简写形式。map中的entry元素的简写形式为key/key-ref和value/value-ref属性。<entry> <key> <ref bean="myKeyBean" /> ===== <entry key-ref="myKeyBean" value-ref="myValueBean" /> </key> <ref bean="myValueBean" /> </entry>8、组合属性名称当设置bean的组合属性时,除了最后一下属性外,只要其他属性值不为null,组合或嵌套属性名是完全合法的。<bean id="foo" class="foo.Bar"> <property name="fred.bob.sammy" value="123" /> </bean> 9、depends-on属性:depends-on属性可以用于当前bean初始化之前显式的强制一个或多个bean被初始化。<bean id="beanOne" class="ExampleBean" depends-on="manager"> <property name="manager" ref="manager" /> </bean> <bean id="manager" class="ManagerBean" />若需要表达对多个bean的依赖,可民认在<depends-on />中将指定的多个bean名字用分隔符进行分隔,分隔符可以是逗号、空格及分号等。<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao"> <property name="manager" ref="manager" /> </bean> <bean id="manager" class="ManagerBean" /> <bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />10、延迟初始化bean--lazy-init 属性:<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"> <!-- various properties here... --> </bean> <bean name="noo.lazy" class="com.foo.AnotherBean"> <!-- various properties here... --> </bean>如 果一个bean被设置为延迟初始化,而另一个非延迟初始化的singleton bean依赖于它,那么当ApplicationContext提前实例化singleton bean时,它必须也确保所有上述singleton依赖bean也被预先初始化,当然也包括设置为延迟实例化的bean.在容器层次中通过在<beans />元素上使用‘default-lazy-init’属性来控制延迟初始化也是可能的。 <beans default-lazy-init="true"> <!-- no beans will be eagerly pre-instantiated... --> </beans>11、autowire<自动装配> 属性:模式 说明no 不使用自动装配,必须通过ref元素指定依赖,这是默认设置。byName 根据属性名自动装配。Spring将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。byType 如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个,则抛出异常。constructor 与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中未找到与构造器参数类型一致的bean,那么将抛出异常。autodetect 通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式。--通过设置<bean />元素的autowire-candidate="false",可以针对单个bean设置其是否为被自动装配对象。12、dependency-check <依赖检查> 属性:此属性用于检查bean定义中实际属性值的设置。模式 说明none 没有依赖检查,如果bean的属性没有值的话可以不用设置。simple 对于原始类型及集合(除协作者外的一切东西)执行依赖检查。object 仅对协作者执行依赖检查员。all 对协作者,原始类型及集合执行依赖检查。 3.2.2. 实例化容器 Spring IoC容器的实例化非常简单,如下面的例子: Resource resource = new FileSystemResource("beans.xml"); BeanFactory factory = new XmlBeanFactory(resource); ... 或... ClassPathResource resource = new ClassPathResource("beans.xml"); BeanFactory factory = new XmlBeanFactory(resource); ... 或... ```
ApplicationContext context = new ClassPathXmlApplicationContext( new String[] {"applicationContext.xml", "applicationContext-part2.xml"}); // of course, an ApplicationContext is just a BeanFactory BeanFactory factory = (BeanFactory) context;
| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| class | [Section 3.2.3.2, “实例化bean”](http://www.jactiongroup.net/reference2/html/beans.html#beans-factory-class "3.2.3.2. 实例化bean") |
| name | [Section 3.2.3.1, “命名bean”](http://www.jactiongroup.net/reference2/html/beans.html#beans-beanname "3.2.3.1. 命名bean") |
| scope | [Section 3.4, “bean的作用域”](http://www.jactiongroup.net/reference2/html/beans.html#beans-factory-scopes "3.4.\ bean的作用域") |
| constructor arguments | [Section 3.3.1, “注入依赖”](http://www.jactiongroup.net/reference2/html/beans.html#beans-factory-collaborators "3.3.1. 注入依赖") |
| properties | [Section 3.3.1, “注入依赖”](http://www.jactiongroup.net/reference2/html/beans.html#beans-factory-collaborators "3.3.1. 注入依赖") |
| autowiring mode | [Section 3.3.6, “自动装配(autowire)协作者”](http://www.jactiongroup.net/reference2/html/beans.html#beans-factory-autowire "3.3.6. 自动装配(autowire)协作者") |
| dependency checking mode | [Section 3.3.7, “依赖检查”](http://www.jactiongroup.net/reference2/html/beans.html#beans-factory-dependencies "3.3.7. 依赖检查") |
| lazy-initialization mode | [Section 3.3.5, “延迟初始化bean”](http://www.jactiongroup.net/reference2/html/beans.html#beans-factory-lazy-init "3.3.5. 延迟初始化bean") |
| initialization method | [Section 3.5.1, “Lifecycle接口”](http://www.jactiongroup.net/reference2/html/beans.html#beans-factory-lifecycle "3.5.1.\ Lifecycle接口") |
| destruction method | [Section 3.5.1, “Lifecycle接口”](http://www.jactiongroup.net/reference2/html/beans.html#beans-factory-lifecycle "3.5.1.\ Lifecycle接口") | | |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | - |