为了便于理解Spring属性注入,这里来模拟Spring容器是如何实现将PersonDaoBean注入到PersonServiceBean的。
所需jar包:下载地址http://download.csdn.net/detail/jeofey/8747927

PersonDaoBean.java
-
package xjj.dao.impl;
-
-
import xjj.dao.PersonDao;
-
-
public class PersonDaoBean implements PersonDao {
-
public void add(){
-
System.out.println("执行PersonDaoBean中的add()方法");
-
}
-
}
PersonDao.java接口
-
package xjj.dao;
-
-
public interface PersonDao {
-
-
public void add();
-
-
}
PersonServiceBean.java
-
package xjj.service.impl;
-
-
import xjj.dao.PersonDao;
-
import xjj.service.PersonService;
-
-
public class PersonServiceBean implements PersonService {
-
private PersonDao personDao;
-
-
public PersonDao getPersonDao() {
-
return personDao;
-
}
-
-
public void setPersonDao(PersonDao personDao) {
-
this.personDao = personDao;
-
}
-
-
public void save(){
-
personDao.add();
-
}
-
}
PersonService.java接口
-
package xjj.service;
-
-
public interface PersonService {
-
-
public void save();
-
-
}
BeanDefinition.java
-
package junit.test;
-
-
import java.util.ArrayList;
-
import java.util.List;
-
-
public class BeanDefinition {
-
private String id;
-
private String className;
-
private List<PropertyDefinition> propertys = new ArrayList<PropertyDefinition>();
-
-
public BeanDefinition(String id, String className) {
-
this.id = id;
-
this.className = className;
-
}
-
public String getId() {
-
return id;
-
}
-
public void setId(String id) {
-
this.id = id;
-
}
-
public String getClassName() {
-
return className;
-
}
-
public void setClassName(String className) {
-
this.className = className;
-
}
-
public List<PropertyDefinition> getPropertys() {
-
return propertys;
-
}
-
public void setPropertys(List<PropertyDefinition> propertys) {
-
this.propertys = propertys;
-
}
-
-
}
PropertyDefinition.java
-
package junit.test;
-
-
public class PropertyDefinition {
-
private String name;
-
private String ref;
-
-
public PropertyDefinition(String name, String ref) {
-
this.name = name;
-
this.ref = ref;
-
}
-
-
public String getName() {
-
return name;
-
}
-
public void setName(String name) {
-
this.name = name;
-
}
-
public String getRef() {
-
return ref;
-
}
-
public void setRef(String ref) {
-
this.ref = ref;
-
}
-
-
}
XjjClassPathXMLApplicationContext.java
SpringTest.java
-
package junit.test;
-
-
import org.junit.BeforeClass;
-
import org.junit.Test;
-
-
import xjj.service.PersonService;
-
-
public class SpringTest {
-
-
@BeforeClass
-
public static void setUpBeforeClass() throws Exception {
-
}
-
-
@Test public void instanceSpring(){
-
XjjClassPathXMLApplicationContext ctx = new XjjClassPathXMLApplicationContext("beans.xml");
-
PersonService personService = (PersonService)ctx.getBean("personService");
-
personService.save();
-
}
-
}
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="personDao" class="xjj.dao.impl.PersonDaoBean"></bean>
-
<bean id="personService" class="xjj.service.impl.PersonServiceBean">
-
<property name="personDao" ref="personDao"></property>
-
</bean>
-
</beans>
结果:

Spring2.5学习2.2_编码剖析Spring依赖注入原理
原文:http://blog.csdn.net/jeofey/article/details/46289621