版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/l707268743/article/details/79339746
用maven搭建一个简易的springAOP
1.首先新建一个maven项目
2.修改pom.xml文件,引入3个jar包:
- spring-aop
- spring-context
- aspectjweaver
<?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.anting</groupId>
<artifactId>anting</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<!-- -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
</dependencies>
</project>
3.在src/main/java下面进行操作
3.1.在resources目录下新建application.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
"><!-- 要添加最后2行 -->
<context:annotation-config />
<context:component-scan base-package="com.anting"/> <!-- 自动扫描 -->
<aop:aspectj-autoproxy/> <!-- 要添加本行 -->
</beans>
3.2 新建AOP类
package com.anting;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* @author liyk
* @date 2018-02-19 下午 8:35
*/
@Component
@Aspect
public class Aop {
/**
* 定义切面,拦截方法
execution(
public : 方法的修饰词(*代表所有)
String : 方法的返回值(*代表所有)
com.anting..* : 包com.anting和其下面所有子包的所有类(..表示包含子包下的所有类)
.run* : 方法名,此处可指定类名以run开头(*run*表示方法名只需要run即可,类似*run)
(..) : 接受的参数类型,(..表示所有参数)[(int,*)表示接受参数为2个,其中第一个为int类型,第二个任一]
)
*/
@Pointcut("execution (public * com.anting..*.run*(..))")
public void pointCutMethod(){};
/* @Before("pointCutMethod()")
public void before() {
System.out.println("method start");
}
@After("pointCutMethod()")
public void after(){
System.out.println("method end");
}*/
@Around("pointCutMethod()")
public void around(ProceedingJoinPoint joinpoint) throws Throwable {
//可以先做一些判断
if(1==1){
System.out.println("方法条件满足,开始执行方法!");
joinpoint.proceed();
System.out.println("执行方法完毕!");
}else{
System.out.println("方法条件不满足,直接退出!");
}
}
}
3.3 新建测试用类
package com.anting;
import org.springframework.stereotype.Component;
//定义Spring的bean
@Component("person")
public class Person {
public void run(){
System.out.println("我在run...");
}
public String run(int i){
System.out.println("我在run"+i+"...");
return i+"";
}
public void run(int i,String a){
System.out.println("我在run"+i+"..." + a);
}
public void say(){
System.out.println("我在say...");
}
}
4 测试结果
package com.anting;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author liyk
* @date 2018-02-19 下午 9:53
*/
public class Demo1 {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("application.xml");
Person person = (Person)ctx.getBean("person");
person.run(1);
person.run(1,"test");
person.say();
}
}
5 测试结果
方法条件满足,开始执行方法!
我在run1...
执行方法完毕!
方法条件满足,开始执行方法!
我在run1...test
执行方法完毕!
我在say...