Spring@Aspect切⾯参数传递
Spring @Aspect切⾯参数传递:
Xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="/2001/XMLSchema-instance" xmlns="/schema/beans" xmlns:p="/schema/p"
xmlns:util="/schema/util" xmlns:context="/schema/context" xmlns:aop="/schema/aop"    xsi:schemaLocation="/schema/aop /schema/aop/spring-aop-4.1.xsd
/schema/beans /schema/beans/spring-beans-3.0.xsd
/schema/util /schema/util/spring-util-4.1.xsd
/schema/context /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 t.ApplicationContext;
import t.support.ClassPathXmlApplicationContext;
public class AppBeans12 {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("l");
// 在AOP情况下,如果有接⼝就必须⽤接⼝来接,否则会报ClassCastException;
Thinker bean = (Thinker) Bean("volunteer");
bean.thinkOfSomething("volunteer think of something");
MindReader bean2 = (MindReader) Bean("magician");
String thoughts = Thoughts();
System.out.println(thoughts);
}
spring framework表达式assign}
切⾯:
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;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。