Spring的两种依赖注入方式:setter方法注入与构造方法注入
2018-04-06 14:24
686 查看
Spring的两种依赖注入方式:setter注入与构造方法注入,这两种方法的不同主要就是在xml文件下对应使用property和constructor-arg属性, 例如:
property属性:<property name="id" value="123"></property>(其中name的值为原类中的属性名)
constructor-arg属性:<constructor-arg index="0" value="456"></constructor-arg>(其中index的值为0~n-1,n代表构造函数中的输入参数的数量)
1.setter方法注入
setter方法注入即是创建一个普通的JavaBean类,为需要注入的属性通过对应的setter方法即可,如:
(1)创建一个Id类:
[java] view plain copypackage com.loster.li;
public class Id {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(2)创建配置文件Id_Bean.xml
[html] view plain copy<?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-2.5.xsd">
<bean id="id" class="com.loster.li.Id">
<property name="id" value="123"></property>
<property name="name" value="xiaoli"></property>
</bean>
</beans>
(3)编写测试类IdTest.java,并运行:[java] view plain copypackage com.loster.li;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class IdTest {
public static void main(String[] args){
ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext("com/loster/li/Id_Bean.xml");
Id id = (Id)context.getBean("id");
System.out.println(id.getId());
System.out.println(id.getName());
}
}
运行结果如下:
2.构造方法注入
(1)将上面的Id.class修改为:
[java] view plain copypackage com.loster.li;
public class Id {
private int id;
private String name;
public Id(int id,String name){
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(2)将上面的Id_Bean.xml修改为:
[html] view plain copy<?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-2.5.xsd">
<bean id="id" class="com.loster.li.Id">
<constructor-arg index="0" value="456"></constructor-arg>
<constructor-arg index="1" value="dawang"></constructor-arg>
</bean>
</beans>
(3)再次运行IdTest.java,运行结果如下:
property属性:<property name="id" value="123"></property>(其中name的值为原类中的属性名)
constructor-arg属性:<constructor-arg index="0" value="456"></constructor-arg>(其中index的值为0~n-1,n代表构造函数中的输入参数的数量)
1.setter方法注入
setter方法注入即是创建一个普通的JavaBean类,为需要注入的属性通过对应的setter方法即可,如:
(1)创建一个Id类:
[java] view plain copypackage com.loster.li;
public class Id {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(2)创建配置文件Id_Bean.xml
[html] view plain copy<?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-2.5.xsd">
<bean id="id" class="com.loster.li.Id">
<property name="id" value="123"></property>
<property name="name" value="xiaoli"></property>
</bean>
</beans>
(3)编写测试类IdTest.java,并运行:[java] view plain copypackage com.loster.li;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class IdTest {
public static void main(String[] args){
ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext("com/loster/li/Id_Bean.xml");
Id id = (Id)context.getBean("id");
System.out.println(id.getId());
System.out.println(id.getName());
}
}
运行结果如下:
2.构造方法注入
(1)将上面的Id.class修改为:
[java] view plain copypackage com.loster.li;
public class Id {
private int id;
private String name;
public Id(int id,String name){
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(2)将上面的Id_Bean.xml修改为:
[html] view plain copy<?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-2.5.xsd">
<bean id="id" class="com.loster.li.Id">
<constructor-arg index="0" value="456"></constructor-arg>
<constructor-arg index="1" value="dawang"></constructor-arg>
</bean>
</beans>
(3)再次运行IdTest.java,运行结果如下:
相关文章推荐
- Spring的两种依赖注入方式:setter方法注入与构造方法注入
- Spring的两种依赖注入方式:setter方法注入与构造方法注入 以及两种方式的区别!
- [优质例子存档]Spring的两种依赖注入方式:setter方法注入与构造方法注入
- Spring依赖注入方式_属性setter方法注入_构造器注入_注解注入
- spring Ioc 依赖注入的三种方式:构造函数注入、setter方法注入和接口注入
- Spring的两种常见的注入方式 构造方法注入以及setter方法注入
- Spring(六)属性依赖注入(构造方法,setter方法,p命名空间,spel表达式,集合注入)
- Spring依赖注入的三种方式详解之二:构造方法注入
- Spring依赖注入的三种方式详解之二:构造方法注入
- Spring通过构造方法注入的四种方式
- Spring 依赖注入的两种方式
- Spring通过构造方法注入的四种方式
- Spring中依赖注入的两种方式的区别
- [Java] Spring3.0 setter 注入 / 留印象构造方法注入 7、8、9.avi
- Spring第六弹—-依赖注入之使用构造器注入与使用属性setter方法注入
- Spring依赖注入的三种方式详解之三:工厂方法注入
- Spring_3_Spring的依赖注入_setter方法和构造函数方法
- Spring通过构造方法注入的四种方式
- 7.3.4: spring的核心机制依赖注入---两种注入方式的对比
- Spring - 基于构造方法的依赖注入