SpringAOP的几个通知的应用
xml版
新建一个maven项目
项目结构图
pom.xml 文件
<?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.spring</groupId>
<artifactId>SpringAOP</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
新建一个advice类用来配置通知
package com.spring.advice;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAdvice {
public void before(){
System.out.println("前置通知");
}
public void afterReturning(){
System.out.println("后置通知");
}
public void afterThrowing(){
System.out.println("异常通知");
}
public void after(){
System.out.println("最终通知");
}
/**
* 环绕通知
* @param pjp 得到目标对象及其方法参数等信息
* @return
*/
public Object around(ProceedingJoinPoint pjp){
Object obj = null;
try {
System.out.println("环绕通知--前置通知");
//得到当前执行方法中的参数
Object[] args = pjp.getArgs();
//放行方法
obj = pjp.proceed(args);
System.out.println("环绕通知--后置通知");
}catch (Throwable t){
t.printStackTrace();
System.out.println("环绕通知--异常通知");
}finally {
System.out.println("环绕通知--最终通知");
}
return obj;
}
创建一个service类
package com.spring.service;
public interface Service {
public void findAll();
public Integer save();
public Integer update(Integer i);
}
service的实现类
package com.spring.service.impl;
import com.spring.service.Service;
public class ServiceImpl implements Service {
@Override
public void findAll() {
//int i=1/0;
System.out.println("查询成功");
}
@Override
public Integer save() {
System.out.println("保存成功");
return 1;
}
@Override
public Integer update(Integer i) {
System.out.println("更新成功");
return 1;
}
}
创建一个beans.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.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">
<!--把目标对象交给ioc容器-->
<bean id="userService" class="com.spring.service.impl.ServiceImpl"/>
<!--把通知对象交给ioc容器-->
<bean id="myAdvice" class="com.spring.advice.MyAdvice"/>
<!--配置aop:里面可以配置多个切面-->
<aop:config>
<!--切入点表达式-->
<aop:pointcut id="pointcut" expression="execution(* com.spring.service.impl.*.*(..))"/>
<!--
具体的一个切面配置
id 是当前切面的唯一标识
ref 是指定当前切面使用哪个通知
-->
<aop:aspect id="aspect" ref="myAdvice">
<!--前置通知-->
<!--让通知类myAdvice中的before方法在UserServiceImpl.findAll执行之前执行-->
<aop:before method="before" pointcut-ref="pointcut"/>
<!--后置通知-->
<aop:after-returning method="afterReturning" pointcut-ref="pointcut"/>
<!--异常通知-->
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"/>
<!--最终通知-->
<aop:after method="after" pointcut-ref="pointcut"/>
<!--环绕通知-->
<!--<aop:around method="around" pointcut-ref="pointcut"/>-->
</aop:aspect>
</aop:config>
</beans>
测试类
package com.spring.test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:beans.xml")
public class Test {
@Autowired
private com.spring.service.Service service;
@org.junit.Test
public void test() {
service.findAll();
System.out.println();
}
}
测试结果:
前置通知
查询成功
后置通知
最终通知
如果想要使用环绕通知则需要将beans.xml中的前置通知、后置通知、最终通知注释掉,然后打开环绕通知的注释。
注解版
项目结构图
pom.xml文件与xml版一样
advice类
package com.spring.advice;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MyAdvice {
@Pointcut("execution(* com.spring.service.impl.*.*(..))")
public void pointcut(){};
@Before("pointcut()")
public void before(){
System.out.println("前置通知");
}
@AfterReturning("pointcut()")
public void afterReturning(){
System.out.println("后置通知");
}
@AfterThrowing("pointcut()")
public void afterThrowing(){
System.out.println("异常通知");
}
@After("pointcut()")
public void after(){
System.out.println("最终通知");
}
/**
* 环绕通知
* @param pjp 得到目标对象及其方法参数等信息
* @return
*/
// @Around("execution(* com.spring.service.impl.*.*(..))")
public Object around(ProceedingJoinPoint pjp){
Object obj = null;
try {
System.out.println("前置通知");
//得到当前执行方法中的参数
Object[] args = pjp.getArgs();
//放行方法
obj = pjp.proceed(args);
System.out.println("后置通知");
}catch (Throwable t){
t.printStackTrace();
System.out.println("异常通知");
}finally {
System.out.println("最终通知");
}
return obj;
}
}
service类
package com.spring.service;
public interface Service {
public void findAll();
public Integer save();
public Integer update(Integer i);
}
package com.spring.service.impl;
import com.spring.service.Service;
@org.springframework.stereotype.Service
public class ServiceImpl implements Service {
@Override
public void findAll() {
//int i=1/0;
System.out.println("查询成功");
}
@Override
public Integer save() {
System.out.println("保存成功");
return 1;
}
@Override
public Integer update(Integer i) {
System.out.println("更新成功");
return 1;
}
}
写一个配置类
package com.spring.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@ComponentScan("com.spring")
@EnableAspectJAutoProxy
public class SpringConfig {
}
测试类
package com.spring.test;
import com.spring.config.SpringConfig;
import com.spring.service.Service;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class Test {
@Resource
private Service service;
@Test
public void test() {
service.findAll();
System.out.println();
}
}
测试结果:
前置通知
查询成功
最终通知
后置通知