Spring的构造注入和设值注入
构造注入
在类被实例化的时候,它的构造方法被调用并且只能调用一次。所以它被用于类的初始化操作。<constructor-arg>是<bean>标签的子标签。通过其<value>子标签可以为构造
先建立一个学生类:
public class Student {
String name;
String address;
int age;
public Student(String name,String address,int age) {
this.address = address;
this.age = age;
this.name = name;
}
public void say(){
System.out.println(name+address+age);
}
}
创建装 配 JavaBean 的 配 置 文 件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="stu" class="test04.Student">
<constructor-arg>
<value>Json</value>
</constructor-arg>
<constructor-arg>
<value>China</value>
</constructor-arg>
<constructor-arg>
<value>88</value>
</constructor-arg>
</bean>
</beans>
测试类:
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("c.xml");
Student student = (Student) context.getBean("stu");
student.say();
}
输出:
四月 23, 2017 3:43:34 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7987b796: startup date [Sun Apr 23 15:43:34 CST 2017]; root of context hierarchy
四月 23, 2017 3:43:34 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [c.xml]
四月 23, 2017 3:43:35 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2d615ef2: defining beans [stu]; root of factory hierarchy
JsonChina88
通过这个实例,可以看到容器通过多个<constructor-arg>标签完成了对构造方法的传参,但是如果标签的赋值顺序与构造方法中参数的顺序或参数类型不同,程序会产生异常。<constructor-arg>标签可以使用“index”属性和“type”属性解决此类问题。下面分别介绍这两个属性的用法。 type 属性:可以指定参数类型以确定要为构造方法的哪个参数赋值。当需要赋值的属性在构造方法中没有相同的类型时,可以使用这个参数。
index 属性:用于指定构造方法的参数索引,指定当前<constructor-arg>标签为构造方法的那个参数赋值。因为程序中含有两个 String 类型的参数,type 属性无法做判断, 导致 name 和 sex 参数无法判断正确性。这时需要设置 index 属性定义赋值索引来指定构造方法的参数位置。
设值注入
一个简单的 JavaBean 最明显的规则就是以一个私有属性对应 set()和 get()方法,来实现对属性的封装。既然 JavaBean 有 set()方法来设置 Bean 的属性,spring 就会有相应的支持。除了为构造方法传递参数的<constructor-arg>标签之外,<property>标签可以为JavaBean 的 set()方法传递参数,即通过 set()方法为属性赋值。
一样创建学生类:
package test03;
public class Student {
String name;
String adderss;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAdderss() {
return adderss;
}
public void setAdderss(String adderss) {
this.adderss = adderss;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="stu" class="test03.Student">
<property name="name" value="Josn"></property>
<property name="age" value="11"></property>
<property name="adderss" value="China"></property>
</bean>
</beans>
测试类:
package test03;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("b.xml");
Student student = (Student) context.getBean("stu");
System.out.println(student.getAdderss());
System.out.println(student.getAge());
System.out.println(student.getName());
}
}
输出:
四月 23, 2017 3:47:16 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@37f02eaa: startup date [Sun Apr 23 15:47:16 CST 2017]; root of context hierarchy
四月 23, 2017 3:47:16 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [b.xml]
四月 23, 2017 3:47:16 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3a8ff4b0: defining beans [stu]; root of factory hierarchy
China
11
Josn