在 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 }
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 }
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 }
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 }
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 }
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 }
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 }
三、资源引用配置
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>
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>