一、Spring 註解&XML 實現 AOP
第一步:新建持久層(dao)接口以及實現類
package cn.lemon.dao;
public interface IAccountDao {
void in(Float money);
void out(Float money);
}
package cn.lemon.dao.impl;
import cn.lemon.dao.IAccountDao;
import org.springframework.stereotype.Repository;
@Repository
public class AccountDaoImpl implements IAccountDao {
@Override
public void in(Float money) {
System.out.println("成功轉入" + money + "元錢");
}
@Override
public void out(Float money) {
System.out.println("成功轉出" + money + "元錢");
}
}
第二步:新建業務層(service)接口以及實現類
package cn.lemon.service;
public interface IAccountServcie {
void transfer(Float money);
}
package cn.lemon.service.impl;
import cn.lemon.dao.IAccountDao;
import cn.lemon.service.IAccountServcie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AccountServiceImpl implements IAccountServcie {
@Autowired
private IAccountDao iAccountDao;
@Override
public void transfer(Float money) {
iAccountDao.in(money);
iAccountDao.out(money);
}
}
第三步:新建配置文件 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:context="http://www.springframework.org/schema/context"
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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 告知 spring,在創建容器時要掃描的包 -->
<context:component-scan base-package="cn.lemon"></context:component-scan>
<!--開啓 Spring 註解對 AOP 的支持
# proxy-target-class="true" 使用 CGLib 代理
-->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
</beans>
第四步:新建註解
package cn.lemon.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @Target說明了Annotation所修飾的對象範圍:Annotation可被用於 packages、types(類、接口、枚舉、Annotation類型)、
* 類型成員(方法、構造方法、成員變量、枚舉值)、方法參數和本地變量(如循環變量、catch參數)
* 取值(ElementType.METHOD 用於描述方法
* RetentionPolicy.RUNTIME:註解不僅被保存到class文件中,jvm加載class文件之後,仍然存在;
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotaiton {
}
第五步:新建增強類 LoggerAdvice.java
第一種寫法
package cn.lemon.advice;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect//表明當前類是一個切面類(或者說是一個增強類)
public class LoggerAdvice {
@Before("execution(* cn.lemon.service.impl.*.*(..))")//切入點表達式
public void before(JoinPoint joinPoint) {
System.out.println("前置增強,方法爲:" + joinPoint.getSignature().getName());
}
@AfterReturning(value = "execution(* cn.lemon.service.impl.*.*(..))", returning = "result")//切入點表達式
public void afterReturning(JoinPoint joinPoint, Object result) {
System.out.println("後置增強,方法爲:" + joinPoint.getSignature().getName() + "返回值:" + result);
}
@Around("execution(* cn.lemon.service.impl.*.*(..))")//切入點表達式
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("環繞前置增強.......");
Object result = proceedingJoinPoint.proceed();//執行業務方法
System.out.println("環繞後置增強。。。。。");
return result;
}
@AfterThrowing(value = "execution(* cn.lemon.service.impl.*.*(..))", throwing = "e")//切入點表達式
public void afterTrowing(JoinPoint joinPoint, Exception e) {
System.out.println("異常增強,方法名:" + joinPoint.getSignature().getName() + ",異常信息爲:" + e.getMessage());
}
@After("execution(* cn.lemon.service.impl.*.*(..))")//切入點表達式
public void after(JoinPoint joinPoint) {
System.out.println("最終增強,方法名:" + joinPoint.getSignature().getName());
}
}
第二種寫法
package cn.lemon.advice;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect//表明當前類是一個切面類(或者說是一個增強類)
public class LoggerAdvice {
/**
* 由於 "execution(* cn.lemon.service.impl.*.*(..))" 切入點表達式在下面多次出現,可以定義一個方法
*/
@Pointcut("execution(* cn.lemon.service.impl.*.*(..))")
private void myPointCut() {
}
@Before("myPointCut()")//切入點表達式
public void before(JoinPoint joinPoint) {
System.out.println("前置增強,方法爲:" + joinPoint.getSignature().getName());
}
@AfterReturning(value = "myPointCut()", returning = "result")//切入點表達式
public void afterReturning(JoinPoint joinPoint, Object result) {
System.out.println("後置增強,方法爲:" + joinPoint.getSignature().getName() + "返回值:" + result);
}
@Around("myPointCut()")//切入點表達式
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("環繞前置增強.......");
Object result = proceedingJoinPoint.proceed();//執行業務方法
System.out.println("環繞後置增強。。。。。");
return result;
}
@AfterThrowing(value = "myPointCut()", throwing = "e")//切入點表達式
public void afterTrowing(JoinPoint joinPoint, Exception e) {
System.out.println("異常增強,方法名:" + joinPoint.getSignature().getName() + ",異常信息爲:" + e.getMessage());
}
@After("myPointCut()")//切入點表達式
public void after(JoinPoint joinPoint) {
System.out.println("最終增強,方法名:" + joinPoint.getSignature().getName());
}
}
第六步:新建測試類
package cn.lemon.service.impl;
import cn.lemon.service.IAccountServcie;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/applicationContext.xml")
public class AccountServiceImplTest {
@Autowired
private IAccountServcie iAccountServcie;
@Test
public void transfer() {
iAccountServcie.transfer(100f);
}
}
二、Spring 純註解實現 AOP(修改上面的代碼)
第一步:刪掉配置文件 applicationContext.xml,新建配置類 SpringConfiguration.java
package cn.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration//表示是一個配置類
@ComponentScan("cn.lemon")//搜索有註解的包
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class SpringConfiguration {
}
第二步:修改測試類
替換註解@ContextConfiguration(locations = "classpath:/applicationContext.xml")
修改爲:@ContextConfiguration(classes = SpringConfiguration.class)