在Spring整合aspectj实现aop的两种方式
-----------------------------基于XML配置方案
目标对象接口
1 public interface IUserService {
public void add();
public void update();
public void del();
public void search();
}
1.创建目标对象
public class UserServiceImpl implements IUserService {
@Override
public void add() {
System.out.println("add....");
}
@Override
public void update() {
System.out.println("update....");
}
@Override
public void del() {
System.out.println("del....");
}
@Override
public void search() {
System.out.println("search....");
}
}
2.创建通知(增强 advice)
//adcice增强类 Aspectj框架定义的通知类型
public class UserServiceHelper {
//前置通知
public void before(JoinPoint jp){
System.out.println("拦截目标类"+jp.getSignature().getDeclaringTypeName());
System.out.println("拦截目标方法名"+jp.getSignature().getName());
System.out.println("前置通知"); }
public void afterreturning(JoinPoint jp,Object val){
System.out.println("目标返回值"+val);
System.out.println("后置通知");
}
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("环绕前通知");
Object value = pjp.proceed();
System.out.println("环绕后通知");
return value;
}
public void afterthrowing(JoinPoint jp,Throwable ex){
//有异常时才输出
System.out.println("异常抛出通知"+ex);
}
public void after(JoinPoint jp){
System.out.println("拦截目标方法名"+jp.getSignature().getName());
System.out.println("最终通知");
}
}
3.1在Spring的applicationContext.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"
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">
<import resource="./aop4.xml" />
</beans>
3.2在Spring的xmp配置apo1.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">
<!-- target 目标对象 -->
<bean id="userService" class="cn.aspectjs.UserServiceImpl"/>
<!-- advice 通知-->
<bean id="userServiceAdvice" class="cn.aspectjs.UserServiceHelper"/>
<!-- config是用来声明,内容表示不管有没有接口,都用aspectj框架实现aop,aspect用来配置切面 -->
<aop:config proxy-target-class="true">
<aop:aspect ref="userServiceAdvice">
<aop:pointcut expression="execution(* *.add(..))" id="MypointCut"/>
<!-- method是增强类 -->
<aop:before method=""/>
<aop:before method="before" pointcut-ref="MypointCut"/>
<aop:after-returning method="afterreturning" pointcut-ref="MypointCut" returning="val"/>
<aop:around method="around" pointcut-ref="MypointCut" />
<aop:after-throwing method="afterthrowing" pointcut-ref="MypointCut" throwing="ex"/>
<aop:after method="after" pointcut-ref="MypointCut" />
</aop:aspect>
</aop:config>
</beans>
4.测试类
//Spring整合JUnit测试
@RunWith(SpringJUnit4ClassRunner.class)
//参数定义了要装入的Spring配置文件
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class AspectJTest{
@Autowired
private IUserService userService;
@Test
public void test1(){
//对应aop1.xml中的aspectj定义的通知.
userService.add();
}
}
基于annotation方案
1.编写目标
@Component //spring 扫描对象
public interface ICustomerService {
public void save();
public void search();
public void update();
}
@Service
public class CustomerService implements ICustomerService {
@Override
public void save() {
System.out.println("save......");
}
@Override
public void search() {
//System.out.println(10/0);
System.out.println("search......");
}
@Override
public void update() {
System.out.println("update......");
} }
2.编写增强
//增强类(通知)
@Component//声明Spring容器
@Aspect//声明当前的bean的就是一个切面
public class CustomerServiceHelper {
@Pointcut("execution(* *.s*(..))")
private void mypointcut(){}; @Pointcut("execution(* *.update(..))")
private void mypointcut1(){};
//前置通知
@Before("mypointcut()||mypointcut1()")
public void beforeA(JoinPoint jp){
System.out.println("目标类名称"+jp.getSignature().getDeclaringTypeName());
System.out.println("目标方法名称"+jp.getSignature().getName());
System.out.println("前置通知......");
}
@AfterReturning(value="execution(* *.s*(..))",returning="o")
public void AfterReturning(JoinPoint jp,Object o){
System.out.println("后置通知.,目标方法的返回是"+o);
}
@Around("execution(* *.s*(..))")
public Object Around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("执行前操作...");
//是否执行目标方法
Object value = pjp.proceed();
System.out.println("执行后操作...");
//环绕通知必须要有返回值,值为目标方法的返回值
System.out.println("目标方法的返回值"+value);
return value;
}
@AfterThrowing (value="execution(* *.s*(..))",throwing="t")
public void AfterThrowing(JoinPoint jp,Throwable t){
System.out.println("异常抛出通知......"+t);
}
@org.aspectj.lang.annotation.After(value="execution(* *.s*(..))")
public void After(JoinPoint jp){
System.out.println("最后通知......");
}
}
3.配置
<?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">
<import resource="./aop2.xml"/>
</beans>
applicationContext.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">
<!-- 在Spring配置文件中配置扫描注解 -->
<context:component-scan base-package="cn.annotation"/>
<!-- 开启aspectj注解自动代理 .false代表的是如果目标是有接口的使用proxy代理,如果没有接口使用cglib动态代理-->
<aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="false"/>
</beans>
aop2
4.测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class AnnotationTest {
@Autowired
private ICustomerService customerService;
@Test
public void test(){
customerService.search();
}
}
5.控制台打印结果
执行前操作...
目标类名称cn.annotation.CustomerService
目标方法名称search
前置通知......
search......
执行后操作...
执行通知,目标方法的返回值null
最后通知......
后置通知.,目标方法的返回是null
在Spring整合aspectj实现aop的两种方式的更多相关文章
- Spring整合AspectJ的AOP
学而时习之,不亦说乎! --<论语> 看这一篇之前最好先看前面关于AOP的两篇. http://www.cnblogs.com/z ...
- springmvc配置AOP的两种方式
spingmvc配置AOP有两种方式,一种是利用注解的方式配置,另一种是XML配置实现. 应用注解的方式配置: 先在maven中引入AOP用到的依赖 <dependency> <gr ...
- Spring加载properties文件的两种方式
在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...
- Spring Boot定义系统启动任务的两种方式
Spring Boot定义系统启动任务的两种方式 概述 如果涉及到系统任务,例如在项目启动阶段要做一些数据初始化操作,这些操作有一个共同的特点,只在项目启动时进行,以后都不再执行,这里,容易想到web ...
- Spring Boot 中实现定时任务的两种方式
在 Spring + SpringMVC 环境中,一般来说,要实现定时任务,我们有两中方案,一种是使用 Spring 自带的定时任务处理器 @Scheduled 注解,另一种就是使用第三方框架 Qua ...
- Spring容器自动调用方法的两种方式
先看一个Spring中Bean的实例化过程: 1.配置文件中指定Bean的init-method参数 <bean class="com.jts.service.UserService& ...
- 使用aspectJ实现Spring AOP的两种方式
方式一:基于aspectJ的XML配置 方式二:基于aspectJ的注解方式 基于aspectJ的XML配置 1) 引入相关jar包 2) 创建Spring核心配置文件,必须导 ...
- 关于aop的两种方式-基于注解和基于aspectj
spring的aop确实好用,能够在不影响业务功能的情况下,实现一些低耦合的功能. 而aop又有两种常用的实现方式,一种是用aspectj表达式去匹配,实现全局的配置,表达式还可以使用与或非符号去连接 ...
- spring的面向切面实现的两种方式
面向切面:主要应用在日志记录方面.实现业务与日志记录分离开发. spring面向切面有两种实现方式:1.注解 2.xml配置. 1.注解实现如下: (1)配置如下: <?xml version= ...
随机推荐
- oracle 单实例DG(搭建篇一)
一,介绍 lodding... 二,安装前环境配置 01,依赖包的安装: yum install binutils-* yum install compat-libstdc++-* yum insta ...
- Spark Shell启动时遇到<console>:14: error: not found: value spark import spark.implicits._ <console>:14: error: not found: value spark import spark.sql错误的解决办法(图文详解)
不多说,直接上干货! 最近,开始,进一步学习spark的最新版本.由原来经常使用的spark-1.6.1,现在来使用spark-2.2.0-bin-hadoop2.6.tgz. 前期博客 Spark ...
- innosetup区分正常状态和静默安装状态(通过传递的参数)
命令行运行程序,如: myprogram.exe /abc /bcd 如果我们想获取其中的参数,“/abc”.“/bcd” 1. 直接使用innosetup自带的方法, GetCmdTail() ...
- pat1013. Battle Over Cities (25)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- AtCoder Grand Contest 023 C - Painting Machines
Description 一个长度为 \(n\) 的序列,初始都为 \(0\),你需要求出一个长度为 \(n-1\) 的排列 \(P\), 按照 \(1\) 到 \(n\) 的顺序,每次把 \(P_i\ ...
- 深入理解JavaScript系列(25):设计模式之单例模式
介绍 从本章开始,我们会逐步介绍在JavaScript里使用的各种设计模式实现,在这里我不会过多地介绍模式本身的理论,而只会关注实现.OK,正式开始. 在传统开发工程师眼里,单例就是保证一个类只有一个 ...
- AndroidManifest.xml配置文件详解(转载)
AndroidManifest.xml配置文件详解 2013-01-05 10:25:23 分类: Android平台 AndroidManifest.xml配置文件对于Android应用开发来说是 ...
- goto语句和标签
goto 语句用于将执行流更改到标签处,虽然t-sql和pl/sql都提供了该语句,但是作为编程而言,我们不推荐使用此编程技术.要编写一个标签,应当在标识符后面加一个冒号.列如,下面示例使用goto语 ...
- [LeetCode]22. Generate Parentheses括号生成
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 前端参数统一校验工具类ValidParamUtils
1,前端参数不可信,对于后端开发人员来说应该是一条铁律,所以对于前端参数的校验,必不可少,而统一的前端参数校验工具,对我们进行参数校验起到事半功倍的效果 2,统一参数校验工具ValidParamUti ...