知识点:
【
基本类型对象注入:
<bean id="orderService" class="cn.itcast.service.OrderServiceBean">
<constructor-arg index=“0” type=“java.lang.String” value=“xxx”/>//构造器注入
<property name=“name” value=“zhao/>//属性setter方法注入
</bean>
注入其他bean:
方式一
<bean id="orderDao" class="cn.itcast.service.OrderDaoBean"/>
<bean id="orderService" class="cn.itcast.service.OrderServiceBean">
<property name="orderDao" ref="orderDao"/>
</bean>
方式二(使用内部bean,但该bean不能被其他bean使用)
<bean id="orderService" class="cn.itcast.service.OrderServiceBean">
<property name="orderDao">
<bean class="cn.itcast.service.OrderDaoBean"/>
</property>
</bean>
】
环境搭建见上....
实现步骤:
第一步:建立PersonDaoBean 和 IpersonDao接口
IpersonDao.java
public interface IPersonDao {
public abstract void add();
}
PersonDaoBean.java
public class PersonDaoBean implements IPersonDao {
/* (non-Javadoc)
* @see com.liyong.PersonDaoBean.Imp.PersonDao#add()
*/
public void add(){
System.out.println("执行PersonDaoBean中的add()方法");
}
}
第二步:编写PersonServer和IPersonServer
IPersonServer.java
public interface IPersonServer {
public abstract void save();
}
PersonServer.java
public class PersonServer implements IPersonServer {
/*
* 注意必须为这些属性提供setter方法
* 通过属性注入依赖对象
*/
private IPersonDao personDao;
private String name;
public PersonServer(){}
/*
* 通过构造器来注入依赖对象
*/
public PersonServer(IPersonDao personDao,String name) {
this.name = name;
this.personDao = personDao;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public IPersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(IPersonDao personDao) {
this.personDao = personDao;
}
public void save()
{
personDao.add();
System.out.println("name="+name);
System.out.println("save is OK !");
}
}
第四步:编写beans.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-2.5.xsd">
<!--外部引用
<bean id="personDaoBean" class="com.liyong.PersonDaoBean.Imp.PersonDaoBean"/>
<bean id="personServer" class="com.liyong.ServersBean.PersonServer" >
<property name="personDao" ref="personDaoBean"/>
</bean>
-->
<!--使用内部bean,但该bean不能被其他bean使用
<bean id="personServer" class="com.liyong.ServersBean.PersonServer" >
<property name="personDao">
<bean class="com.liyong.PersonDaoBean.Imp.PersonDaoBean"/>
</property>
</bean>
-->
<!--使用构造器注入依赖对象-->
<bean id="personDaoBean" class="com.liyong.PersonDaoBean.Imp.PersonDaoBean"/>
<bean id="personServer" class="com.liyong.ServersBean.PersonServer">
<constructor-arg index="0" ref="personDaoBean"/>
<constructor-arg index="1" value="liyong"/>
</bean>
</beans>
第五步:编写单元测试
public class JUnitTest {
@Test
public void TestSave()
{
//得到Spring容器实例
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
//ApplicationContext ctx2 = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
//这里面向接口
IPersonServer server =(IPersonServer)ctx.getBean("personServer");
server.save();
}
}
第六步:部署
.....
标签:personDao,IPersonDao,依赖,name,IPersonServer,对象,void,public,注入 From: https://blog.51cto.com/u_16091571/6232153