spring AOP的学习
@autor:韩克 2019-07-19 21:09:44
方式一:使用bean生成Proxy的方式(jdk动态代理方式)
1. 创建目标类接口
使用bean生成Proxy的方式底层使用的是jdk动态代理,所以需要创建接口。
public interface EmployeeService {
public void dosome();
}
2.创建目标类,实现相应的接口
public class EmployeeServiceImpl implements EmployeeService{
@Override
public void dosome() {
System.out.println("员工工作中....");
}
@Override
public void playGame() {
System.out.println("趁老板不在,玩会游戏...");
}
}
3.创建通知类,实现相应的接口,在重写方法中写相应的通知。
前置通知:implements MethodBeforeAdvice
package com.xt.advice;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class EmpBeforeAdvice implements MethodBeforeAdvice{
@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("记录员工行为....");
}
}
后置通知:implements AfterReturningAdvice
package com.xt.advice;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class EmpAfterAdvice implements AfterReturningAdvice{
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.println("摄像头已记录用户行为...");
}
}
4.配置applicationContext.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="empAfterAdvice" class="com.xt.advice.EmpAfterAdvice"></bean>
<bean id="empBeforeAdvice" class="com.xt.advice.EmpBeforeAdvice"></bean>
<bean id="employeeServiceImpl" class="com.xt.service.impl.EmployeeServiceImpl"></bean>
<bean id="regExpAfterAdvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="patterns">
<list>
<value>.*dosome.*</value>
</list>
</property>
<property name="advice" ref="empAfterAdvice"></property>
</bean>
<bean id="regExpBeforeAdvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="patterns" value=".*dosome.*"></property>
<property name="advice" ref="empBeforeAdvice"></property>
</bean>
<bean id="empProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="employeeServiceImpl"></property>
<property name="interfaces" value="com.xt.service.EmployeeService"></property>
<property name="interceptorNames">
<list>
<value>regExpAfterAdvice</value>
<value>regExpBeforeAdvice</value>
</list>
</property>
</bean>
</beans>
方式二:AspectJ注解方式(通过cglib动态代理实现)
一、创建目标对象,在目标对象中不需要使用任何注解。
package com.xt.pojo;
public class Tauren {
public void buyHP(){
System.out.println("战士买HP");
}
public void buyMP() {
System.out.println("战士买MP");
}
public void combat() {
System.out.println("斗牛士在战斗");
}
}
二、创建切面
package com.xt.advice;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
/* @Aspect:用来声明切面,写在通知类 */
@Aspect
public class Shop {
//前置增强
/* 注意括号的匹配,execution不要写错! */
@Before("execution(* com.xt.pojo.Tauren.buy*(..))")
public void buybefore(){
System.out.println("玩家进入商店!");
}
//后置增强
@After("execution(* com.xt.pojo.Tauren.buy*(..))")
public void buyAfter(){
System.out.println("药水添加到背包!");
}
}
三、在applicationContext.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册目标类 -->
<bean id="tauren" class="com.xt.pojo.Tauren"></bean>
<!-- 注册切面 -->
<bean id="shop" class="com.xt.advice.Shop"></bean>
<!-- 注册自动代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
四、测试
package com.xt.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xt.pojo.Tauren;
public class Test {
@org.junit.Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Tauren tauren = (Tauren) applicationContext.getBean("tauren",Tauren.class);//注意使用目标类
tauren.buyHP();
tauren.buyMP();
tauren.combat();
}
}