Spring AOP切面的时候参数的传递
Spring AOP切面的时候参数的传递
Xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<bean id="volunteer" class="com.stono.sprtest2.Volunteer"></bean>
<bean id="magician" class="com.stono.sprtest2.Magician"></bean>
<bean id="singer" class="com.stono.sprtest2.Singer"></bean>
<aop:config>
<aop:aspect ref="magician">
<aop:pointcut expression="execution(* com.stono.sprtest2.Thinker.thinkOfSomething(String)) and args(thoughts)" id="thinking" />
<aop:before method="interceptThoughts" pointcut-ref="thinking" arg-names="thoughts" />
</aop:aspect>
</aop:config>
</beans>
AppBean:
package com.stono.sprtest2; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppBeans10 {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("appbeans10.xml");
// 在AOP情况下,如果有接口就必须用接口来接,否则会报ClassCastException;
Thinker bean = (Thinker) context.getBean("volunteer");
bean.thinkOfSomething("volunteer think of something");
MindReader bean2 = (MindReader) context.getBean("magician");
String thoughts = bean2.getThoughts();
System.out.println(thoughts);
// 没有接口的Bean还是可以用类的;
Singer bean3 = (Singer) context.getBean("singer");
bean3.perform();
}
}
POJO:
package com.stono.sprtest2; public interface Thinker {
void thinkOfSomething(String thoughts);
}
package com.stono.sprtest2; public class Volunteer implements Thinker {
private String thoughts;
@Override
public void thinkOfSomething(String thoughts) {
this.thoughts = thoughts;
}
public String getThoughts() {
return thoughts;
}
}
AOP:
package com.stono.sprtest2; public interface MindReader {
void interceptThoughts(String thoughts);
String getThoughts();
}
package com.stono.sprtest2; public class Magician implements MindReader {
private String thoughts;
@Override
public void interceptThoughts(String thoughts) {
System.out.println("Intercepting volunteer's thoughts");
this.thoughts = thoughts;
}
@Override
public String getThoughts() {
return thoughts;
}
}
Spring AOP切面的时候参数的传递的更多相关文章
- Spring AOP 切面编程记录日志和接口执行时间
最近客户现在提出系统访问非常慢,需要优化提升访问速度,在排查了nginx.tomcat内存和服务器负载之后,判断是数据库查询速度慢,进一步排查发现是因为部分视图和表查询特别慢导致了整个系统的响应时间特 ...
- spring AOP(切面) 表达式介绍
在 spring AOP(切面) 例子基础上对表达式进行介绍 1.添加接口删除方法 2.接口实现类 UserDaoServer 添加实现接口删除方法 3.测试类调用delUser方法 4. 输出结果截 ...
- 使用Spring AOP切面解决数据库读写分离
http://blog.jobbole.com/103496/ 为了减轻数据库的压力,一般会使用数据库主从(master/slave)的方式,但是这种方式会给应用程序带来一定的麻烦,比如说,应用程序如 ...
- 利用Spring AOP切面对用户访问进行监控
开发系统时往往需要考虑记录用户访问系统查询了那些数据.进行了什么操作,尤其是访问重要的数据和执行重要的操作的时候将数记录下来尤显的有意义.有了这些用户行为数据,事后可以以用户为条件对用户在系统的访问和 ...
- spring aop 利用JoinPoint获取参数的值和方法名称
AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点 ...
- Spring AOP 切面编程的方法
spring aop的使用分为两种,一种是使用注解来实现,一种是使用配置文件来实现. 先来简单的介绍一下这两种方法的实现,接下来详细的介绍各处的知识点便于查阅.目录如下: 1.基于注解实现spring ...
- 使用Spring AOP预处理Controller的参数
实际编程中,可能会有这样一种情况,前台传过来的参数,我们需要一定的处理才能使用,比如有这样一个Controller @Controller public class MatchOddsControll ...
- Spring AOP 切面实现操作日志
创建接口注解日志类 package com.fh.service.logAop; /** * Created by caozengling on 2018/7/21. */ import java.l ...
- Spring aop切面插入事物回滚
<!-- tx标签配置 事物--> <tx:advice id="txadvice" transaction-manager="transactionM ...
随机推荐
- Allegro pcb -等长设计
1.首先注意打开的Allegro PCB是哪个产品控件,如下图,若打开的是Allegro PCB Designer,在后面,看别人的讲解过程中会找不到“SiXplorer”,原因 就是出在这里,All ...
- 如何将CAD文件导入到Protel和PADS中
一. 如何把CAD中的板框图纸导入到Protel中? a. 在CAD中单位设置为“毫米”,并做简单的处理,板框图是有合并还是分解都无所谓,另存为R12(*dxf)格式文件. b. 打开DXP,新建PC ...
- Python之路:线程池
版本一 #!/usr/bin/env python # --*--coding:utf-8 --*-- import Queue import threading class ThreadPool( ...
- Android 各层中日志打印功能的应用
1. HAL层 头文件:#include <utils/Log.h> 对应的级别 打印方法 VERBOSE LOGV()DEBUG LOGD()INFO LOGI()WARN LOGW() ...
- w3school之JavaScript学习笔记
在前端测试过程中,少不了听到开发说到JS,JS在webJavaScript 是浏览器脚本语言(简称JS),主要用来向HTML页面添加交互行为. 学习网址:http://www.w3school.com ...
- jQuery Ajax 二次封装
jQuery Ajax通用js封装 第一步:引入jQuery库 <script type="text/javascript" src="<%=path%> ...
- Mybatis 示例之 Association - 偶尔记一下 - 博客频道 - CSDN.NET
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- spring加载过程中jar包加载不了,解决方法
当我们在开发spring项目时,一般会将jar包放到webInf/lib下,这样是myeclipse自动将jar包加载到tomcat中webapps下,但是当我们新建一个lib文件夹的情况下,我们ad ...
- svn第一篇----入门指南
摘要:trunk存放的是主代码,不修改,branch,tag,milestone均是从trunk中衍生的.branch复制trunk中代码用于开发,tag用于存放比较重要的发行版,存放release版 ...
- 为HTTP分类作序
作者:zccst 曾经,认为对HTTP已经非常熟悉了,觉得不需要学习什么,知道2014年春天,让我感觉到自己是如此的无知. 举例: 1,对HTTP头部信息知道多少?每一个字段分别都可以取哪些值,每一个 ...