Spring中的AOP的学习,从两个角度:1、Spring的传统AOP(不作重点)2、Spring的AspectJ(重点)
一、Spring中的传统AOP
AOP:不是由Spring定义,而是由AOP联盟来定义。AOP联盟为通知Advice定义了org.aopalliance.Interface.Advice。
Spring按照通知Advice在目标方法的连接点位置,可以分为五类
前置通知:org.springframework.aop.MethodBeforeAdvice,在目标方法执行之前实施增强
后置通知:org.springframework.aop.AfterReturningAdvice,在目标方法执行之后实施增强
环绕通知:org.aopalliance.intercept.MethodInterceptor,在目标方法执行前后实施增强
异常抛出通知:org.springframework.aop.ThrowsAdvice,在方法抛出异常之后实施增强
引介通知:org.springframework.aop.IntroductionInterceptor,在目标类中添加一些新的方法和属性(类级别的增强,本次专题不涉及)
Spring中切面的类型:
Advisor:Spring中的传统切面。
Aspect:都是有一个切点和一个通知的组合
Advisor:多个切点和多个通知的组合
lAdvisor : 代表一般切面,Advice本身就是一个切面,对目标类所有方法进行拦截(*不带有切点的切面)
lPointcutAdvisor : 代表具有切点的切面,可以指定拦截目标类哪些方法带有切点的切面,针对某个方法进行拦截
package com.js.aopStu;
public interface CustomerDao {
public void add();
public void delete();
public void update();
public void find();
}
package com.js.aopStu;
public class CustomerImpl implements CustomerDao {
@Override
public void add() {
System.out.println("添加客户...");
}
@Override
public void delete() {
System.out.println("删除客户...");
}
@Override
public void update() {
System.out.println("修改客户...");
}
@Override
public void find() {
System.out.println("查询客户...");
}
}
package com.js.aopStu;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
/**
* 前置增强
* 实现指定接口
* @author JiangShuai
*
*/
public class MyBeforeAdvice implements MethodBeforeAdvice{
/**
* method:执行的方法
* args:参数
* target:目标对象
*/
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("前置增强===");
}
}
package com.js.aopStu;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDemo1 {
@Autowired
@Qualifier("customerDao")
private CustomerDao customerDao;
@Test
public void Demo1(){
customerDao.add();
customerDao.delete();
customerDao.find();
customerDao.update();
}
}
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- bean definitions here -->
<!-- 定义目标对象 -->
<bean id="customerDao" class="com.js.aopStu.CustomerImpl"></bean>
<!-- 定义增强 -->
<bean id="beforeAdice" class="com.js.aopStu.MyBeforeAdvice"></bean>
<!-- 生成代理 -->
</beans>
<!-- Spring支持配置来生成代理 -->
<bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name=""></property>
</bean>
<list>
<value></value>
....
</list>
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- bean definitions here -->
<!-- 定义目标对象 -->
<bean id="customerDao" class="com.js.aopStu.CustomerImpl"></bean>
<!-- 定义增强 -->
<bean id="beforeAdice" class="com.js.aopStu.MyBeforeAdvice"></bean>
<!-- Spring支持配置来生成代理 -->
<bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 设置目标对象 -->
<property name="target" ref="customerDao"></property>
<!-- 设置实现的接口,value中写接口的全路径 -->
<property name="proxyInterfaces" value="com.js.aopStu.CustomerDao"></property>
<!-- 配置需要拦截的,一定是value,此处对customerDao中的所有方法拦截 -->
<property name="interceptorNames" value="beforeAdice"></property>
</bean>
</beans>
这时,进行测试应该应用我们的代理对象: