经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
spring-AOP(面向切面编程)-注解方式配置
来源:cnblogs  作者:黄浩#  时间:2018/10/25 9:36:44  对本文有异议

项目结构:

 

 


 

 

增强(切面)类:

  1. package edu.nf.ch12.service.aspect;
  2. import org.aspectj.lang.JoinPoint;
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4. import org.aspectj.lang.annotation.*;
  5. import org.springframework.stereotype.Component;
  6. /**
  7. * @author wangl
  8. * @date 2018/10/24
  9. */
  10. @Aspect //这个注解标识当前的类为一个切面
  11. @Component //标识容器受管的Bean对象
  12. public class UserServiceAspect {
  13. /**
  14. * 声明一个切入点,并编写切入点表达式
  15. */
  16. @Pointcut("execution(* edu.nf.ch12.service.*.*(..))")
  17. public void pointcut(){
  18. }
  19. /**
  20. * 前置通知,指定切入点函数
  21. * 也可在注解中自定义不同的切入点表达式
  22. * @Before("execution(...)")
  23. *
  24. */
  25. @Before("pointcut()")
  26. public void before(JoinPoint joinPoint){
  27. System.out.println("前置通知..."+joinPoint.getArgs()[0]);
  28. }
  29. /**
  30. * 后置通知
  31. */
  32. @AfterReturning(value = "pointcut()", returning = "returnVal")
  33. public void afterReturn(String returnVal){
  34. System.out.println("后置通知..." + returnVal);
  35. }
  36. /**
  37. * 环绕通知
  38. */
  39. @Around("pointcut()")
  40. public Object around(ProceedingJoinPoint pjp) throws Throwable {
  41. System.out.println("环绕通知前...");
  42. Object returnVal = pjp.proceed();
  43. System.out.println("环绕通知后...");
  44. return returnVal;
  45. }
  46. /**
  47. * 异常通知
  48. * 通过pointcut指定切入点
  49. */
  50. @AfterThrowing(pointcut = "pointcut()", throwing = "e")
  51. public void throwableAdvice(Throwable e){
  52. System.out.println("异常通知..." + e.getMessage());
  53. }
  54. /**
  55. * 最终通知
  56. */
  57. @After("pointcut()")
  58. public void after(){
  59. System.out.println("最终通知...");
  60. }
  61. }

配置类AppConfig:

  1. package edu.nf.ch12.service.config;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.EnableAspectJAutoProxy;
  4. /**
  5. * @author wangl
  6. * @date 2018/10/24
  7. */
  8. @ComponentScan("edu.nf.ch12") //启用包扫描
  9. @EnableAspectJAutoProxy //启用AspectJ注解自动配置,proxyTargetClass用于指定是否强制使用cglib代理
  10. public class AppConfig {
  11. }

接口类:

  1. package edu.nf.ch12.service;
  2. /**
  3. * @author wangl
  4. * @date 2018/10/24
  5. */
  6. public interface UserService {
  7. /**
  8. * 查询用户
  9. * @param uid
  10. * @return
  11. */
  12. String getUserNameById(String uid);
  13. }

接口实现类:

  1. package edu.nf.ch12.service.impl;
  2. import edu.nf.ch12.service.UserService;
  3. import org.springframework.stereotype.Service;
  4. /**
  5. * @author wangl
  6. * @date 2018/10/24
  7. */
  8. @Service("userService")
  9. public class UserServiceImpl implements UserService {
  10. @Override
  11. public String getUserNameById(String uid) {
  12. System.out.println("查询用户..."+uid);
  13. return "user1";
  14. }
  15. }

程序测试类:

  1. package edu.nf.ch12.test;
  2. import edu.nf.ch12.service.UserService;
  3. import edu.nf.ch12.service.config.AppConfig;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. /**
  9. * @author wangl
  10. * @date 2018/10/24
  11. */
  12. public class UserServiceTest {
  13. @Test
  14. public void testGetUser(){
  15. //ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  16. ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
  17. UserService service = context.getBean("userService", UserService.class);
  18. service.getUserNameById("1001");
  19. }
  20. }

如果半注解半配置文件实现的话, new ClassPathXmlApplicationContext("applicationContext.xml");实例 然后再配置一个xml

applicationContext:

  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. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  7.  
  8. <!-- 启用包扫描 -->
  9. <context:component-scan base-package="edu.nf.ch12"/>
  10. <!-- 启用AspectJ注解自动配置,proxy-target-class是否强制使用cglib动态代理,true表示强制-->
  11. <aop:aspectj-autoproxy/>
  12.  
  13. </beans>

 

运行结果:

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号