在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= ...
随机推荐
- jacob自己动生成word文档目录
任务目的 1自动生成word文档目录. 用例测试操作步骤 在一个word文档的第二页填写占位符: {目录}保存.调用程序读取目标文档,自动根据标题生成目录到{目录}位置. 效果 关键代码 insert ...
- (转)shell--read命令的选项及用法
shell--read命令 原文:https://www.cnblogs.com/lottu/p/3962921.html http://blog.csdn.net/skdkjzz/article/d ...
- Struts2中Action对象的set方法和get方法调用规则
Struts的Action是采用的是多实例多线程设计,而不是像Servlet那样采用单实例多线程设计,因此在struts中,一个请求就对应一个Action对象,个对象之间的数据相互之间互不干扰.没接到 ...
- 机器学习——GBDT
基础概念 GBDT(Gradient Boosting Decision Tree) 全称梯度提升决策树,是一种迭代的决策树算法.GBDT是集成学习Boosting的家族成员,GBDT中的树是回归树, ...
- 在 Flask 应用中使用 gevent
在 Flask 应用中使用 gevent 普通的 flask 应用 通常在用 python 开发 Flask web 应用时,使用 Flask 自带的调试模式能够给开发带来极大便利.Flask 自带的 ...
- 深入理解JavaScript系列(16):闭包(Closures)
介绍 本章我们将介绍在JavaScript里大家经常来讨论的话题 —— 闭包(closure).闭包其实大家都已经谈烂了.尽管如此,这里还是要试着从理论角度来讨论下闭包,看看ECMAScript中的闭 ...
- Halcon学习笔记——机器视觉应用工程开发思路及相机标定
机器视觉应用工程开发思路 机器视觉应用工程主要可划分为两大部分,硬件部分和软件部分. 1.硬件部分,硬件的选型至关重要,决定了后续工作是否可以正常开展,其中关键硬件部分包括:光源,相机以及镜头. 2. ...
- 00字体图标iconfont的制作与使用--阿里矢量图库
一.iconfont的使用范围 在工作当中,经常会用到嵌在元素里的小图标 在这种情况下,如果使用<img>标签或者用作背景图片,也能实现这种效果.但是如果这么做的话,就必须把图片一个个切下 ...
- Java使用POI操作Excel文件
1.简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式文件读和写的功能. 2.依赖的jar包 <!-- ex ...
- 【IOS】IOS综合
取精华.去糟粕!适合iOS开发者的15大网站推荐 E:/IOS视频: IOS基础/01 苹果开发零基础教程 3780m --> 63h /02 IOS开发快速入门教程 /03 IOS开发进阶教 ...