1、什么是AOP
AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的唯一维护的一种技术。AOP是OOP的延续,是软件开发的一个热点,也是spring框架的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
AOP在spring中的作用
- 提供声明式事务;允许用户自定义切面
以下是需要理解的名词:
-
横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 …
-
切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。
-
通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
-
目标(Target):被通知对象。
-
代理(Proxy):向目标对象应用通知之后创建的对象。
-
切入点(PointCut):切面通知 执行的 “地点”的定义。
-
连接点(JointPoint):与切入点匹配的执行点。
2、使用spring实现AOP
使用AOP织入,需要导入一个依赖包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
2.1、aop实现方式一:spring API接口
- 首先编写我们业务接口和实现类
public interface UserService {
void add();
void delete();
void update();
void select();
}
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("添加了一个用户");
}
@Override
public void delete() {
System.out.println("删除了一个用户");
}
@Override
public void update() {
System.out.println("修改了一个用户");
}
@Override
public void select() {
System.out.println("查询了一个用户");
}
}
- 编写增强类,一个前置增强,一个后置增强
public class LogBefore implements MethodBeforeAdvice {
//method:要执行的目标对象方法
//objects:参数
//o:目标对象
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName()+"执行了"+method.getName()+"方法");
}
}
public class LogAfter implements AfterReturningAdvice {
//returnValue 返回值
//method被调用的方法
//args 被调用的方法的对象的参数
//target 被调用的目标对象
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"执行了"+method.getName()+"返回值是:"+returnValue);
}
}
- 配置spring的xml,并实现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:context="http://www.springframework.org/schema/context"
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/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册Bean-->
<bean id="userServiceImpl" class="com.chen.service.UserServiceImpl"></bean>
<bean id="logBefore" class="com.chen.log.LogBefore"></bean>
<bean id="logAfter" class="com.chen.log.LogAfter"></bean>
<!--aop实现方式一:使用spring API接口-->
<!--配置aop-->
<aop:config>
<!--配置切入点 -> expression:表达式,execution:要执行的位置-->
<aop:pointcut id="pointcut" expression="execution(* com.chen.service.UserServiceImpl.*(..))"/>
<!--执行环绕:advice-ref执行方法 pointcut-ref:切入点 -->
<aop:advisor advice-ref="logBefore" pointcut-ref="pointcut"></aop:advisor>
<aop:advisor advice-ref="logAfter" pointcut-ref="pointcut"></aop:advisor>
</aop:config>
</beans>
- 测试
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserService userServiceImpl = (UserService) context.getBean("userServiceImpl");
userServiceImpl.add();
}
}
注意:动态代理代理的是接口
UserService userServiceImpl = (UserService) context.getBean("userServiceImpl");