Spring AOP
在beans.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
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-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.edu.hpu" />
<aop:aspectj-autoproxy/>
</beans>
导入相关jar包
- aspectj-1.7.2.jar
- aspectjweaver.jar
- spring-aop-3.2.1.RELEASE.jar
- spring-aspects-3.2.1.RELEASE.jar
- aopalliance-alpha1.jar
- aopalliance.jar
编写需要加入逻辑的类LogInteceptor,并在UserDAOImpl类save方法前插入逻辑
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component(value="logInterceptor")
public class LogInterceptor {
@Before("execution(public void
com.edu.hpu.impl.UserDAOImpl.save(com.edu.hpu.model.User))")
public void before() {
System.out.println("method start");
}
}
在save方法正常执行完毕的时候加入逻辑
@AfterReturning("execution(public void
com.edu.hpu.impl.UserDAOImpl.save(com.edu.hpu.model.User))")
public void afterReturning() {
System.out.println("after Returning!");
}
在任何方法出现异常的时候加入逻辑
@AfterThrowing("execution(public * com.edu.hpu.impl.*.*(..))")
public void afterThrowing() {
System.out.println("after Throwing");
}
在方法前后加入逻辑
@Around("execution(public * com.edu.hpu.impl.*.*(..))")
public void around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("around method start");
pjp.proceed();
System.out.println("around method end");
}
定义通用方法,使用Pointcut切面
@Pointcut("execution(public * com.edu.hpu.impl.*.*(..))")
public void myMethod() {};
@Before("myMethod()")
public void before() {
System.out.println("method start");
}
使用XML进行Aspect配置
在相应save方法前加上LogInterceptor类before方法的逻辑
第一种方法:声明一个全局的切面(Pointcut)
<bean id="logInteceptor" class="com.edu.hpu.aop.LogInterceptor"></bean>
<aop:config>
<aop:pointcut expression="
execution(public * com.edu.hpu.impl.UserDAOImpl.*(com.edu.hpu.model.User))" id="servicePointcut"/>
<aop:aspect id="logAspect" ref="logInteceptor">
<aop:before method="before" pointcut-ref="servicePointcut"/>
</aop:aspect>
</aop:config>
第二种方法:声明一个局部的Pointcut
<bean id="logInterceptor" class="com.edu.hpu.aop.LogInterceptor"></bean>
<aop:config>
<aop:aspect id="logAspect" ref="logInterceptor">
<aop:pointcut
expression="execution(public * *.*(..))" id="servicePointcut"/>
<aop:before method="before" pointcut-ref="servicePointcut"/>
</aop:aspect>
</aop:config>
第三种方法:不声明Pointcut
<bean id="logInterceptor" class="com.edu.hpu.aop.LogInterceptor"></bean>
<aop:config>
<aop:aspect id="logAspect" ref="logInterceptor">
<aop:before method="before" pointcut="
execution(public * com.edu.hpu.impl.UserDAOImpl.*(com.edu.hpu.model.User))" />
</aop:aspect>
</aop:config>
分享到:
相关推荐
Spring基础:Spring AOP简单使用
Spring AOP简单demo 入门级的 advice
spring aop简单例子,入门学习的好资料
spring的aop简单例子spring的aop简单例子spring的aop简单例子spring的aop简单例子
NULL 博文链接:https://306963591.iteye.com/blog/1129837
Spring框架的关键组件之一是面向方面编程(AOP)框架。 面向方面的编程需要将程序逻辑分解成不同的部分。 此教程将通过简单实用的方法来学习Spring框架提供的AOP/面向方面编程。
一个基于配置文件的Spring AOP的实现。实现了前置通知,后置通知,以及拦截器的功能,配置中有详细的注释。
SpringAop的简单理解.pdf
代码实现简单,易于维护:使用 Spring AOP 可以将耗时监测的逻辑与业务逻辑进行解耦,避免业务逻辑代码的冗余和代码维护难度的提高。 2. 安全性高:使用 Spring AOP 进行方法耗时监测,可以在不修改业务逻辑代码的...
使用Spring的AOP技术,实现日志记录功能。记录如下信息: 在哪个时间访问了哪个方法,方法传入了哪些参数
Spring AOP 日志管理 实例LoggingThrowsAdvice.java
用spring AOP做的最简单的日志记录小例子,特别简单,可直接运行。
关于AOP注解前置通知、后置通知、返回通知、异常通知的注解注释及应用
主要对Spring AOP的相关概念和简单的静态代理、动态代理以及常见的几种AOP配置方式做总结学习。主要包括:1. AOP的常见概念 2. 静态代理 3. jdk动态代理 4. Aspectj and Aspectjweaver 5. **aop-config** 6. CGLIB ...
自己总结的学Spring AOP的步骤,以及代码示例。可供刚学AOP的同学们参考。
NULL 博文链接:https://fsh430623.iteye.com/blog/1226945
spring aop 通过注解实现的项目,这只是一个简单的例子。运行环境也简单。
源代码是spring-boot添加aop切面功能的demo,简单易懂.
自己动手做的一个spring aop 的小demo,主要是annotation标签的配置联系,比较简单.
一个基于@AspectJ的spring2.0 AOP应用实例,很小很简单,没有任何额外信息,最适合AOP入门学习。使用log4j打印信息。把项目直接import进myeclipse就可以使用啦......