博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring学习笔记5——注解方式AOP
阅读量:4310 次
发布时间:2019-06-06

本文共 2022 字,大约阅读时间需要 6 分钟。

 

第一步:注解配置业务类

  使用@Component("Pservice")注解ProductService 类

1 package com.spring.service; 2  3 import org.springframework.stereotype.Component; 4  5 @Component("Pservice") 6 public class ProductService { 7     public void doSomeService() { 8         System.out.println("doSomeService"); 9     }10 }

第二步:注解配置切面

@Aspect 注解表示这是一个切面

@Component 表示这是一个bean,由Spring进行管理
@Around(value = "execution(* com.spring.service.ProductService.*(..))") 表示对com.spring.service.ProductService 这个类中的所有方法进行切面操作

1 package com.spring.aspect; 2  3 import org.aspectj.lang.ProceedingJoinPoint; 4 import org.aspectj.lang.annotation.Around; 5 import org.aspectj.lang.annotation.Aspect; 6 import org.springframework.stereotype.Component; 7  8 @Aspect 9 @Component10 public class LoggerAspect {11     @Around(value = "execution(* com.spring.service.ProductService.*(..))")12     public Object log(ProceedingJoinPoint joinPoint) throws Throwable {13         System.out.println("start log:" + joinPoint.getSignature().getName());14         Object object = joinPoint.proceed();15         System.out.println("end log:" + joinPoint.getSignature().getName());16         return object;17     }18 }

第三步:修改applicationContext.xml

去掉原有信息,添加如下3行

  <1>扫描包com.spring.aspect和com.spring.service,定位业务类和切面类

1 
2

  <2>找到被注解了的切面类,进行切面配置

1 

 

applicationContext.xml

1 
2
16
17
18
19

第四步:测试

1 package com.spring.test; 2  3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5  6 import com.spring.service.ProductService; 7  8 public class TestSpring { 9 10     public static void main(String[] args) {11         // TODO Auto-generated method stub12         ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });13         ProductService s = (ProductService) context.getBean("Pservice");14         s.doSomeService();15     }16 17 }

 

转载于:https://www.cnblogs.com/lyj-gyq/p/8835242.html

你可能感兴趣的文章
关于yum Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx.
查看>>
2020-11-18
查看>>
Docker面试题(二)
查看>>
一、redis面试题及答案
查看>>
消息队列2
查看>>
C++ 线程同步之临界区CRITICAL_SECTION
查看>>
测试—自定义消息处理
查看>>
MFC中关于虚函数的一些问题
查看>>
根据图层名获取图层和图层序号
查看>>
规范性附录 属性值代码
查看>>
提取面狭长角
查看>>
Arcsde表空间自动增长
查看>>
Arcsde报ora-29861: 域索引标记为loading/failed/unusable错误
查看>>
记一次断电恢复ORA-01033错误
查看>>
C#修改JPG图片EXIF信息中的GPS信息
查看>>
从零开始的Docker ELK+Filebeat 6.4.0日志管理
查看>>
How it works(1) winston3源码阅读(A)
查看>>
How it works(2) autocannon源码阅读(A)
查看>>
How it works(3) Tilestrata源码阅读(A)
查看>>
JDK下载(百度网盘)
查看>>