控制反转(Inversion of Control):在依赖注入的模式下,创建被调用者的工作不再由调用者来完成,因此称为控制反转;依赖注入(Dependency Injection):创建被调用者的实例通常由Spring容器来完成,然后注入调用者,因此称为依赖注入。不管是依赖注入,还是控制反转,都说明Spring采用动态、灵活的方式管理对象。为了更好地理解这一概念,下面编码模拟Spring依赖注入的原理。
1、首先,写一dao接口及其实现类(这里将两个不同的类代码合在了一起):
- public interface PersonDao {
- public void add();
- }
- public class PersonDaoImpl implements PersonDao {
- @Override
- public void add() {
- System.out.println("执行PersonDaoImpl中的add方法!");
- }
- }
2、写一service接口及其实现类(这里将两个不同的类代码合在了一起):
- public interface PersonService {
- public void save();
- }
- public class PersonServiceImpl implements PersonService {
- private PersonDao personDao;
- private int id;
- private String name;
- public PersonDao getPersonDao() {
- return personDao;
- }
- public void setPersonDao(PersonDao personDao) {
- this.personDao = personDao;
- }
- 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;
- }
- @Override
- public void save() {
- personDao.add();
- System.out.println("id=" + id + ",name=" + name);
- }
- }
3、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="com.wgy.dao.impl.PersonDaoImpl" />
- <bean id="personService" class="com.wgy.service.impl.PersonServiceImpl">
- <property name="personDao" ref="personDao" />
- <property name="name" value="wgy" /> <!-- 基本属性值的注入 -->
- <property name="id" value="88" />
- </bean>
- </beans>
4、PropertyDefinition类(封装从beans.xml中读取到的bean节点下的property信息)
- public class PropertyDefinition {
- private String name;
- private String ref;
- private String value;
- public PropertyDefinition(String name, String ref, String value) {
- this.name = name;
- this.ref = ref;
- this.value = value;
- }
- 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;
- }
- public String getValue() {
- return value;
- }
- public void setValue(String value) {
- this.value = value;
- }
- }
5、BeanDefinition类(封装从beans.xml中读取到的bean信息)
- 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;
- }
- }
6、MyClassPathXmlAppCtx类( 自定义容器:模拟ClassPathXmlApplicationContext类的作用)
- public class MyClassPathXmlAppCtx {
- private List<BeanDefinition> beanDefinitions = new ArrayList<BeanDefinition>();
- private Map<String, Object> instances = new HashMap<String, Object>();
- public MyClassPathXmlAppCtx(String filename) {
- readXML(filename);
- instanceBeans();
- injectObject();
- }
- /**
- * 使用dom4j解析beans.xml文件
- */
- @SuppressWarnings("unchecked")
- private void readXML(String filename) {
- SAXReader saxReader = new SAXReader();
- Document document = null;
- URL xmlPath = this.getClass().getClassLoader().getResource(filename);
- try {
- document = saxReader.read(xmlPath);
- } catch (Exception e) {
- e.printStackTrace();
- }
- Map<String, String> nsMap = new HashMap<String, String>();
- nsMap.put("ns", "http://www.springframework.org/schema/beans");// 加入命名空间
- XPath xsub = document.createXPath("//ns:beans/ns:bean");// 创建beans/bean查询路径
- xsub.setNamespaceURIs(nsMap);// 设置命名空间
- List<Element> beans = xsub.selectNodes(document);// 获取文档下所有bean节点
- for (Element bean : beans) {
- String id = bean.attributeValue("id");// 获取id属性值
- String clazz = bean.attributeValue("class");// 获取class属性值
- BeanDefinition beanDefinition = new BeanDefinition(id, clazz);
- XPath proPath = document.createXPath("ns:property");
- proPath.setNamespaceURIs(nsMap);
- List<Element> propertys = proPath.selectNodes(bean);
- for (Element property : propertys) {
- String propertyName = property.attributeValue("name");
- String propertyRef = property.attributeValue("ref");
- String propertyValue = property.attributeValue("value");
- PropertyDefinition propDefinition = new PropertyDefinition(
- propertyName, propertyRef, propertyValue);
- beanDefinition.getPropertys().add(propDefinition);
- }
- beanDefinitions.add(beanDefinition);
- }
- }
- /**
- * 完成bean的实例化
- */
- private void instanceBeans() {
- for (BeanDefinition beanDefinition : beanDefinitions) {
- String className = beanDefinition.getClassName();
- if (null != className && !"".equals(className.trim())) {
- System.out.println(className);
- try {
- instances.put(beanDefinition.getId(),
- Class.forName(className).newInstance());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * 为bean对象的属性注入值
- */
- private void injectObject() {
- for (BeanDefinition beanDefinition : beanDefinitions) {
- Object bean = instances.get(beanDefinition.getId());
- if (bean != null) {
- try {
- PropertyDescriptor[] pds = Introspector.getBeanInfo(
- bean.getClass()).getPropertyDescriptors();
- for (PropertyDefinition propertyDefinition : beanDefinition
- .getPropertys()) {
- for (PropertyDescriptor pd : pds) {
- if (propertyDefinition.getName().equals(
- pd.getName())) {
- Method setter = pd.getWriteMethod(); // 获取属性的setter方法
- // setter.setAccessible(true);
- if (setter != null) {
- Object value = null;
- String ref = propertyDefinition.getRef();
- if (ref != null && !"".equals(ref)) {
- value = instances
- .get(propertyDefinition
- .getRef());
- } else {
- value = ConvertUtils.convert(
- propertyDefinition.getValue(),
- pd.getPropertyType());
- }
- setter.invoke(bean, value); // 把引用对象注入到属性
- }
- break;
- }
- }
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * 获取bean的实例
- */
- public Object getBean(String beanName) {
- return instances.get(beanName);
- }
- }
7、写一单元测试方法:
- @Test
- public void instanceSpring() {
- // ApplicationContext ctx = new
- // ClassPathXmlApplicationContext("beans.xml");
- MyClassPathXmlAppCtx ctx = new MyClassPathXmlAppCtx("beans.xml");
- PersonService personService = (PersonService) ctx.getBean("personService");
- personService.save();
- }