Spring学习历程---Bean基于xml的装配
2016-10-13 21:51
561 查看
先附上代码:
User.java
package com.wanhao; public class User { private String name; private String password; public User() { name=""; password=""; } public User(String name,String psw) { this.name=name; this.password=psw; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "name:"+name+"password:"+password; } }applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user1" class="com.wanhao.User"> <constructor-arg index="0" value="wh"></constructor-arg> <constructor-arg index="1" value="wh6"></constructor-arg> </bean> <bean id="user2" class="com.wanhao.User"> <property name="name" value="wh666"></property> <property name="password" value="wh666"></property> </bean> </beans>Main.java
public class Main { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println(ac.getBean("user1")); System.out.println(ac.getBean("user2")); } }
1.如果字面值包含特殊字符,可以使用<![CDATA[]]>包裹起来
2.属性值也可以使用value子节点进行配置
<constructor-arg type="java.lang.String">
<value><![CDATA[<ShangHai^>]]></value>
</constructor-arg>
3.Bean的配置文件中,可以通过<ref>元素,或ref属性为Bean的属性或构造器草书指定对Bean的引用还可以声明内部Bean。
4.可以通过<null/>指定空元素。
5.为级联属性赋值,注意:属性先要初始化,才会为级联属性赋值。
<property name="car.speed" value="250"></property>
6.为集合list属性赋值
<property name="cars">
<list>
<ref bean="car1"/>
<ref bean="car2"/>
</list>
</property>
map集合赋值
<property name="cars">
<map>
<entry key="AA" value-ref="car"/>
<entry key="BB" value-ref="car2"/>
</map>
</property>
7.配置单例的集合bean,以供多个bean进行引用,需要导入 util 命名空间,
刚一执行到 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");则装载对象,每次getBean(),参数相同时,也是得到同一对象。反之若是把scope设置为prototype,则每次getBean()都会返回全新的对象。
<util:list id="cars">
<ref bean="car"/>
<ref bean="car2"/>
</util:list>
为集合list属性赋值
<property name="cars" ref="cars"></property>
8.通过p标签,为bean的属性赋值,需要先导入p命名空间
<bean id="person5" class="com.collection.Person" p:age="30" p:name="Queen" p:cars-ref="cars"/>
Constructor argument resolution
Constructor argument resolution matching occurs using the argument's type. If no potential ambiguity exists in the constructor arguments of a bean definition, then the order in which the constructor arguments are defined in a bean definition is the order in
which those arguments are supplied to the appropriate constructor when the bean is being instantiated. Consider the following class:
package x.y; public class Foo { public Foo(Bar bar, Baz baz) { // ... } }
No potential ambiguity exists, assuming that
Barand
Bazclasses are not related by inheritance. Thus the following configuration works fine, and you
do not need to specify the constructor argument indexes and/or types explicitly in the
<constructor-arg/>element.
<beans> <bean id="foo" class="x.y.Foo"> <constructor-arg ref="bar"/> <constructor-arg ref="baz"/> </bean> <bean id="bar" class="x.y.Bar"/> <bean id="baz" class="x.y.Baz"/> </beans>
When another bean is referenced, the type is known, and matching can occur (as was the case with the preceding example). When a simple type is used, such as
<value>true<value>, Spring cannot determine the type
of the value, and so cannot match by type without help. Consider the following class:
package examples; public class ExampleBean { // No. of years to the calculate the Ultimate Answer private int years; // The Answer to Life, the Universe, and Everything private String ultimateAnswer; public ExampleBean(int years, String ultimateAnswer) { this.years = years; this.ultimateAnswer = ultimateAnswer; } }
Constructor argument type matching
In the preceding scenario, the container can use type matching with simple types if you explicitly specify the type of the constructor argument using the
typeattribute.
For example:
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg type="int" value="7500000"/> <constructor-arg type="java.lang.String" value="42"/> </bean>
Constructor argument index
Use the
indexattribute to specify explicitly the index of constructor arguments. For example:
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg index="0" value="7500000"/> <constructor-arg index="1" value="42"/> </bean>
In addition to resolving the ambiguity of multiple simple values, specifying an index resolves ambiguity where a constructor has two arguments of the same type. Note that the index is
0 based.
Constructor argument name
As of Spring 3.0 you can also use the constructor parameter name for value disambiguation:
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg name="years" value="7500000"/> <constructor-arg name="ultimateanswer" value="42"/> </bean>
Keep in mind that to make this work out of the box your code must be compiled with the debug flag enabled so that Spring can look up the parameter name from the constructor. If you can't compile your code with
debug flag (or don't want to) you can use
@ConstructorPropertiesJDK
annotation to explicitly name your constructor arguments. The sample class would then have to look as follows:
package examples; public class ExampleBean { // Fields omitted @ConstructorProperties({"years", "ultimateAnswer"}) public ExampleBean(int years, String ultimateAnswer) { this.years = years; this.ultimateAnswer = ultimateAnswer; } }
相关文章推荐
- Spring(五)基于XML装配bean(作用域)
- Spring学习--xml 中 Bean 的自动装配
- Spring(1-1、基于xml装配Bean)
- Spring 装配Bean 基于XML
- Spring4 学习系列之——基于Bean的xml配置
- Spring4 学习笔记(3)-Spring 基于 XML 的方式配置 Bean
- Spring装配bean基于XML,注解
- spring笔记(二)---基于XML的Bean装配
- Spring4 学习笔记(3)-Spring 基于 XML 的方式配置 Bean(供自己学习)
- Spring入门学习——用XML配置自动装配Bean
- Spring基础——在 Spring Config 文件中基于 XML 的 Bean 的自动装配
- Spring中 基于注解的装配Bean 与 基于xml的对比总结
- Spring学习-06:IOC容器装配Bean(xml配置方式)详解
- spring基础-Bean的装配方式(一)基于xml的装配
- spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- 【Java.Spring.Core】【IoC】基于注解的Bean装配(非XML配置)
- Spring基础——在 Spring Config 文件中基于 XML 的 Bean 的自动装配
- Spring In Action(二):基于XML配置装配bean
- spring装配Bean(基于xml)
- Spring学习笔记之通过XML装配Bean的一些细节