awo*_*ine 6 java spring aspectj
我使用Spring配置AspectJ,并且在"捕获"从类外调用的公共方法时它工作正常.现在我想做这样的事情:
public class SomeLogic(){
public boolean someMethod(boolean test){
if(test){
return innerA();
} else {
return innerB();
}
}
private boolean innerA() {// some logic}
private boolean innerA() {// some other logic}
}
Run Code Online (Sandbox Code Playgroud)
SomeLogic是一个SpringBean.方法innerA()和innerB()可以声明为private或public - 从Struts动作调用someMethod()方法.是否有可能从AspectJ中捕获从someMethod()调用的方法innerA()或innerB()?
我的配置(基于XML):
<aop:aspect id="innerAAspect" ref="INNER_A">
<aop:pointcut id="innerAService" expression="execution(* some.package.SomeLogic.innerA(..))"/>
</aop:aspect>
<aop:aspect id="innerAAround" ref="INNER_A">
<aop:around pointcut-ref="innerAService" method="proceed"/>
</aop:aspect>
<aop:aspect id="innerBAspect" ref="INNER_B">
<aop:pointcut id="innerBService" expression="execution(* some.package.SomeLogic.innerB(..))"/>
</aop:aspect>
<aop:aspect id="innerBAround" ref="INNER_B">
<aop:around pointcut-ref="innerBService" method="proceed"/>
</aop:aspect>
Run Code Online (Sandbox Code Playgroud)
是的,使用AspectJ很容易捕获私有方法.
在所有私有方法之前打印句子的示例:
@Pointcut("execution(private * *(..))")
public void anyPrivateMethod() {}
@Before("anyPrivateMethod()")
public void beforePrivateMethod(JoinPoint jp) {
System.out.println("Before a private method...");
}
Run Code Online (Sandbox Code Playgroud)
如果您熟悉Eclipse,我建议使用STS开发AspectJ 或仅安装AJDT插件.
关于Spring AOP的功能的更多信息可以在Spring参考文档中找到这里.
| 归档时间: |
|
| 查看次数: |
19183 次 |
| 最近记录: |