新建一个Maven项目
pom.xml 内容
<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.ailianshuo</groupId>
<artifactId>springaop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springaop</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.10.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.4</version>
</dependency>
</dependencies>
</project>
创建要被代理的Math类
为了实现IOC扫描在Math类上注解了@Service并命名bean为math。相当于实现方式一中在xml配置文件中增加了一个bean,
package com.ailianshuo.springaop.sample02;
import org.springframework.stereotype.Service;
/**
* 创建要被代理的Math类
*
* @author ailianshuo
* 2017年7月25日 下午5:20:32
*/
@Service("math")
public class Math {
//加
public int add(int n1,int n2){
int result=n1+n2;
System.out.println(n1+"+"+n2+"="+result);
return result;
}
//减
public int sub(int n1,int n2){
int result=n1-n2;
System.out.println(n1+"-"+n2+"="+result);
return result;
}
//乘
public int mut(int n1,int n2){
int result=n1*n2;
System.out.println(n1+"X"+n2+"="+result);
return result;
}
//除
public int div(int n1,int n2){
int result=n1/n2;
System.out.println(n1+"/"+n2+"="+result);
return result;
}
}
需要使用到的通知类Advices
添加注解:
@Component表示该类的实例会被Spring IOC容器管理;@Aspect表示声明一个切面;
@Before表示before为前置通知,通过参数execution声明一个切点
package com.ailianshuo.springaop.sample02;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/**
* 通知类,横切逻辑
* @author ailianshuo
* 2017年7月25日 下午5:21:42
*
*/
@Component
@Aspect
public class Advices {
@Before("execution(* com.ailianshuo.springaop.sample02.Math.*(..))")
public void before(JoinPoint jp){
System.out.println("----------before advice----------");
System.out.println(jp.getSignature().getName());
}
@After("execution(* com.ailianshuo.springaop.sample02.Math.*(..))")
public void after(JoinPoint jp){
System.out.println("----------after advice----------");
}
}
配置容器初始化时需要的XML文件
在配置IOC的基础上增加了aop:aspectj-autoproxy节点,Spring框架会自动为与AspectJ切面配置的Bean创建代理,proxy-target-class=”true”属性表示被代理的目标对象是一个类,而非实现了接口的类,主要是为了选择不同的代理方式。
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<context:component-scan base-package="com.ailianshuo.springaop.sample02">
</context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
</beans>
#测试代码Test
package com.ailianshuo.springaop.sample02;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 使用注解配置Spring AOP
* @author ailianshuo
* 2017年7月25日 下午11:42:57
*/
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("aopsample02.xml");
Math math = ctx.getBean("math", Math.class);
int n1 = 10, n2 = 5;
math.add(n1, n2);
math.sub(n1, n2);
math.mut(n1, n2);
math.div(n1, n2);
}
}
“`
运行结果
———-before advice———-
add
10+5=15
———-after advice———-
———-before advice———-
sub
10-5=5
———-after advice———-
———-before advice———-
mut
10X5=50
———-after advice———-
———-before advice———-
div
10/5=2
———-after advice———-
项目下载
请到码云:下载