合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
**1.配置相关-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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--指定包和该包下的子包,扫描注解--> <context:component-scan base-package="com.nobb"/> <!--开启注解配置切面开关--> <aop:aspectj-autoproxy/> </beans> ~~~ **2.使用注解注册通知对象-com.nobb.advice.MyAdvice** ~~~ package com.nobb.advice; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; /** * 通知 */ //交给spring管理 并命名为myAdvice //<bean name="myAdvice" class="com.nobb.advice.MyAdvice"></bean> @Component("myAdvice") //声明在该类中配置切面 @Aspect public class MyAdvice { //声明切点 @Pointcut("execution(* com.nobb.service.*ServiceImpl.*(..))") public void myPc(){ } //切点+前置通知 @Before("myPc()") public void before(){ System.out.println("前置通知"); } //切点+后置通知 @AfterReturning("myPc()") public void after(){ System.out.println("后置通知"); } //切点+异常通知 @AfterThrowing("myPc()") public void afterThrowing(){ System.out.println("异常通知"); } //切点+最终通知 @After("myPc()") public void end(){ System.out.println("最终通知"); } //环绕通知, @Around("execution(* com.nobb.service.*ServiceImpl.*(..))") public Object around(ProceedingJoinPoint pjp){ Object o = null; try{ System.out.println("环绕通知-前置"); //执行业务代码 o = pjp.proceed(); System.out.println("环绕通知-后置"); }catch(Throwable throwable){ throwable.printStackTrace(); System.out.println("环绕通知-异常"); }finally{ System.out.println("环绕通知-最终"); } return o; } } ~~~ **3.UserServiceImpl** ~~~ package com.nobb.service; import org.springframework.stereotype.Service; //将UserServiceImpl 交给spring管理 并命名为userService @Service("userService") public class UserServiceImpl implements UserService { @Override public void save() { System.out.println("save()"); } @Override public void delete() { System.out.println("delete()"); } @Override public void update() { System.out.println("update()"); } @Override public void find() { System.out.println("find()"); } } ~~~ **4.测试代码** ~~~ import com.nobb.service.UserService; 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:applicationContext3.xml") public class AopTest { @Resource(name="userService") private UserService userService; @Test public void fun1(){ userService.save(); } } ~~~