spring的面向切面实现的两种方式
面向切面:主要应用在日志记录方面。实现业务与日志记录分离开发。
spring面向切面有两种实现方式:1、注解 2、xml配置。
1、注解实现如下:
(1)配置如下:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<context:annotation-config/>
<aop:aspectj-autoproxy/>
<bean id="Psv" class="com.test.spring.AOP.Personservice"/>
<bean id="testAop" class="com.test.spring.AOP.testAop"/>
</beans>
注意:红色部分的配置
(2)切入实现的代码如下:
@Aspect
public class testAop { //指明所要代理的类或方法
@Pointcut("execution(* com.test.spring.AOP.Personservice.*(..))")
public void pointCut(){} @After("pointCut()")
public void after(){
System.out.println("after");
} @Before("pointCut()")
public void before(){
//JoinPoint joinPoint
System.out.println("before");
} @Around("pointCut()")
public Object around(ProceedingJoinPoint joinpoint){
//joinpoint.getArgs()能够得到传入到方法的参数
Object valu = null
try {
System.out.println("aaaaaaaa");
valu = joinpoint.proceed();
//返回所代理的方法(有返回值的方法)返回值
System.out.println(valu); } catch (Throwable e) {
e.printStackTrace();
}
return valu;
} @AfterReturning("pointCut()")
public void afteReturning(){
System.out.println("afteReturning");
} @AfterThrowing("pointCut()")
public void afterThrowing(){
System.out.println("afterThrowing");
}
}
2、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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<context:annotation-config/> <bean id="Psv" class="com.test.spring.AOP.Personservice"/>
<bean id="testAop" class="com.test.spring.AOP.testAop"/>
<aop:config>
<aop:aspect id="Pservice" ref="testAop">
<!-- 配置指定切入的对象 -->
<aop:pointcut id="point_cut" expression="execution(* execution(* frame.com.Action.login.*(..)))" />
<!-- 只匹配add方法作为切入点
<aop:pointcut id="except_add" expression="execution(* com.test.spring.AOP.*.addPerson(..))" />
-->
<!-- 前置通知 -->
<aop:before method="before" pointcut-ref="point_cut" />
<!-- 后置通知 returning指定返回参数 -->
<aop:after-returning method="after" pointcut-ref="point_cut" returning="result" />
<!-- 环绕通知 --> <aop:around method="around" pointcut-ref="point_cut"/>
<aop:after-throwing method="doThrow" pointcut-ref="point_cut" throwing="e"/>
</aop:aspect>
</aop:config>
</beans>
spring的面向切面实现的两种方式的更多相关文章
- 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& ...
- Spring系列之AOP实现的两种方式
AOP常用的实现方式有两种,一种是采用声明的方式来实现(基于XML),一种是采用注解的方式来实现(基于AspectJ). 首先复习下AOP中一些比较重要的概念: Joinpoint(连接点):程序执行 ...
- 在Spring整合aspectj实现aop的两种方式
-----------------------------基于XML配置方案目标对象接口1 public interface IUserService { public void add(); pub ...
- Spring使用JMS传递消息的两种方式
方式一:同步收发消息,使用JMS template 消费者阻塞等待消息的到来. 方式二:异步收发消息,使用message listener container 消费者提供一个listener,注册一个 ...
- spring boot中读取配置文件的两种方式
application.properties test.name=测试 test.url=www.test.com 1.@Value注解 在controller里可以这样直接调用 @Value(&qu ...
- Spring集成Quartz框架的两种方式。
可参考:https://blog.csdn.net/yk614294861/article/details/84324603 1.使用Spring与Quarta配置作业得两种方式: a.方式一,Met ...
随机推荐
- 2. Mysql数据库的入门知识
2. Mysql数据库的入门知识 (1)打开Windows系统提供的服务查看相应的服务. (2)在Windows任务管理器的进程中查看 (3)使用命令行管理windows的Mysql数据库服务. Ne ...
- 如何把py文件打包成exe可执行文件
如何把py文件打包成exe可执行文件 1.安装 pip install pyinstaller 或者 pip install -i https://pypi.douban.com/simple pyi ...
- df
hdu 1052 Tian Ji -- The Horse Racing (2011-08-26 08:32:51) 转载▼ 标签: 杂谈 分类: acm杂谈 Tian Ji -- The Horse ...
- The Salt Master has cached the public key报错解决办法
参考:http://www.52devops.com/chuck/814.html 查看salt-minion的运行状态,显示salt-master已经缓存了这个minion,但是minion在重新认 ...
- checkbox做全部选中,全部取消效果
批量选中.取消多个checkbox的用法很简单,这个功能也很常用.这里做个总结. 在HTML中的写法: <div id="ftpFileDownTr"> <but ...
- 在powerDesigner中通过SQL生成pdm
在项目需求分析的阶段,通常需要画数据库表的pdm图.有时候会直接画pdm来设计表,有时候是通过其他方式,如用纸和笔去画……当数据库中的表已经建立好了,怎么把数据库中的表导成SQL形式,然后生成pdm图 ...
- python中的argv使用
sys.argv[]说白了就是一个从程序外部获取参数的桥梁,这个“外部”很关键,所以那些试图从代码来说明它作用的解释一直没看明白.因为我们从外部取得的参数可以是多个,所以获得的是一个列表(list), ...
- WebStorm中配置node.js(Windows)
WebStorm中配置node.js(Windows) 一.node 1.下载安装包 32 位 : https://nodejs.org/dist/v4.4.3/node-v4.4.3-x86.msi ...
- angularjs 在 iframe 里面无法正确显示 src 指定的内容
原 controller : $scope.myURL = URL; 页面: <iframe ng-src='{{myURL}}' class="width-100 height-10 ...
- Python 正则表达式(分组)
正则表达式分组 分组就是用一对圆括号"()"括起来的正则表达式,匹配出的内容就表示一个分组.从正则表达式的左边开始看,看到的第一个左括号"("表示第一个分组,第 ...