什么是AOP?
OP是面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是Spring中的一个重要内容可以利用AOP对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高开发效率。简单的来说:AOP专门用于处理系统中分布于各个模块(不同方法)中交叉关注点的问题
AOP的基本概念
- 横切关注点:需要关注的部分,想要添加进去的部分。比如说想加个日志Log
- 切面(Aspect):横切关注点被模块化的对象,一个类。就是我们要加的Log类
- 通知(Advice):切面要完成的工作。相当于Log中的方法
- 目标(Target):被通知的对象
- 代理(Proxy):向目标对象应用统治之后创建的对象。即代理类。
- 切入点(PointCut):切面通知执行的地点的定义。就相当于要添加Log的位置。
- 连接点(JoinPoint):与切入点匹配的执行点。
使用Spring实现Aop
maven依赖:
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
使用
接口:
package com.lyr.service;
public interface UserService {
public void insert();
public void delete();
public void update();
public void select();
}
实现类:
package com.lyr.service;
public class UserServiceImpl implements UserService {
public void insert() {
System.out.println("增加了一个用户");
}
public void delete() {
System.out.println("删除了一个用户");
}
public void update() {
System.out.println("修改了一个用户");
}
public void select() {
System.out.println("查询了一个用户");
}
}
方式一
使用Spring的API接口【主要SpringAPI接口实现】
Log类:在之前插入一条日志
package com.lyr.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
//args:参数
//target:目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
AfterLog类:在之后插入一条日志
package com.lyr.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
//returnValue返回值
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
}
}
applicationContext.xml:注册bean,设置aop
<?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
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.lyr.service.UserServiceImpl"></bean>
<bean id="log" class="com.lyr.log.Log"></bean>
<bean id="afterLog" class="com.lyr.log.AfterLog"></bean>
<!--配置aop:需要导入aop的约束-->
<aop:config>
<!--切入点:expression:表达式,execution(要执行的位置 * * * * *)-->
<!--execution(*是返回值的类型(*代表所有类型) com.lyr.service.UserServiceImpl.*(..) 前面是包名+类名 *(..)是该类下的所有方法-->
<aop:pointcut id="pointcut" expression="execution(* com.lyr.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
测试类:
import com.lyr.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class myTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.insert();
}
}
方式二
自定义来实现AOP【主要是切面定义】
designPointcut类:自己定义插入日志的类
package com.lyr.design;
public class designPointcut {
public void before(){
System.out.println("----方法执行前----");
}
public void after(){
System.out.println("----方法执行后----");
}
}
applicationContext.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: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
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.lyr.service.UserServiceImpl"></bean>
<bean id="log" class="com.lyr.log.Log"></bean>
<bean id="afterLog" class="com.lyr.log.AfterLog"></bean>
<!--方式二:自定义类 -->
<bean id="design" class="com.lyr.design.designPointcut"></bean>
<aop:config>
<!--自定义切面,ref就是要引用的类-->
<aop:aspect ref="design">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.lyr.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
测试类:
import com.lyr.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class myTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.insert();
}
}
方式三
使用注解实现
annotationPointcut类:使用注解定义切面
package com.lyr.design;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect //标注这个类是一个切面
public class annotationPointcut {
@Before("execution(* com.lyr.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("====方法执行前====");
}
@After("execution(* com.lyr.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("====方法执行前====");
}
}
applicationContext.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: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
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.lyr.service.UserServiceImpl"></bean>
<bean id="log" class="com.lyr.log.Log"></bean>
<bean id="afterLog" class="com.lyr.log.AfterLog"></bean>
<!--方式三:使用注解实现AOP-->
<bean id="annoPointcut" class="com.lyr.design.annotationPointcut"/>
<aop:aspectj-autoproxy/>
</beans>
测试类:
import com.lyr.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class myTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.insert();
}
}
本文深入解析面向切面编程(AOP)的概念及其在Spring框架中的应用,介绍如何使用Spring AOP实现业务逻辑的解耦,包括前置通知、后置通知及切入点的配置,并演示三种实现方式。
782

被折叠的 条评论
为什么被折叠?



