Spring @Aspect切面参数传递
Spring @Aspect切面参数传递:
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">
<!-- 这个声明会创建AnnotationAwareAspectJAutoProxyCreator,进行切面Bean的代理 -->
<aop:aspectj-autoproxy />
<!-- 必须将切面类声明为一个Bean -->
<bean id="magician" class="com.stono.sprtest3.Magician"></bean>
<bean id="volunteer" class="com.stono.sprtest3.Volunteer"></bean>
</beans>
AppBean:
package com.stono.sprtest3;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppBeans12 {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("appbeans12.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);
}
}
切面:
package com.stono.sprtest3;
public interface MindReader {
void interceptThoughts(String thoughts);
String getThoughts();
}
package com.stono.sprtest3;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class Magician implements MindReader {
@Pointcut("execution(* com.stono.sprtest3.Thinker.thinkOfSomething(String)) && args(thoughts)")
public void thinking(String thoughts) {
}
private String thoughts;
@Override
@Before("thinking(thoughts)")
public void interceptThoughts(String thoughts) {
System.out.println("Intercepting volunteer's thoughts");
this.thoughts = thoughts;
}
@Override
public String getThoughts() {
return thoughts;
}
}
POJO:
package com.stono.sprtest3;
public interface Thinker {
void thinkOfSomething(String thoughts);
}
package com.stono.sprtest3;
public class Volunteer implements Thinker {
private String thoughts;
@Override
public void thinkOfSomething(String thoughts) {
this.thoughts = thoughts;
}
public String getThoughts() {
return thoughts;
}
}
Spring @Aspect切面参数传递的更多相关文章
- Spring @Aspect实现切面编程
参考:http://blog.csdn.net/cdl2008sky/article/details/6268628 参考:http://www.360doc.com/content/12/0602/ ...
- [Spring] Aspect Oriented Programming with Spring | AOP | 切面 | 切点
使用Spring面向切面编程 1.介绍 AOP是OOP的补充,提供了另一种关于程序结构的思路. OOP的模块化的关键单位是 类 . AOP的则是aspect切面. AOP 将程序的逻辑分成独立的块(叫 ...
- Spring面向切面编程(AOP)
1 spring容器中bean特性 Spring容器的javabean对象默认是单例的. 通过在xml文件中,配置可以使用某些对象为多列. Spring容器中的javabean对象默认是立即加载(立即 ...
- Spring AOP切面
在软件开发中,分布于应用多出的功能被称为和横切关注点. 通常,这些横切关注点从概念上是与应用的业务逻辑相分离的(可是往往直接嵌入到应用的业务逻辑中).将这些横切关注点与业务逻辑相分离正是面向切面编成( ...
- Spring @Aspect进行类的接口扩展
Spring @Aspect进行类的接口扩展: XML: <?xml version="1.0" encoding="UTF-8"?> <be ...
- Spring AOP切面的时候参数的传递
Spring AOP切面的时候参数的传递 Xml: <?xml version="1.0" encoding="UTF-8"?> <beans ...
- Spring AOP 切面编程实战Demo项目
为什么会有此项目?在某日,我看博客时,看到了讲面向切面编程的内容,之前也知道spring是面向切面编程的,只是自己没有写过相关的代码,于是决定自己写一个test.但是url拦截器从外部看,和AOP有相 ...
- Spring AOP 切面编程记录日志和接口执行时间
最近客户现在提出系统访问非常慢,需要优化提升访问速度,在排查了nginx.tomcat内存和服务器负载之后,判断是数据库查询速度慢,进一步排查发现是因为部分视图和表查询特别慢导致了整个系统的响应时间特 ...
- 使用Spring AOP切面解决数据库读写分离
http://blog.jobbole.com/103496/ 为了减轻数据库的压力,一般会使用数据库主从(master/slave)的方式,但是这种方式会给应用程序带来一定的麻烦,比如说,应用程序如 ...
随机推荐
- 关于struts2的web.xml配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...
- phantom" breakpoints
http://stackoverflow.com/questions/723199/why-does-my-eclipse-project-have-phantom-debugger-breakpoi ...
- 关联规则算法---Eclat算法
Eclat算法 与fp-growth 和apriori算法不同,Eclat算法加入了倒排的思想,具体就是将事务数据中的项作为key,每个项对应的事务ID作为value. 原输入数据为 tid item ...
- mysqldump导入导出mysql数据库
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- iOS开源库--最全的整理
youtube下载神器:https://github.com/rg3/youtube-dl我擦咧 vim插件:https://github.com/Valloric/YouCompleteMevim插 ...
- eclipse中安装配置maven
1.首先说一下在windows中安装maven.非常简单... 到http://maven.apache.org/download.html中下载maven,截止笔者发文时,maven最新版本为mav ...
- Nutch是个不错的选择
对搜索引擎的理解:我们并没有google 的源代码,因此学习搜索引擎Nutch是个不错的选择.了解一个大型分布式的搜索引擎如何工作是一件让人很受益的事情.在写 Nutch的过程中,从学院派和工业派借鉴 ...
- linux中vi和vim编辑工具
linux中知名的还有emacs,功能比vim还要强大 vim 如果文件存在vim是打开这个文件,若果不存在,则先新建再打开 命令模式:任何模式都可以通过Esc回到命令模式,命令模式可以通过命令进行选 ...
- 51nod算法马拉松 contest7
A题 链接:http://www.51nod.com/contest/problem.html#!problemId=1417 推荐链接:http://blog.csdn.net/a837199685 ...
- php smarty
摘自:http://linux.chinaitlab.com/PHP/38324.html 刚开始接触模版引擎的 PHP 设计师,听到 Smarty 时,都会觉得很难.其实笔者也不例外,碰都不敢碰一 ...