spring 解读之 DI 、AOP

1.依赖注入

使用依赖注入,对象的依赖都是在对象创建时由负责协调应用的各个对象的外部实体提供的。这就是依赖被注入到对象中。DI意味着关于对象如何获得他的协作对象的责任被反转了。

DI的关键有点是松解耦合。如果一个对象通过接口只知道其依赖对象,那么该依赖将使用一种不同的实现方式来交互,其依赖对象并不知道这种变化。

简而言之:依赖注入就是  协调依赖对象之间合作的责任从对象自身中转移出来

1.1 Spring依赖注入(DI)的三种方式,分别为:

1.  接口注入

2.  Setter方法注入

3.  构造方法注入


Type1 接口注入
我们常常借助接口来将调用者与实现者分离。如:
public class ClassA   
{
     private InterfaceB clzB;  

     public init()   
    {
      Ojbect obj =Class.forName(Config.BImplementation).newInstance();  
      clzB = (InterfaceB)obj;
    }  
} 

上面的代码中,ClassA依赖于InterfaceB的实现,如何获得InterfaceB实现类的实例?传统的方法是在代码中创建InterfaceB实现类的实例,并将起赋予clzB.

而这样一来,ClassA在编译期即依赖于InterfaceB的实现。为了将调用者与实现者在编译期分离,于是有了上面的代码,我们根据预先在配置文件中设定的实现类的类名,动态加载实现类,并通过InterfaceB强制转型后为ClassA所用。


Type2构造子注入:
首先,我们写一个PersonDao.java接口:
public interface PersonDao {
	public void add();
}
并对这个接口进行实现:
public class PersonDaoBean implements PersonDao{

	@Override
	public void add() {
		System.out.println("我是PersonDaoBean中的add()方法");
		
	}

}

然后,我们写一个PersonService.java接口:
public interface PersonService {
	public  void save();
}
并对这个接口进行实现:

public class PersonServiceBean implements PersonService {
	private PersonDao personDao;
	private String name;
	
	public PersonServiceBean(){}
	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 String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void save(){
		personDao.add();
		System.out.println(name);
		//personDao.add();
	}
}

可以看到,在这个实现类中,我们引入了一个PersonDao类型的属性和一个String类型的属性,并且建造了一个包含这两个参数的构造函数和setter、getter方法。
然后,我们在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
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">   
   	
   	<bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
   	<bean name="personService" class="cn.itcast.service.impl.PersonServiceBean">
   		<constructor-arg  index="0" type="cn.itcast.dao.PersonDao" ref="personDao"></constructor-arg>
   		<constructor-arg index="1" value="itcast" />
   	</bean>
</beans>
可以看到,我们配置了两个bean:
第一个bean是依赖bean,我们在PersonServiceBean.java类中需要依赖这个bean指向的类

第二个bean是我们要用到的bean,然后在这个bean中我们配置了两个<constructor-arg>标签:其中index表明索引,就是构造函数中参数的索引,0代表第一个参数;type表明这个参数的类型,ref表明需要依赖于哪个类。第二个中没有type和ref,是因为String类型不需要指定type,另外也不是依赖类,所以也不需要ref属性了。


Type3  setter设值注入

首先,我们写一个PersonService.java接口
public interface PersonService {
	public abstract void save();
}

然后,我们为这个接口写一个实现类:PersonServiceBean.java
public class PersonServiceBean implements PersonService {

	private String name;

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void save(){
		System.out.println(name);
		//personDao.add();
	}
}
我们编辑beans.xml文件,配置IOC反转容器的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
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">   
   	
   	<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
   		<property name="name" value="itcast" />	
   	</bean>
	
</beans>
原理:通过beans.xml我们配置了IOC反转容器,配置的属性通过setter方法进行注入。如果没有setter方法会报错。

依赖注入只是spring提供的一种技术,它支持松散耦合。面向切面编程技术提供了另外一种解耦和的功能,他将功能和对象分开。

2.面向切面编程


AOP帮助我们将服务模块化,确保了POJO简单。使用AOP可以使用各种功能层来覆盖核心业务层。
面向切面编程(也叫面向方面),可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术。
面向切面编程:就是在原直线编程的某处咔嚓一下,加点动西,而且不影响原来的代码。可以用在日志,加权限,事物,异常管理等方面。

小结:
DI是联合对象的一种方法,例如对象无需知道依赖来自何处或者依赖如何实现。不同于机子获得依赖对象,被依赖的对象被赋予他们所依赖的类。
spring装配bean时,这些切面将在运行中被编制,这样就可以非常有效的给予bean新的行为。
参考:http://blog.csdn.net/xn4545945/article/details/8241206

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值