1、開啓註解模式
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
2、註解切面
在通知的方法上面加上一個註解@Aspect
@Component("myAdvice")
/*切面*/
@Aspect
public class MyAdvice {
3、通知上面加上切點
五種:
@Before(表達式) @After(表達式) @AfterReturning(表達式) @Around(表達式) @AfterThrowing(表達式)
4、書寫表達式的兩種方式
(1).直接寫
@Before("execution(*cn.hd.springProxyAnnotation.impl..*ServiceImpl.*(..))")
@AfterReturning("execution(*cn.hd.springProxyAnnotation.impl..*ServiceImpl.*(..))")
@Around("execution(*cn.hd.springProxyAnnotation.impl..*ServiceImpl.*(..))")
@After("execution(*cn.hd.springProxyAnnotation.impl..*ServiceImpl.*(..))")
@AfterThrowing("execution(*cn.hd.springProxyAnnotation.impl..*ServiceImpl.*(..))")
(2).配置一個切點,調用類的方法獲得切點
@Pointcut("execution(*cn.hd.springProxyAnnotation.impl..*ServiceImpl.*(..))")
public void pc(){
}
@Before("MyAdvice.pc()")
@AfterReturning("MyAdvice.pc()")
……
具體代碼奉上:
MyAdvice:
package cn.hd.springProxyAnnotation; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; @Component("myAdvice") /*切面*/ @Aspect public class MyAdvice { @Pointcut("execution(* cn.hd.springProxyAnnotation.impl..*ServiceImpl.*(..))") public void pc(){ } /*@Before("execution(* cn.hd.springProxyAnnotation.impl..*ServiceImpl.*(..))")*/ @Before("MyAdvice.pc()") public void before(){ System.out.println("在目標方法前調用。"); } /*@AfterReturning("execution(* cn.hd.springProxyAnnotation.impl..*ServiceImpl.*(..))")*/ @AfterReturning("MyAdvice.pc()") public void afterReturning(){ System.out.println("如果目標對象方法沒有出現異常,就會在該方法調用後調用。"); } @Around("execution(* cn.hd.springProxyAnnotation.impl..*ServiceImpl.*(..))") public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("環繞前在目標方法前都調用。"); Object proceed = pjp.proceed(); System.out.println("環繞後在目標方法後都調用。"); return proceed; } @After("execution(* cn.hd.springProxyAnnotation.impl..*ServiceImpl.*(..))") public void after(){ System.out.println("目標方法前後,不管有沒有異常都調用。"); } @AfterThrowing("execution(* cn.hd.springProxyAnnotation.impl..*ServiceImpl.*(..))") public void throwException(){ System.out.println("目標方法出現異常調用該方法。"); }
Demo測試類:
package cn.hd.springProxyAnnotation; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:cn/hd/springProxyAnnotation/applicationContext.xml") public class Demo { /*對應配置bean類的名字*/ @Resource(name = "userService") private UserService userService; @Test public void fun1(){ userService.add(); } }
UserServiceImpl:
package cn.hd.springProxyAnnotation.impl; import cn.hd.springProxyAnnotation.UserService; import org.springframework.stereotype.Service; @Service("userService") public class UserServiceImpl implements UserService { @Override public void add() { // System.out.println("開啓事務"); System.out.println("添加用戶"); // System.out.println("提交用戶"); } @Override public void delete() { // System.out.println("開啓事務"); System.out.println("刪除用戶"); // System.out.println("提交事務"); } @Override public void update() { // System.out.println("開啓事務"); System.out.println("更新用戶"); // System.out.println("提交事務"); } @Override public void find() { // System.out.println("開啓事務"); System.out.println("查詢用戶"); // 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" xmlns:context="http://www.springframework.org/schema/context" 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"> <!--開啓註解方式--> <context:component-scan base-package="cn.hd.springProxyAnnotation"></context:component-scan> <!--開啓註解模式--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>