首页 技术 正文
技术 2022年11月15日
0 收藏 378 点赞 4,398 浏览 6004 个字

依赖注入的原理
依赖注入的方式—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 (外部注入的方式)

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <beans xmlns=”http://www.springframework.org/schema/beans”
  3. xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  4. xmlns:context=”http://www.springframework.org/schema/context”
  5. xsi:schemaLocation=”http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd”>
  8. <bean id=”personDao” class=”com.spring.dao.impl.PersonDaoImpl”></bean>
  9. <bean id=”personService” class=”com.spring.service.impl.PersonServiceBean” >
  10. <property name=”personDao” ref=”personDao”/>
  11. </bean>
  12. </beans>

2) PersonDao.java

  1. package com.spring.dao;
  2. ublic interface PersonDao {
  3. public void add();

3) PersonDaoImpl.java

  1. package com.spring.dao.impl;
  2. import com.spring.dao.PersonDao;
  3. public class PersonDaoImpl implements PersonDao {
  4. public void add() {
  5. System.out.println(“PersonDao.add() is running.”);
  6. }
  7. }

4) PersonService.java

  1. package com.spring.service;
  2. public interface PersonService {
  3. public  void save();
  4. }

5) PersonServiceBean.java

  1. package com.spring.service.impl;
  2. import com.spring.dao.*;
  3. import com.spring.service.PersonService;
  4. import javax.annotation.PostConstruct;
  5. import javax.annotation.PreDestroy;
  6. public class PersonServiceBean implements PersonService {
  7. private PersonDao personDao;
  8. public PersonServiceBean(){
  9. System.out.println(“personServiceBean.constructor() is running.”);
  10. }
  11. public PersonDao getPersonDao() {
  12. return personDao;
  13. }
  14. public void setPersonDao(PersonDao personDao) {
  15. this.personDao = personDao;
  16. }
  17. public void save(){
  18. System.out.println(“Name:” );
  19. personDao.add();
  20. }
  21. @PostConstruct
  22. public void init(){
  23. System.out.println(“PersonServiceBean.init() is running.”);
  24. }
  25. @PreDestroy
  26. public void destory(){
  27. System.out.println(“PersonServiceBean.destory() is running.”);
  28. }
  29. }

6) SpringIOCTest.java(测试类)

  1. package junit.test;
  2. import org.junit.BeforeClass;
  3. import org.junit.Test;
  4. import org.springframework.context.support.AbstractApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. import com.spring.service.PersonService;
  7. public class SpringIOCTest {
  8. @BeforeClass
  9. public static void setUpBeforeClass() throws Exception {
  10. }
  11. @Test public void instanceSpring(){
  12. AbstractApplicationContext context = new ClassPathXmlApplicationContext(“beans.xml”);
  13. PersonService personService = (PersonService)context.getBean(“personService”);
  14. personService.save();
  15. //          context.close();
  16. }
  17. }

我们还可以采用内部注入的方式来处理:
Beans.xml修改如下:

  1. <bean id=”personService”>
  2. <property name=”personDao”>
  3. <bean/>
  4. </property>
  5. </bean>

2、构造器注入

以下是个例子

  1. <bean id=”personDao” class=”com.spring.dao.impl.PersonDaoImpl”></bean>
  2. <bean id=”personService”  class=”com.spring.service.impl.PersonServiceBean” init-method=”init” destroy-method=”destory”>
  3. <constructor-arg index=”0″ type=”com.spring.dao.PersonDao” ref=”personDao”/>
  4. <constructor-arg index=”1″ value=”Jamson” />
  5. </bean>

2) PersonService.java:同Setter方法注入
3) PersonServiceBean.java

  1. public class PersonServiceBean implements PersonService {
  2. private PersonDao personDao;
  3. private String name;
  4. public PersonServiceBean(){
  5. System.out.println(“personServiceBean.constructor() is running.”);
  6. }
  7. public PersonServiceBean(PersonDao personDao, String name) {
  8. this.personDao = personDao;
  9. this.name = name;
  10. }
  11. //      public PersonDao getPersonDao() {
  12. //          return personDao;
  13. //      }
  14. //     public void setPersonDao(PersonDao personDao) {
  15. //         this.personDao = personDao;
  16. //      }
  17. public void save(){
  18. System.out.println(“Name:”+name );
  19. personDao.add();
  20. }
  21. @PostConstruct
  22. public void init(){
  23. System.out.println(“PersonServiceBean.init() is running.”);
  24. }
  25. @PreDestroy
  26. public void destory(){
  27. System.out.println(“PersonServiceBean.destory() is running.”);
  28. }
  29. }

4) PersonDao.java:同Setter方法注入
5) PersonDaoImpl.java:同Setter方法注入
6) 测试类(SpringIOCTest.java):同上

控制台信息

  1. PersonServiceBean.init() is running.
  2. Name:Jamson
  3. PersonDao.add() is running.
  4. 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

  1. <context:annotation-config/>
  2. <bean id=”personDao” class=”com.spring.dao.impl.PersonDaoImpl”></bean>
  3. <bean id=”personService”  class=”com.spring.service.impl.PersonServiceBean”  init-method=”init” destroy-method=”destory”>
  4. </bean>

2) PersonService.java:同上。
3) PersonServiceBean.java

  1. package com.spring.service.impl;
  2. import com.spring.dao.*;
  3. import com.spring.service.PersonService;
  4. import javax.annotation.PostConstruct;
  5. import javax.annotation.PreDestroy;
  6. import javax.annotation.Resource;
  7. public class PersonServiceBean implements PersonService {
  8. @Resource(name=”personDao”)
  9. private PersonDao personDao;
  10. private String name;
  11. public PersonServiceBean(){
  12. System.out.println(“personServiceBean.constructor() is running.”);
  13. }
  14. public PersonServiceBean(PersonDao personDao, String name) {
  15. this.personDao = personDao;
  16. this.name = name;
  17. }
  18. //      public PersonDao getPersonDao() {
  19. //          return personDao;
  20. //      }
  21. //     public void setPersonDao(PersonDao personDao) {
  22. //         this.personDao = personDao;
  23. //      }
  24. public void save(){
  25. System.out.println(“Name:”+name );
  26. personDao.add();
  27. }
  28. @PostConstruct
  29. public void init(){
  30. System.out.println(“PersonServiceBean.init() is running.”);
  31. }
  32. @PreDestroy
  33. public void destory(){
  34. System.out.println(“PersonServiceBean.destory() is running.”);
  35. }
  36. }

4) PersonDao.java:同上
5) PersonDaoService.java:同上
6) 测试类SpringIOCTest.java:同上

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,966
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,487
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,332
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,115
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,748
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,783