[关闭]
@yudesong 2018-02-16T14:28:36.000000Z 字数 3282 阅读 593

Spring AOP

AOP


Aspect Oriented Programming 面向方面编程戒面向切面编程
AOP 关注点是共同处理,可以通过配置将其作用到某一个戒多个目标对象上。好处是实现组件重复
利用,改善程序结构,提高灵活性。将共通组件不目标对象解耦。

AOP 相关概念

导入jar包

编写spring的配置文件applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/util
  7. http://www.springframework.org/schema/util/spring-util-4.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop.xsd
  12. "
  13. xmlns:util="http://www.springframework.org/schema/util"
  14. xmlns:p="http://www.springframework.org/schema/p"
  15. xmlns:context="http://www.springframework.org/schema/context"
  16. xmlns:aop="http://www.springframework.org/schema/aop"
  17. >
  18. <!-- configure the package for spring to scan -->
  19. <context:component-scan base-package="test.Spring.AOP" />
  20. <!-- make the aspectj annotation to be used -->
  21. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  22. </beans>

HelloWordImpl

  1. public interface HelloWord {
  2. public int sayHello(int num);
  3. }
  4. @Component
  5. public class HelloWordImpl implements HelloWord{
  6. public int sayHello(int num){
  7. System.out.println("hello word");
  8. return 100/num;
  9. }
  10. }

SpringAOP注释的类型

  1. @Component
  2. @Aspect
  3. public class HelloWordAspect {
  4. @Before(value="execution(* test.Spring.AOP.HelloWord.sayHello(..))")
  5. public void beforeMethod(JoinPoint jp){
  6. String methodName = jp.getSignature().getName();
  7. System.out.println(methodName);
  8. System.out.println("before method execute,args are "+Arrays.toString(jp.getArgs()));
  9. }
  10. @After("execution(* test.Spring.AOP.HelloWord.sayHello(..))")
  11. public void afterMethod(JoinPoint jp){
  12. System.out.println("after method execute,args are "+Arrays.toString(jp.getArgs()));
  13. }
  14. @AfterThrowing(value="execution(* test.Spring.AOP.HelloWord.sayHello(..))",throwing="ex")
  15. public void afterThrow(Exception ex){
  16. System.out.println("afterThrow"+ex.getMessage());
  17. }
  18. @AfterReturning(value="execution(* test.Spring.AOP.HelloWord.sayHello(..))",returning="result")
  19. public void afterReturn(Object result){
  20. System.out.println("the result is "+result);
  21. }
  22. }

主函数

  1. public class Main {
  2. public static void main(String[] args) {
  3. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextForAOP.xml");
  4. HelloWord hw = (HelloWord) context.getBean("helloWordImpl");
  5. hw.sayHello(10);
  6. }
  7. }

使用Around环绕通知切面类实现类似效果

  1. @Component
  2. @Aspect
  3. public class HelloWordAspectAround {
  4. @Around(value="execution(* test.Spring.AOP.HelloWord.sayHello(..)))")
  5. public Object aroundMethod(ProceedingJoinPoint pjp){
  6. Object result = null;
  7. String methodName = pjp.getSignature().getName();
  8. try {
  9. result = pjp.proceed();
  10. System.out.println("the result is "+result);
  11. } catch (Throwable e) {
  12. System.out.println("Exception occurs : "+e.getMessage());
  13. throw new RuntimeException(e);
  14. }
  15. System.out.println(methodName+" end");
  16. return result;
  17. }
  18. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注