原理:
AOP原理其实非常简单,假如原来的程序是线性运行的,那么AOP就是在你指定的地方横插一刀,然后干一些自己的事(如权限检查,事务操作),这些操作全在一个切面里面。
指定的地方就叫JoinPoint,
订阅这个JoinPoint的就叫PointCut
权限检查就是Advice(Before是指在PointCut的前面执行Advice)
将JoinPoint和Advice全是寄身于一个Aspect里面的
需求:
1、有一个类叫UserService,里面有个方法叫addUser
2、现在期望在执行此方法前进行权限检查
步骤:
1、新建一个类UserService
package com.bjpowernode;
import org.springframework.stereotype.Component;
/**
* Created by Germmy on 2018/7/2.
*/
@Component
public class UserService {
public void addUser(){
System.out.println("add user...");
}
}
2、新建一个切面
package com.bjpowernode;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* Created by Germmy on 2018/7/2.
*/
@Aspect
@Component
public class SecureHandler {
@Pointcut("execution(* add*(..))")
private void addAddMethod(){}
@After("addAddMethod()")
public void safeCheck(){
System.out.println("safe check...");
}
}
3、新建Junit测试例
@Test
public void testAopWithAutowired(){
BeanFactory beanFactory=new ClassPathXmlApplicationContext("application_aspectj_bjpowernode_autowired.xml");
UserService userService= (UserService) beanFactory.getBean("userService");
userService.addUser();
}
4、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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
">
<!-- 使用注解识别Bean,而不是xml -->
<context:annotation-config />
<context:component-scan base-package="com.bjpowernode"/>
<aop:aspectj-autoproxy/> <!-- 启用aspect对annotation的支持 -->
</beans>
注意:如果想用Aop Annotation,则必须用AspectJ,引入Aspect的2个jar包,一个是aspect,一个是weaver
4.1、pom如下
<?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.mdjf</groupId>
<artifactId>test.aop</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
</dependencies>
</project>
5、效果如下图所示