spring AOP的學(xué)習(xí)
@autor:韓克?2019-07-19 21:09:44
方式一:使用bean生成Proxy的方式(jdk動(dòng)態(tài)代理方式)
1. 創(chuàng)建目標(biāo)類接口
使用bean生成Proxy的方式底層使用的是jdk動(dòng)態(tài)代理力麸,所以需要?jiǎng)?chuàng)建接口至壤。
public interface EmployeeService {
public void dosome();
}
2.創(chuàng)建目標(biāo)類利诺,實(shí)現(xiàn)相應(yīng)的接口
public class EmployeeServiceImpl implements EmployeeService{
@Override
public void dosome() {
System.out.println("員工工作中....");
}
@Override
public void playGame() {
System.out.println("趁老板不在翁脆,玩會(huì)游戲...");
}
}
3.創(chuàng)建通知類,實(shí)現(xiàn)相應(yīng)的接口,在重寫(xiě)方法中寫(xiě)相應(yīng)的通知尘执。
前置通知: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注解方式(通過(guò)cglib動(dòng)態(tài)代理實(shí)現(xiàn))
一、創(chuàng)建目標(biāo)對(duì)象,在目標(biāo)對(duì)象中不需要使用任何注解宴凉。
package com.xt.pojo;
public class Tauren {
public void buyHP(){
System.out.println("戰(zhàn)士買HP");
}
public void buyMP() {
System.out.println("戰(zhàn)士買MP");
}
public void combat() {
System.out.println("斗牛士在戰(zhàn)斗");
}
}
二誊锭、創(chuàng)建切面
package com.xt.advice;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
/* @Aspect:用來(lái)聲明切面,寫(xiě)在通知類 */
@Aspect
public class Shop {
//前置增強(qiáng)
/* 注意括號(hào)的匹配弥锄,execution不要寫(xiě)錯(cuò)丧靡! */
@Before("execution(* com.xt.pojo.Tauren.buy*(..))")
public void buybefore(){
System.out.println("玩家進(jìn)入商店!");
}
//后置增強(qiáng)
@After("execution(* com.xt.pojo.Tauren.buy*(..))")
public void buyAfter(){
System.out.println("藥水添加到背包籽暇!");
}
}
三温治、在applicationContext.xml中注冊(cè)目標(biāo)和切面
<?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">
<!-- 注冊(cè)目標(biāo)類 -->
<bean id="tauren" class="com.xt.pojo.Tauren"></bean>
<!-- 注冊(cè)切面 -->
<bean id="shop" class="com.xt.advice.Shop"></bean>
<!-- 注冊(cè)自動(dòng)代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
四、測(cè)試
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);//注意使用目標(biāo)類
tauren.buyHP();
tauren.buyMP();
tauren.combat();
}
}