依赖注入的原理
依赖注入的方式---XML配置
依赖注入的方式---注解的方式
Spring 它的核心就是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方法
下面是一个Sample
1)、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.java
- 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.");
- }
- }
4) PersonService.java
- package com.spring.service;
- public interface PersonService {
- public void save();
- }
5) PersonServiceBean.java
- 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.");
- }
- }
6) SpringIOCTest.java(测试类)
- 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();
- }
- }
我们还可以采用内部注入的方式来处理:
Beans.xml修改如下:
- <bean id="personService">
- <property name="personDao">
- <bean/>
- </property>
- </bean>
2、构造器注入
以下是个例子
- <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>
2) PersonService.java:同Setter方法注入
3) PersonServiceBean.java
- 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.");
- }
- }
4) PersonDao.java:同Setter方法注入
5) PersonDaoImpl.java:同Setter方法注入
6) 测试类(SpringIOCTest.java):同上
控制台信息
- PersonServiceBean.init() is running.
- Name:Jamson
- PersonDao.add() is running.
- PersonServiceBean.destory() is running.
三 DI的方式---注解的配置
自从Jdk5中引入Annotation类后,在EJB和Spring中得到广泛的使用,也越来越被开发者们在平时的应用中使用。主要是用在Field上
的注入。在日常的应用开发中,一个项目中有很多的bean,如果使用XML文件配置,就会导致配置文件难以管理。使用注解注入的时候,能够使bean的配
置文件内容不至于繁杂。
首先介绍下注解的两种方式:@Resource(javax.annotation.Resource)和@Autowired.
@Resource:首先按照名称去寻找当前的bean,如果找不到的话,那就以类型装配。
@Autowired:首先按照类型去寻找当前的bean, 如果找不到的话,那就以名称装配。
1. Resource
下面介绍一个Sample:
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>
2) PersonService.java:同上。
3) PersonServiceBean.java
- 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.");
- }
- }
4) PersonDao.java:同上
5) PersonDaoService.java:同上
6) 测试类SpringIOCTest.java:同上