Spring IOC 依赖注入的两种方式XML和注解
依赖注入的原理
依赖注入的方式---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:同上
Spring IOC 依赖注入的两种方式XML和注解的更多相关文章
- Spring中依赖注入的四种方式
在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入 这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...
- spring的ioc依赖注入的三种方法(xml方式)
常见的依赖注入方法有三种:构造函数注入.set方法注入.使用P名称空间注入数据.另外说明下注入集合属性 先来说下最常用的那个注入方法吧. 一.set方法注入 顾名思义,就是在类中提供需要注入成员的 s ...
- spring的IOC——依赖注入的两种实现类型
一.构造器注入: 构造器注入,即通过构造函数完成依赖关系的设定.我们看一下spring的配置文件: <constructor-arg ref="userDao4Oracle" ...
- spring的依赖注入的四种方式,数组与集合注入;引用注入;内部bean注入
三种注入方式 第一种: 基于构造函数 hi.java (bean) package test_one; public class hi { private String name; public hi ...
- Spring的依赖注入的2种方式(1天时间)
今天花了一天的时间才调试出来 private 接口 实现类的那个bean; 最后面的那个名字不能随便的写,必须是配置文件中,实现类的那个bean 就是后面的那个名字写错了,花了整整一天 ...
- 【SSH进阶之路】Spring的IOC逐层深入——依赖注入的两种实现类型(四)
上篇博文,我们介绍了为什么使用IOC容器,和IOC的设计思想以及IOC容器的优缺点,并且给大家转载了一篇介绍IOC原理的博文,我们这篇主要给大家依赖注入的两种方式,以及他们的优缺点. 我们这篇博文还是 ...
- ASP.NET MVC中使用Unity进行依赖注入的三种方式
在ASP.NET MVC中使用Unity进行依赖注入的三种方式 2013-12-15 21:07 by 小白哥哥, 146 阅读, 0 评论, 收藏, 编辑 在ASP.NET MVC4中,为了在解开C ...
- Spring中属性注入的几种方式以及复杂属性的注入
在Spring框架中,属性的注入我们有多种方式,我们可以通过构造方法注入,可以通过set方法注入,也可以通过p名称空间注入,方式多种多样,对于复杂的数据类型比如对象.数组.List集合.map集合.P ...
- 峰Spring4学习(2)依赖注入的几种方式
一.装配一个bean 二.依赖注入的几种方式 com.cy.entity People.java: package com.cy.entity; public class People { pri ...
随机推荐
- 理解RESTful
REST全称为Representational State Transfer,可以翻译为“表现状态转换”,是由是Roy Thomas Fielding在他2000年的博士论文中提出的,目的是为了得到一 ...
- hdu 5596 GTW likes gt
题目链接: hdu 5596 题意不难懂(虽然我还是看了好久)大概就是说 n 个人排成一列,分成两组, 第 i 秒时第 i 个人会消灭掉前面比他 b[i] 值低的且和他不同组的人,c[i] 表示第 c ...
- jq变态全选vs原生变态全选
<script> $(function(){ var num=0; $("#btn").on('click',function(){ if(this.checked){ ...
- ngui中 代码调用按钮事件(后来改成了按钮绑定键盘..)
ngui中 代码调用按钮事件 好烦人啊这个问题, 我弄完发上来 这个问题解决了一半 发现可以用 按钮绑定来解决这个问题,并且更安全方便快速 直接在按钮上添加一个 key binding 指定按键 搞定 ...
- 【J-meter】变量加密之Bean shell使用
参考资料: http://www.cnblogs.com/puresoul/p/4915350.html http://www.cnblogs.com/tester-hehehe/p/5466364. ...
- [HTML/HTML5]6 使用图像
6.1 将图像作为Web页面的前景元素 使用img元素,就可以在Web页面中添加图像.img是image的缩写.只需在img元素中添加src(source的缩写)属性,并为该属性设置相应的值,就可以 ...
- pip常见操作收录
由于这些东西比较容易忘掉,在这里几下吧 1. pip 对应用进行安装: sudo pip install your_app 2. pip 对应用进行update sudo pip install ...
- vue的transition过渡效果
需要4个类: *-enter: 进入的开始状态, *-enter-active: 进入的结束状态, *-leave: 离开的开始状态, *-leave-active: 离开的结束状态 vue-rout ...
- php curl用法
curl 是使用URL语法的传送文件工具,支持FTP.FTPS.HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP.curl 支持SSL证书.HTTP POS ...
- java的前台与后台
技术上:前台是指web展示,webservice接口等输入输出接口,后台是指支持这些接口的程序. 例如读写数据库,读写文件,业务逻辑处理. 业务上来讲:前台是提供给最终用户使用的界面,后台是指管理使用 ...