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>