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切面的时候参数的传递的更多相关文章

  1. Spring AOP 切面编程记录日志和接口执行时间

    最近客户现在提出系统访问非常慢,需要优化提升访问速度,在排查了nginx.tomcat内存和服务器负载之后,判断是数据库查询速度慢,进一步排查发现是因为部分视图和表查询特别慢导致了整个系统的响应时间特 ...

  2. spring AOP(切面) 表达式介绍

    在 spring AOP(切面) 例子基础上对表达式进行介绍 1.添加接口删除方法 2.接口实现类 UserDaoServer 添加实现接口删除方法 3.测试类调用delUser方法 4. 输出结果截 ...

  3. 使用Spring AOP切面解决数据库读写分离

    http://blog.jobbole.com/103496/ 为了减轻数据库的压力,一般会使用数据库主从(master/slave)的方式,但是这种方式会给应用程序带来一定的麻烦,比如说,应用程序如 ...

  4. 利用Spring AOP切面对用户访问进行监控

    开发系统时往往需要考虑记录用户访问系统查询了那些数据.进行了什么操作,尤其是访问重要的数据和执行重要的操作的时候将数记录下来尤显的有意义.有了这些用户行为数据,事后可以以用户为条件对用户在系统的访问和 ...

  5. spring aop 利用JoinPoint获取参数的值和方法名称

    AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点 ...

  6. Spring AOP 切面编程的方法

    spring aop的使用分为两种,一种是使用注解来实现,一种是使用配置文件来实现. 先来简单的介绍一下这两种方法的实现,接下来详细的介绍各处的知识点便于查阅.目录如下: 1.基于注解实现spring ...

  7. 使用Spring AOP预处理Controller的参数

    实际编程中,可能会有这样一种情况,前台传过来的参数,我们需要一定的处理才能使用,比如有这样一个Controller @Controller public class MatchOddsControll ...

  8. Spring AOP 切面实现操作日志

    创建接口注解日志类 package com.fh.service.logAop; /** * Created by caozengling on 2018/7/21. */ import java.l ...

  9. Spring aop切面插入事物回滚

    <!-- tx标签配置 事物--> <tx:advice id="txadvice" transaction-manager="transactionM ...

随机推荐

  1. android usb挂载分析

    http://blog.csdn.net/new_abc/article/details/7409018

  2. vi编辑器 :x与:wq的区别

    按一下ESC键,之后 :wq保存和退出VI [vi是Unix/Linux系统最常用的编辑器之一,我习惯使用":x"命令来保存文件并退出,不愿意使用":wq"命令 ...

  3. Android学习笔记之Intent(2)

    打开网页 package com.jiahemeikang.helloandroid; import java.io.File; import com.jiahemikang.service.Echo ...

  4. Android学习笔记之Service

    与服务通信 用bindservice 而startservice并无通信条件. service 为android为系统服务,所以程序员无法new出来,只能建立服务,共其他组件使用. package c ...

  5. PAT (Advanced Level) 1082. Read Number in Chinese (25)

    模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  6. Ext4.1 , #Ext4.2

    在类中定义url 和 在参数中定义url不同 Ext4.1 和 Ext4.2 处理上有较大的区别

  7. php 依赖注入容器

    原文: http://blog.csdn.net/realghost/article/details/35212285 https://my.oschina.net/cxz001/blog/53316 ...

  8. DIV+CSS 常见问题及解决办法整理

    http://blog.shaogroup.com/divcss-%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98%E5%8F%8A%E8%A7%A3%E5%86%B3%E5% ...

  9. 配置 Gitblit 进行 Git 代码管理

    配置 Gitblit 进行 Git 代码管理 环境 CentOS 7 x64 IP: 10.6.0.2 首先需要安装jdk  安装步骤 就略过了 下载最新版本  gitblit wget http:/ ...

  10. 一个不应该犯的错octave

    今天在完成Andrew NG的机器学习神经网络作业,在实现花费函数的时候,没有使用循环,直接向量计算.前面都想的挺好的,很快就想到了如何使用向量来计算,可是在扩展y的时候,犯了一个超级傻的错误. y是 ...