在 Spring 中使用注解方式( annotation )实现 AOP

〇、介绍:Spring  使用 注解(annotation) 方式实现 aop 

 

一、文件格式(不知道为什么 pom 会报错,运行没问题)

二、java代码

 1 package spr.crm.aspect;
 2 
 3 import org.aspectj.lang.ProceedingJoinPoint;
 4 import org.aspectj.lang.annotation.After;
 5 import org.aspectj.lang.annotation.AfterReturning;
 6 import org.aspectj.lang.annotation.AfterThrowing;
 7 import org.aspectj.lang.annotation.Around;
 8 import org.aspectj.lang.annotation.Aspect;
 9 import org.aspectj.lang.annotation.Before;
10 import org.aspectj.lang.annotation.Pointcut;
11 import org.springframework.stereotype.Component;
12 /**
13  * 切面 解耦
14  * @author YEYILING
15  */
16 @Component
17 @Aspect
18 public class CrmAspect {
19     @Pointcut("execution (* spr.crm.service.impl.CrmServiceImpl.deleteEmp(..))")
20     public void befoeAdvice() {
21     }
22 
23     @Pointcut("execution (* spr.crm.service.impl.CrmServiceImpl.deleteEmp(..))")
24     public void afterReturningAdvice() {
25     }
26 
27     @Pointcut("execution (* spr.crm.service.impl.CrmServiceImpl.deleteEmp(..))")
28     public void afterAdvice() {
29     }
30 
31     @Pointcut("execution (* spr.crm.service.impl.CrmServiceImpl.selectEmp(..))")
32     public void aroundAdvice() {
33     }
34 
35     @Pointcut("execution (* spr.crm.service.impl.CrmServiceImpl.deleteEmp(..))")
36     public void exceptionAdvice() {
37     }
38 
39     // 前置
40     @Before("befoeAdvice()")
41     public void validate() {// 横切关注点
42         System.out.println("@Before......");
43     }
44 
45     // 后置
46     @AfterReturning("afterReturningAdvice()")
47     public void log() {// 横切关注点
48         System.out.println("@AfterReturning......");
49     }
50 
51     // 环绕(该方法有格式要求)
52     @Around("aroundAdvice()")
53     public Object myAround(ProceedingJoinPoint pjp) throws Throwable {
54         System.out.println("@Around");
55         Object obj = pjp.proceed();
56         System.out.println("@Around");
57         return obj;
58     }
59 
60     // 异常
61     @AfterThrowing("exceptionAdvice()")
62     public void myException() {
63         System.out.println("@AfterThrowing......");
64     }
65 
66     // 最终
67     @After("afterAdvice()")
68     public void myAfter() {
69         System.out.println("@After......");
70     }
71 }
CrmAspect
 1 package spr.crm.dao.impl;
 2 
 3 import org.springframework.stereotype.Repository;
 4 
 5 import spr.crm.dao.inter.CrmDaoInter;
 6 /**
 7  * 
 8  * @author YEYILING
 9  *
10  */
11 @Repository("crmDaoImpl")
12 public class CrmDaoImpl implements CrmDaoInter {
13 
14     public void insertEmp() {
15         System.out.println("dao-insertEmp");
16     }
17 
18     /** 删除之前要做一个权限的验证,删除之后要做一个日志记录 */
19     public void deleteEmp() {
20         System.out.println("dao-deleteEmp");
21     }
22 
23     public void updateEmp() {
24         System.out.println("dao-updateEmp");
25     }
26 
27     public void selectEmp() {
28         System.out.println("dao-selectEmp");
29     }
30 
31 }
CrmDaoImpl
 1 package spr.crm.dao.inter;
 2 
 3 /**
 4  * dao接口,定义增删改查功能
 5  */
 6 public interface CrmDaoInter {
 7     public void insertEmp();
 8 
 9     public void deleteEmp();
10 
11     public void updateEmp();
12 
13     public void selectEmp();
14 }
CrmDaoInter
 1 package spr.crm.service.impl;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5 
 6 import spr.crm.dao.inter.CrmDaoInter;
 7 import spr.crm.service.inter.CrmServiceInter;
 8 
 9 /**
10  * 监控对象
11  * @author YEYILING
12  *
13  */
14 @Service("crmServiceImpl")
15 public class CrmServiceImpl implements CrmServiceInter {
16     @Autowired
17     public CrmDaoInter dao;
18 
19     public void insertEmp() {
20         System.out.println("service-insertEmp");
21     }
22 
23     public void deleteEmp() {
24         System.out.println("service-deleteEmp");
25         System.out.println(10/0);
26     }
27 
28     public void updateEmp() {
29         System.out.println("service-updateEmp");
30     }
31 
32     public void selectEmp() {
33         System.out.println("service-selectEmp");
34     }
35 
36     public CrmDaoInter getDao() {
37         return dao;
38     }
39 
40     public void setDao(CrmDaoInter dao) {
41         this.dao = dao;
42     }
43 
44 }
CrmServiceImpl
 1 package spr.crm.service.inter;
 2 
 3 /**
 4  * 业务层接口
 5  * @author Administrator
 6  */
 7 public interface CrmServiceInter {
 8     public void insertEmp() ;
 9     public void deleteEmp() ;
10     public void updateEmp() ;
11     public void selectEmp() ;
12 }
CrmServiceInter
 1 package spr.crm.test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 /**
 5  * 加载ioc容器
 6  * 获取controller层对象
 7  * 调用控制层方法
 8  */
 9 import org.springframework.context.support.ClassPathXmlApplicationContext;
10 
11 import spr.web.controller.CrmController;
12 public class TestRequest {
13     public static void main(String[] args) {
14         // 加载ioc容器
15         ApplicationContext context =
16                 new ClassPathXmlApplicationContext("applicationContext.xml");
17         //获取controller层对象
18         //CrmController controller=(CrmController)context.getBean("crmController");
19         CrmController controller=context.getBean(CrmController.class); 
20         //调用控制层方法
21         controller.execute();
22     }
23 }
TestRequest
 1 package spr.web.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 
 6 import spr.crm.service.inter.CrmServiceInter;
 7 
 8 /** web层控制器 */
 9 @Controller("crmController")
10 public class CrmController {
11     @Autowired
12     public CrmServiceInter service;
13     public void execute() {
14         System.out.println("web-execute");
15         service.deleteEmp();
16     }
17     public CrmServiceInter getService() {
18         return service;
19     }
20     public void setService(CrmServiceInter service) {
21         this.service = service;
22     }
23 }
CrmController

三、资源引用配置

 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" xmlns:p="http://www.springframework.org/schema/p"
 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
 7     http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
 8     http://www.springframework.org/schema/context
 9     http://www.springframework.org/schema/context/spring-context-4.3.xsd
10     http://www.springframework.org/schema/aop
11     http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
12     <!-- 启用classpath扫描注解 -->
13     <context:component-scan base-package="spr"></context:component-scan>
14     <!-- 启用aop注解 -->
15     <aop:aspectj-autoproxy/>
16 </beans>
Context
 1 <project xmlns="http://maven.apache.org/POM/4.0.0"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <groupId>spring</groupId>
 6     <artifactId>Spring_AOP</artifactId>
 7     <version>0.0.1-SNAPSHOT</version>
 8     <dependencies>
 9         <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
10         <dependency>
11             <groupId>org.springframework</groupId>
12             <artifactId>spring-core</artifactId>
13             <version>4.3.2.RELEASE</version>
14         </dependency>
15         <dependency>
16             <groupId>org.springframework</groupId>
17             <artifactId>spring-context</artifactId>
18             <version>4.3.2.RELEASE</version>
19         </dependency>
20         <dependency>
21             <groupId>org.springframework</groupId>
22             <artifactId>spring-beans</artifactId>
23             <version>4.3.2.RELEASE</version>
24         </dependency>
25         <dependency>
26             <groupId>org.springframework</groupId>
27             <artifactId>spring-expression</artifactId>
28             <version>4.3.2.RELEASE</version>
29         </dependency>
30         <!-- 配置spring aop -->
31         <dependency>
32             <groupId>org.springframework</groupId>
33             <artifactId>spring-aop</artifactId>
34             <version>4.3.2.RELEASE</version>
35         </dependency>
36         <!--spring-aspects -->
37         <dependency>
38             <groupId>org.springframework</groupId>
39             <artifactId>spring-aspects</artifactId>
40             <version>4.3.2.RELEASE</version>
41         </dependency>
42 
43         <!--使用AspectJ方式注解需要相应的包 -->
44         <dependency>
45             <groupId>org.aspectj</groupId>
46             <artifactId>aspectjrt</artifactId>
47             <version>1.6.11</version>
48         </dependency>
49         <!--使用AspectJ方式注解需要相应的包 -->
50         <dependency>
51             <groupId>org.aspectj</groupId>
52             <artifactId>aspectjweaver</artifactId>
53             <version>1.6.11</version>
54         </dependency>
55     </dependencies>
56 </project>
pom

 

转载于:https://www.cnblogs.com/cjxyu/articles/9677742.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOPSpring框架的一个重要模块,它提供了面向切面编程(AOP)的支持。AOP是一种编程思想,它可以在不改变原有代码的情况下,通过在程序运行时动态地将代码“织入”到现有代码,从而实现对原有代码的增强。 Spring AOP提供了基于注解AOP实现,使得开发者可以通过注解方式来定义切面、切点和通知等相关内容,从而简化了AOP使用。 下面是一个基于注解AOP实现的例子: 1. 定义切面类 ```java @Aspect @Component public class LogAspect { @Pointcut("@annotation(Log)") public void logPointcut() {} @Before("logPointcut()") public void beforeLog(JoinPoint joinPoint) { // 前置通知 System.out.println("执行方法:" + joinPoint.getSignature().getName()); } @AfterReturning("logPointcut()") public void afterLog(JoinPoint joinPoint) { // 后置通知 System.out.println("方法执行完成:" + joinPoint.getSignature().getName()); } @AfterThrowing(pointcut = "logPointcut()", throwing = "ex") public void afterThrowingLog(JoinPoint joinPoint, Exception ex) { // 异常通知 System.out.println("方法执行异常:" + joinPoint.getSignature().getName() + ",异常信息:" + ex.getMessage()); } } ``` 2. 定义业务逻辑类 ```java @Service public class UserService { @Log public void addUser(User user) { // 添加用户 System.out.println("添加用户:" + user.getName()); } @Log public void deleteUser(String userId) { // 删除用户 System.out.println("删除用户:" + userId); throw new RuntimeException("删除用户异常"); } } ``` 3. 在配置文件开启AOP ```xml <aop:aspectj-autoproxy/> <context:component-scan base-package="com.example"/> ``` 在这个例子,我们定义了一个切面类LogAspect,其通过@Aspect注解定义了一个切面,通过@Pointcut注解定义了一个切点,通过@Before、@AfterReturning和@AfterThrowing注解分别定义了前置通知、后置通知和异常通知。 在业务逻辑类,我们通过@Log注解标注了需要增强的方法。 最后,在配置文件,我们通过<aop:aspectj-autoproxy/>开启了AOP功能,并通过<context:component-scan>扫描了指定包下的所有组件。 这样,当我们调用UserService的方法时,就会触发LogAspect定义的通知,从而实现对原有代码的增强。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值