依赖注入的原理
依赖注入的方式---XML配置 依赖注入的方式---注解的方式它的核心就是IOC和AOP。而IOC中实现Bean注入的实现方式之一就是DI(依赖注入)。
一 DI的原理 DI的基本原理:对象之间的依赖关系只会通过三种方式:构造函数参数,工厂方法的参数以及构造函数或工厂方法创建的对象属性设置。因此,容器的工作哦就是在创建Bean时注入所有的依赖关系。相对于由Bean自己控制其实例化,直接在构造器中指定依赖关系或者类似服务定位器(Service Locator)这三种自主控制依赖关系注入的方法,而控制权从根本上发生改变,即控制反转(Inverse of Controll)---IOC. 应用DI规则后,我们不用在关注对象之间的依赖关系,从而达到高层次的松耦合。DI有两种实现方式---Setter/getter方式(传值方式)和构造器方式(引用方式)。 下面就从XML配置和注解角度来介绍这两种方式。
二、DI的方式---XML配置
1. Setter/getter方法 下面是一个Sample1)、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"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <bean id="personDao" class="com.spring.dao.impl.PersonDaoImpl"></bean>
- <bean id="personService" class="com.spring.service.impl.PersonServiceBean" >
- <property name="personDao" ref="personDao"/>
- </bean>
- </beans>
2) PersonDao.
- package com.spring.dao;
- ublic interface PersonDao {
- public void add();
3) PersonDaoImpl.java
- package com.spring.dao.impl;
- import com.spring.dao.PersonDao;
- public class PersonDaoImpl implements PersonDao {
- public void add() {
- System.out.println("PersonDao.add() is running.");
- }
- }
- package com.spring.service;
- public interface PersonService {
- public void save();
- }
- package com.spring.service.impl;
- import com.spring.dao.*;
- import com.spring.service.PersonService;
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- public class PersonServiceBean implements PersonService {
- private PersonDao personDao;
- public PersonServiceBean(){
- System.out.println("personServiceBean.constructor() is running.");
- }
- public PersonDao getPersonDao() {
- return personDao;
- }
- public void setPersonDao(PersonDao personDao) {
- this.personDao = personDao;
- }
- public void save(){
- System.out.println("Name:" );
- personDao.add();
- }
- @PostConstruct
- public void init(){
- System.out.println("PersonServiceBean.init() is running.");
- }
- @PreDestroy
- public void destory(){
- System.out.println("PersonServiceBean.destory() is running.");
- }
- }
- package junit.test;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import org.springframework.context.support.AbstractApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.spring.service.PersonService;
- public class SpringIOCTest {
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- }
- @Test public void instanceSpring(){
- AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
- PersonService personService = (PersonService)context.getBean("personService");
- personService.save();
- // context.close();
- }
- }
- <bean id="personService">
- <property name="personDao">
- <bean/>
- </property>
- </bean>
以下是个例子
- <bean id="personDao" class="com.spring.dao.impl.PersonDaoImpl"></bean>
- <bean id="personService" class="com.spring.service.impl.PersonServiceBean" init-method="init" destroy-method="destory">
- <constructor-arg index="0" type="com.spring.dao.PersonDao" ref="personDao"/>
- <constructor-arg index="1" value="Jamson" />
- </bean>
- public class PersonServiceBean implements PersonService {
- private PersonDao personDao;
- private String name;
- public PersonServiceBean(){
- System.out.println("personServiceBean.constructor() is running.");
- }
- public PersonServiceBean(PersonDao personDao, String name) {
- this.personDao = personDao;
- this.name = name;
- }
- // public PersonDao getPersonDao() {
- // return personDao;
- // }
- // public void setPersonDao(PersonDao personDao) {
- // this.personDao = personDao;
- // }
- public void save(){
- System.out.println("Name:"+name );
- personDao.add();
- }
- @PostConstruct
- public void init(){
- System.out.println("PersonServiceBean.init() is running.");
- }
- @PreDestroy
- public void destory(){
- System.out.println("PersonServiceBean.destory() is running.");
- }
- }
控制台信息
- PersonServiceBean.init() is running.
- Name:Jamson
- PersonDao.add() is running.
- PersonServiceBean.destory() is running.
1)beans.xml
- <context:annotation-config/>
- <bean id="personDao" class="com.spring.dao.impl.PersonDaoImpl"></bean>
- <bean id="personService" class="com.spring.service.impl.PersonServiceBean" init-method="init" destroy-method="destory">
- </bean>
- package com.spring.service.impl;
- import com.spring.dao.*;
- import com.spring.service.PersonService;
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- import javax.annotation.Resource;
- public class PersonServiceBean implements PersonService {
- @Resource(name="personDao")
- private PersonDao personDao;
- private String name;
- public PersonServiceBean(){
- System.out.println("personServiceBean.constructor() is running.");
- }
- public PersonServiceBean(PersonDao personDao, String name) {
- this.personDao = personDao;
- this.name = name;
- }
- // public PersonDao getPersonDao() {
- // return personDao;
- // }
- // public void setPersonDao(PersonDao personDao) {
- // this.personDao = personDao;
- // }
- public void save(){
- System.out.println("Name:"+name );
- personDao.add();
- }
- @PostConstruct
- public void init(){
- System.out.println("PersonServiceBean.init() is running.");
- }
- @PreDestroy
- public void destory(){
- System.out.println("PersonServiceBean.destory() is running.");
- }
- }
==================================================