1,AOP的基本理解:
AOP (Aspect Orient Programming),直译过来就是 面向切面编程。AOP 是一种编程思想,是面向对象编程(OOP)的一种补充。面向对象编程将程序抽象成各个层次的对象,而面向切面编程是将程序抽象成各个切面。
2,Aop的特点:
AOP 框架有很多种, Spring 中的 AOP 是通过动态代理实现的。不同的 AOP 框架支持的连接点也有所区别,例如,AspectJ 和 JBoss,除了支持方法切点,它们还支持字段和构造器的连接点。而 Spring AOP 不能拦截对对象字段的修改,也不支持构造器连接点,我们无法在 Bean 创建时应用通知。
3,AOP直面理解:
动态代理
AOP
律师---代理---原告和被告
经纪人---代理---艺人/运动员
具体表现为有一个人对另外一个人进行增强
动态代理原始类----核心功能类
对核心功能类的方法进行增强----使用动态代理AOP--- 不修改源代码的基础上对功能进行增强
AOP:面向切面编程,要指定增强位置、配置该位置做的事情(切面)不修改源代码的基础上对代理对象的功能进行增强,增强是动态执行的(反射/动态代理)
4,代码展示:
1.创建被代理的类,并且定义功能方法:
package com.li.service;
public interface BookService {
void findAll();
void save(int a);
int del();
void update();
}
2.接口的实现类:
package com.li.service.impl;
import com.li.service.BookService;
/**
* 核心类
*/
public class BookServiceImpl implements BookService {
@Override
public void findAll() {
System.out.println("查询所有");
}
@Override
public void save(int a) {
System.out.println("保存信息" + a);
}
@Override
public int del() {
System.out.println("删除信息");
return 6;
}
@Override
public void update() {
System.out.println("修改信息");
}
}
3. Logger 定义的切入方法:
package com.li.logger;
public class Logger {
public void check() {
System.out.println("前置通知/增强:权限验证");
}
public void logPrint() {
System.out.println("后置通知/增强:日志输出");
}
public void exception() {
System.out.println("异常通知/增强:异常处理");
}
public void distroy() {
System.out.println("最终通知/增强:资源释放");
}
}
3 在src包中配置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">
<!--1.把所有类的对象交给IOC容器进行管理-->
<bean id="logger" class="com.li.logger.Logger"/>
<bean id="bookService" class="com.li.service.impl.BookServiceImpl"/>
<!--2.AOP的配置:让增强类 的 哪个方法 动态进行何种增强 核心类 的 哪个方法-->
<aop:config>
<!--指定增强类并起个名字-->
<aop:aspect id="check" ref="logger">
<!--前置通知/增强:在核心类方法-->
<aop:before method="check" pointcut="execution(* *..BookServiceImpl.*(..))"/>
<aop:after-returning method="logPrint" pointcut="execution(* *..BookServiceImpl.*(..))"/>
<aop:after-throwing method="exception" pointcut="execution(* *..BookServiceImpl.*(..))"/>
<aop:after method="distroy" pointcut="execution(* *..BookServiceImpl.*(..))"/>
</aop:aspect>
</aop:config>
</beans>
4.测试类:
package com.li.selvret;
import com.li.service.BookService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test01 {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
BookService bookService = context.getBean(BookService.class);
bookService.findAll();
bookService.save(6);
bookService.del();
bookService.update();
}
}