SpringAOP的3种实现方式
- 方式一:通过实现spring提供的接口实现
- 方式二:通过自定义方式织入实现
- 方式三:通过注解的方式实现
AOP (Aspect Orient Programming),直译过来就是 面向切面编程。AOP 是一种编程思想,是面向对象编程(OOP)的一种补充。面向对象编程将程序抽象成各个层次的对象,而面向切面编程是将程序抽象成各个切面。
方式一:通过实现spring提供的接口实现
配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="User" class="com.work.springAopDemo.oneWay.User"/>
<bean id="UserService" class="com.work.springAopDemo.oneWay.UserServiceImpl"/>
<bean id="LogBeforeAspect" class="com.work.springAopDemo.oneWay.LogBeforeAspect"/>
<bean id="LogAfterAspect" class="com.work.springAopDemo.oneWay.LogAfterAspect"/>
<!--配置aop-->
<aop:config>
<!--execution(* com.work.springAopDemo.oneWay.UserServiceImpl.*(..))
第一个*是要执行的位置
后面是对应类的路径
后面的*代表任意方法,
(..)代表方法可以有任意参数
-->
<aop:pointcut id="logAspect" expression="execution(* com.work.springAopDemo.oneWay.UserServiceImpl.*(..))"/>
<!--执行环绕增强
advice-ref="LogBeforeAspect"对应的是对应bean的ID值
pointcut-ref="logAspect"对应的是对应切面的ID值
-->
<aop:advisor advice-ref="LogBeforeAspect" pointcut-ref="logAspect"/>
<aop:advisor advice-ref="LogAfterAspect" pointcut-ref="logAspect"/>
</aop:config>
</beans>
前置增强
package com.work.springAopDemo.oneWay;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class LogBeforeAspect implements MethodBeforeAdvice {
/**
* 前置增强
*
* @param method 被代理的方法
* @param objects 被代理方法的参数
* @param o 被代理的目标对象
* @throws Throwable 抛出一个异常类
*/
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("前置增强执行了" + o.getClass().getName() + "类的" + method.getName() + "方法");
}
}
后置增强
package com.work.springAopDemo.oneWay;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class LogAfterAspect implements AfterReturningAdvice{
/**
* 日志的
*
* @param returnValue 方法的返回值
* @param method 被代理类的方法
* @param args 方法的参数
* @param target 被代理的类
* @throws Throwable 抛出的异常
*/
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("后置增强执行了" + target.getClass().getName() + "的" + method.getName() + "方法");
}
}
被代理的接口
package com.work.springAopDemo.oneWay;
public interface UserService {
public void addUser(User user);
public void deleteUser(User user);
public void updateUser(User user);
public void queryUser(int id);
}
被代理真实对象
package com.work.springAopDemo.oneWay;
public class UserServiceImpl implements UserService {
public void addUser(User user) {
System.out.println("添加一个用户");
}
public void deleteUser(User user) {
System.out.println("删除一个用户");
}
public void updateUser(User user) {
System.out.println("修改一个用户");
}
public void queryUser(int id) {
System.out.println("查询一个用户");
}
}
测试类
package com.work.springAopDemo.oneWay;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopAspectOneTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = applicationContext.getBean("UserService", UserService.class);
userService.addUser(new User());
userService.queryUser(1);
}
}
输出结果
前置增强执行了com.work.springAopDemo.oneWay.UserServiceImpl类的addUser方法
添加一个用户
后置增强执行了com.work.springAopDemo.oneWay.UserServiceImpl的addUser方法
方式二:通过自定义方式织入实现
配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="User" class="com.work.springAopDemo.twoWay.User"/>
<bean id="UserService" class="com.work.springAopDemo.twoWay.UserServiceImpl"/>
<bean id="DiyAspect" class="com.work.springAopDemo.twoWay.DiyAspect"/>
<!--配置aop-->
<aop:config>
<aop:pointcut id="pointCut" expression="execution(* com.work.springAopDemo.twoWay.UserServiceImpl.*(..))"/>
<aop:aspect id="diyAspect" ref="DiyAspect">
<aop:before method="before" pointcut-ref="pointCut"/>
<aop:after method="after" pointcut-ref="pointCut"/>
</aop:aspect>
</aop:config>
</beans>
自定义织入类
package com.work.springAopDemo.twoWay;
//自定义切面
public class DiyAspect {
public void before() {
System.out.println("===========前置增强========");
}
public void after() {
System.out.println("===========后置增强========");
}
}
被代理的接口和真实对象不变
测试类
package com.work.springAopDemo.twoWay;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopAspectTwoTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");
UserService userService = applicationContext.getBean("UserService", UserService.class);
userService.addUser(new User());
}
}
输出结果
===========前置增强========
添加一个用户
===========后置增强========
方式三:通过注解的方式实现
配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="User" class="com.work.springAopDemo.threeWay.User"/>
<bean id="UserService" class="com.work.springAopDemo.threeWay.UserServiceImpl"/>
<bean id="DiyAnnotationAspect" class="com.work.springAopDemo.threeWay.DiyAnnotationAspect"/>
<!--配置aop-->
<aop:aspectj-autoproxy/>
</beans>
自定义织入类
package com.work.springAopDemo.threeWay;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//自定义切面
@Aspect
public class DiyAnnotationAspect {
@Before("execution(* com.work.springAopDemo.threeWay.UserServiceImpl.*(..))")
public void before() {
System.out.println("===========前置增强========");
}
@After("execution(* com.work.springAopDemo.threeWay.UserServiceImpl.*(..))")
public void after() {
System.out.println("===========后置增强========");
}
@Around("execution(* com.work.springAopDemo.threeWay.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("===========环绕前置增强========");
joinPoint.proceed();//执行方法
System.out.println("===========环绕后置增强========");
}
}
被代理的接口和真实对象不变
测试类
package com.work.springAopDemo.threeWay;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopAspectThreeTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext3.xml");
UserService userService = applicationContext.getBean("UserService", UserService.class);
userService.addUser(new User());
}
}
输出结果
===========环绕前置增强========
===========前置增强========
添加一个用户
===========后置增强========
===========环绕后置增强========
要导入的依赖
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.3</version>
</dependency>
以上就是SpringAOP的3种实现方式