一,why AOP?
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
总结: 把核心业务代码和非核心业务代码进行分离,从而降低核心业务代码和非核心代码的耦合度。
举例说明:使用动态代理可以完成核心业务代码和非核心业务代码的分离
1.创建接口
public interface ArithmeticCalculator {
/**
* 加法运算
* @param a
* @param b
* @return
*/
public int add(int a,int b);
/**
* 减法运算
* @param a
* @param b
* @return
*/
public int sub(int a,int b);
/**
* 乘法运算
* @param a
* @param b
* @return
*/
public int mul(int a,int b);
/**
* 除法运算
* @param a
* @param b
* @return
*/
public int div(int a,int b);
}
2.接口的实现类
public class ArithmeticCalculatorImpl implements ArithmeticCalculator{
public int add(int a, int b) {
int result=a+b;
return result;
}
public int sub(int a, int b) {
int result=a-b;
return result;
}
public int mul(int a, int b) {
int result=a*b;
return result;
}
public int div(int a, int b) {
int result=a/b;
return result;
}
}
3.创建代理类
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
public class LogProxy {//日志代理类
private ArithmeticCalculator target;//被代理的对象
public LogProxy(ArithmeticCalculator target){
this.target=target;
}
public ArithmeticCalculator getProxyInstance(){
//ClassLoader loader,
//Class<?>[] interfaces,
//InvocationHandler h
ClassLoader loader=target.getClass().getClassLoader();
Class<?>[] interfaces={ArithmeticCalculator.class}; //被代理的类实现了哪些接口
InvocationHandler h=new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("The method "+method.getName()+" beigin with:"+Arrays.asList(args));
Object result = method.invoke(target, args);
System.out.println("The method "+method.getName()+" end with:"+result);
return result;
}
};
ArithmeticCalculator instance =(ArithmeticCalculator) Proxy.newProxyInstance(loader,interfaces,h);//获取代理类对象
return instance;
}
}
4.测试
public class Test {
public static void main(String[] args) {
//创建被代理的对象。---张三
ArithmeticCalculator target=new ArithmeticCalculatorImpl();
//获取代理对象----李四
LogProxy logProxy=new LogProxy(target);
ArithmeticCalculator proxy=logProxy.getProxyInstance();
//使用代理对象调用相应的方法
int add = proxy.add(10, 5);
System.out.println("结果:"+add);
int sub = proxy.sub(10, 5);
System.out.println("结果:"+sub);
}
}
如上所示,这种写法很难理解? 没关系 spring框架帮你抽取了aop模式
使用AOP的步骤
(1)引入依赖--jar包 该版本号需要和spring其他框架版本号一致
<!--引入切面依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
(2)创建一个日志切面
/**
* aop:得使用步骤:
* (1)引入springaspect依赖
* (2)创建一个日志切面类。 该类必须使用@Aspect注解来标记。
* (3)该类也必须有spring容器来管理。@Component
* (4)spring配置文件中必须开启Aspect注解<aop:aspectj-autoproxy/>
*/
@Component //@Service @Controller一样
@Aspect //表示该类为切面类
public class LogAspect {
/**
* 第一个* 表示该方法得修饰符和返回类型
* 第二个* 表示指定包下所有的类。
* 第三个* 表示该类下所有的方法
* .. 方法得任意参数
*/
//该方法在指定表达式之前执行
@Before(value = "execution(* com.lj.aop.after.*.*(..))")
public void before(){
System.out.println("日志开始");
}
//该方法在指定表达式之后执行
@After(value = "execution(* com.lj.aop.after.*.*(..))")
public void after(){
System.out.println("日志结束");
}
}
(3)spring.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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启包扫描-->
<context:component-scan base-package="com.ykq.aop.after"/>
<!--开始切面注解-->
<aop:aspectj-autoproxy/>
</beans>
(4)测试
public class Test {
public static void main(String[] args) {
ApplicationContext app=new ClassPathXmlApplicationContext("spring.xml");
ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) app.getBean("arithmeticCalculator");
double add = arithmeticCalculator.fun(10, 5,6);
System.out.println("结果:"+add);
}
}
以上是基于注解开启切面编程
还有一种基于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:tx="http://www.springframework.org/schema/tx"
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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启包扫描-->
<context:component-scan base-package="com.lj.service"/>
<!--事务管理 理解为事务切面-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasoure"/>
</bean>
<!--配置事务的属性 必须规范-->
<!--read-only意思是只读 表示该名下的方法为只读 不能修改 一般用在查询方法上-->
<tx:advice id="myadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="select*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" timeout="3"/>
</tx:attributes>
</tx:advice>
<!--配置切面-->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.lj.service.*.*(..))"/>
<aop:advisor advice-ref="myadvice" pointcut-ref="pointcut"/>
</aop:config>
</beans>
二,AOP常用注解
@Before: 前置通知, 在方法执行之前执行
@After: 后置通知, 在方法执行之后执行
@AfterRunning: 返回通知, 在方法返回结果之后执行
@AfterThrowing: 异常通知, 在方法抛出异常之后
总结:
1.AOP: 什么是AOP?
切面编程,将核心业务代码与非核心业务代码分离,降低耦合性
2.AOP在实际项目的应用:
事物 日志
3.AOP的实现原理:
动态代理。