Spring AOP
AOP属于解释:
主营业务(targetobject):crud,安全性检查就是我们的横切关注点,把横切关注点拿出来模块化为一个类,这个类就叫做切面类(Aspect),切面类作用在目标对象之前还是之后还是throws时,叫做advice,作用在目标对象的哪些方法上,叫做pointcut,怎么作用在目标对象上,这个过程叫做织物(weave),织物是一个概念,通过织物怎么作用在目标对象上,需要一个点来接,这个点叫做joinpoint,它也是一个概念,一个标识。
注解配置:
用到的依赖:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ming</groupId> <artifactId>spring_aop_annotation</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
xml配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.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"> <!--开启注解模式--> <aop:aspectj-autoproxy/> <!--targetObject--> <bean id="userManagerImpl" class="com.ming.UserManagerImpl"/> <!--Annotation--> <bean id="securityHandler" class="com.ming.SecurityHandler"/> </beans>
主营业务接口:
public interface IUserManager { public void addUser(String username, String password); public void modifyUser(int id, String username, String password); }
接口实现类:
public class UserManagerImpl implements IUserManager{ public void addUser(String username, String password) { System.out.println("=========UserManagerImpl.addUser()==========="); } public void modifyUser(int id, String username, String password) { System.out.println("=========UserManagerImpl.modifyUser()==========="); } }
切面类:
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class SecurityHandler { @Pointcut("execution(* com.ming.UserManagerImpl.*(..))") private void allMethod(){} @Before("allMethod()") public void checkSecurity(){ System.out.println("=======checkSecurity()=========="); } }
测试:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); IUserManager iUserManager = (IUserManager) context.getBean("userManagerImpl"); iUserManager.addUser("abc","111"); } }
xml配置
xml配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.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"> <!--targetObject--> <bean id="userManagerImpl" class="com.ming.UserManagerImpl"/> <!--Annotation--> <bean id="securityHandler" class="com.ming.SecurityHandler"/> <!-- Advice PointCut JoinPoint --> <aop:config> <aop:aspect id="myAspect" ref="securityHandler"> <aop:pointcut id="allMethod" expression="execution(* com.ming.UserManagerImpl.add*(..))"/> <aop:before method="checkSecurity" pointcut-ref="allMethod"/> </aop:aspect> </aop:config> </beans>
主营业务接口:
public interface IUserManager { public void addUser(String username, String password); public void modifyUser(int id, String username, String password); }
接口实现类:
public class UserManagerImpl implements IUserManager{ public void addUser(String username, String password) { System.out.println("=========UserManagerImpl.addUser()==========="); } public void modifyUser(int id, String username, String password) { System.out.println("=========UserManagerImpl.modifyUser()==========="); } }
切面类(安全性检查):
public class SecurityHandler { public void checkSecurity(){ System.out.println("=======checkSecurity()=========="); } }
测试:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); IUserManager iUserManager = (IUserManager) context.getBean("userManagerImpl"); iUserManager.addUser("abc","111"); iUserManager.modifyUser(1,"ddd","222"); } }