Spring依赖注入之对象注入
2010-11-18 22:12
423 查看
依赖注入(Dependency Injection)
当我们把依赖对象交给外部容器负责创建,那么PersonServiceBean 类可以改成如下:
public class PersonServiceBean {
private PersonDao personDao ;
//通过构造器参数,让容器把创建好的依赖对象注入进PersonServiceBean,当然也可以使用setter方法进行注入。
public PersonServiceBean(PersonDao personDao){
this.personDao=personDao;
}
public void save(Person person){
personDao.save(person);
}
}
所谓依赖注入就是指:在运行期,由外部容器动态地将依赖对象注入到组件中。
依赖对象:指PersonServiceBean依赖对象personDao。
beans.xml:
<bean id="userServiceDao" class="com.asm.dao.UserServiceDao">
<constructor-arg index="0"><value>1</value></constructor-arg>
<constructor-arg index="1"><value>qqqq</value></constructor-arg>
</bean>
<bean id="userServiceBean" class="com.asm.service.UserServiceBean.UserServiceBean">
<constructor-arg index="0"><value>Yee</value></constructor-arg>
<constructor-arg index="1">
<ref bean="userServiceDao"/>
</constructor-arg>
<!--
此例为依赖注入
<property name="userServiceDao">
<ref bean="userServiceDao" />
</property>
-->
</bean>
对象userServiceDao注入到userServiceBean,先在配置文件中写userServiceDao的bean,并注入属性值。然后写userServiceBean的bean,利用构造方法依赖注入属性和对象。
所谓控制反转就是应用本身不负责依赖对象的创建及维护,依赖对象的创建及维护是由外部容器负责的。这样控制权就由应用转移到了外部容器,控制权的转移就是所谓反转。
测试类test中放入方法:
public void testSave() {
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
UserServiceBean userServiceBean = (UserServiceBean) ctx.getBean("userServiceBean");
//既是控制反转
System.out.println(userServiceBean.userservicedao.getId());
System.out.println(userServiceBean.userservicedao.getName());
System.out.println(userServiceBean.getUsername());
}
当我们把依赖对象交给外部容器负责创建,那么PersonServiceBean 类可以改成如下:
public class PersonServiceBean {
private PersonDao personDao ;
//通过构造器参数,让容器把创建好的依赖对象注入进PersonServiceBean,当然也可以使用setter方法进行注入。
public PersonServiceBean(PersonDao personDao){
this.personDao=personDao;
}
public void save(Person person){
personDao.save(person);
}
}
所谓依赖注入就是指:在运行期,由外部容器动态地将依赖对象注入到组件中。
依赖对象:指PersonServiceBean依赖对象personDao。
beans.xml:
<bean id="userServiceDao" class="com.asm.dao.UserServiceDao">
<constructor-arg index="0"><value>1</value></constructor-arg>
<constructor-arg index="1"><value>qqqq</value></constructor-arg>
</bean>
<bean id="userServiceBean" class="com.asm.service.UserServiceBean.UserServiceBean">
<constructor-arg index="0"><value>Yee</value></constructor-arg>
<constructor-arg index="1">
<ref bean="userServiceDao"/>
</constructor-arg>
<!--
此例为依赖注入
<property name="userServiceDao">
<ref bean="userServiceDao" />
</property>
-->
</bean>
对象userServiceDao注入到userServiceBean,先在配置文件中写userServiceDao的bean,并注入属性值。然后写userServiceBean的bean,利用构造方法依赖注入属性和对象。
所谓控制反转就是应用本身不负责依赖对象的创建及维护,依赖对象的创建及维护是由外部容器负责的。这样控制权就由应用转移到了外部容器,控制权的转移就是所谓反转。
测试类test中放入方法:
public void testSave() {
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
UserServiceBean userServiceBean = (UserServiceBean) ctx.getBean("userServiceBean");
//既是控制反转
System.out.println(userServiceBean.userservicedao.getId());
System.out.println(userServiceBean.userservicedao.getName());
System.out.println(userServiceBean.getUsername());
}
相关文章推荐
- Spring给普通对象注入依赖
- Spring整理系列(02)——spring依赖注入,组装对象之间的依赖关系
- Spring获取Bean对象以及依赖注入的实现原理
- 用spring做领域对象的依赖注入
- Spring (三)注入依赖对象、注入集合类型的属性
- Spring注入依赖对象
- Spring学习07--Bean对象的初始化(IOC的依赖注入)原理
- 模拟spring注入依赖对象
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- Spring之对象依赖关系(依赖注入Dependency Injection)
- 85——Spring中对象依赖注入,给对象属性赋值,包含哪些内容(1条)
- 用spring的注解建立bean与bean之间的关系组件装配及自动注入依赖对象
- spring基于xml文件注入依赖对象
- spring.net学习笔记1--依赖对象注入
- spring容器DI依赖注入对象<三>
- spring基于xml文件注入依赖对象
- Spring.NET依赖注入框架学习-- 泛型对象的创建和使用
- Spring中利用AOP对领域对象(非依赖注入的对象,如new操作符、反射的方式生存的对象)下面是spring配置文件的一部分 <context:load-time-weaver /> <be
- Spring之对象依赖关系(依赖注入Dependency Injection)