在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= ...
随机推荐
- RBAC基于角色的权限访问控制
RBAC是什么,能解决什么难题?ThinkPHP中RBAC实现体系安全拦截器认证管理器访问决策管理运行身份管理器ThinkPHP中RBAC认证流程权限管理的具体实现过程RBAC相关的数据库介绍Th ...
- 牛客网Java刷题知识点之TCP、UDP、TCP和UDP的区别、socket、TCP编程的客户端一般步骤、TCP编程的服务器端一般步骤、UDP编程的客户端一般步骤、UDP编程的服务器端一般步骤
福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 Java全栈大联盟 ...
- Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还是有区别的
转:http://www.cnblogs.com/2gua/ Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还是有区别的: 1)静态方法无需传入self参数,类成员方法 ...
- TOJ 3488 Game Dice
描述 In the game of Dungeons & Dragons, players often roll multi-sided dice to generate random num ...
- pat02-线性结构4. Pop Sequence (25)
02-线性结构4. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- web前端与后台数据访问的对象封装
前言:通常情况下,在不使用angularJS/nodeJS/react等这类完整性的解决方案的js时,前端与后台的异步交互都是使用Ajax技术进行解决 一:作为java web开发工程师可能以下代码是 ...
- react native 完美解决启动白屏
先讲下我的RN版本0.58.5 首先安装react-native-splash-screen(目前使用的版本是3.2.0) 项目地址https://github.com/crazycodeboy/re ...
- CSS Hack兼容
CSS中有很多标签在不同浏览器中有不同的兼容性问题,问了让网页的功能更好的不同浏览器中显示正常, 需要通过hack的方式来解决浏览器兼容问题. CSS hack问题由来已久,目前我的了解甚少,以下是转 ...
- Html5的localStorage与sessionStorage五种循序渐进的使用方法
需求:本地记录用户上次输入的内容 使用关键技术:localStorage 第一步:使用jQuery的普通写法 1.JS代码 // 获取window的localStorage对象 var localS ...
- C++基础--static的用法
首先,看看变量的存储: int global ; int main() { int stackStore ; int heapStore* = (int *)malloc(sizeof(int)); ...