一,1.Spring AOP来历 ,Spring AOP是 Spring 框架的核心模块之一,它使用纯 Java 实现,因此不需要专门的编译过程和类加载器,可以在程序运行期通过代理方式向目标类织入增强代码。就是不修改源代码之修配置文件完成一下代码的接耦性。
2,Spring AOP 的代理机制
Spring 在运行期会为目标对象生成一个动态代理对象,并在代理对象中实现对目标对象的增强。
3,Spring AOP 的底层是通过以下 2 种动态代理机制,为目标对象(Target Bean)执行横向织入的。
二,1.Spring AOP 连接点
Spring AOP 并没有像其他 AOP 框架(例如 AspectJ)一样提供了完成的 AOP 功能,它是 Spring 提供的一种简化版的 AOP 组件。其中最明显的简化就是,Spring AOP 只支持一种连接点类型:方法调用。您可能会认为这是一个严重的限制,但实际上 Spring AOP 这样设计的原因是为了让 Spring 更易于访问。
2.方法调用连接点是迄今为止最有用的连接点,通过它可以实现日常编程中绝大多数与 AOP 相关的有用的功能。如果需要使用其他类型的连接点(例如成员变量连接点),我们可以将 Spring AOP 与其他的 AOP 实现一起使用,最常见的组合就是 Spring AOP + ApectJ。
Spring AOP 通知类型
AOP 联盟为通知(Advice)定义了一个 org.aopalliance.aop.Interface.Advice 接口。Spring AOP 通知类型
AOP 联盟为通知(Advice)定义了一个 org.aopalliance.aop.Interface.Advice 接口。
三,一般切面的 AOP 开发
当我们在使用 Spring AOP 开发时,若没有对切面进行具体定义,Spring AOP 会通过 Advisor 为我们定义一个一般切面(不带切点的切面),然后对目标对象(Target)中的所有方法连接点进行拦截,并织入增强代码。
四,1.AOP基于XML配置的入门案例:
2.首先是导入架包和创建所需要的包
3.创建service类,这个类完成核心功能操作。
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 5;
}
@Override
public void update(){
System.out.println("修改");
}
}
4.接口中含有四个方法
public interface BookService {
void findAll();
void save(int a);
int del();
void update();
}
5.这是核心文件,接下来要做的就是在不更改核心文件代码的情况下进行加强代码的添加
6.创建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("最终通知/增强:资源释放");}
}
7. 在 src 目录下创建一个 Spring 的配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
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容器进行管理-->
<!--2.AOP的配置:让增强类 的 哪个方法 动态进行何种增强 核心类 的 哪个方法-->
<bean id="Loging" class="com.wang.dao.BookDao"/>
<bean id="bookServise" class="com.wang.servise.Inpl.BookServiseImpl"/>
<aop:config>
<aop:aspect id="check" ref="Loging">
<aop:before method="check" pointcut="execution(* *..BookServiseImpl.*(..))"/>
<aop:after-returning method="LogPrintk" pointcut="execution(* *..BookServiseImpl.*(..))"/>
<aop:after-throwing method="exception" pointcut="execution(* *..BookServiseImpl.*(..))"/>
<aop:after method="Distrooy" pointcut="execution(* *..BookServiseImpl.*(..))"/>
</aop:aspect>
</aop:config>
</beans>
五,以上步骤都完成后就 即可完成增强 可以在servlet中能Text01类中测试了。
public class Test01 {
public static void main(String[] args) {
//先加载spring配置文件,通过容器获取对象
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
BookService bean = context.getBean(BookService.class);
bean.findAll();
bean.save(6);
bean.del();
bean.update();
}
}