您的位置:首页 > 编程语言 > Java开发

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 
Bar
 and 
Baz
 classes 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 
type
 attribute.
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 
index
 attribute 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 
@ConstructorProperties
 JDK
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息