ABC.java中定义了两种方法
public void method1(){
.........
method2();
...........
}
public void method2(){
...............
...............
}
Run Code Online (Sandbox Code Playgroud)
我想有AOP上的呼叫方法2.所以,我创建了一个类,AOPLogger.java,具有在方法提供方面功能的checkAccess
在配置文件中,我不喜欢的东西下面
<bean id="advice" class="p.AOPLogger" />
<aop:config>
<aop:pointcut id="abc" expression="execution(*p.ABC.method2(..))" />
<aop:aspect id="service" ref="advice">
<aop:before pointcut-ref="abc" method="checkAccess" />
</aop:aspect>
</aop:config>
Run Code Online (Sandbox Code Playgroud)
但是当调用我的method2时,不会调用AOP功能,即不会调用checkAccess方法的AOPLogger类.
我错过了什么?
有可能在Spring中获取给定对象的代理吗?我需要调用子类的函数.但是,显然,当我直接打电话时,方面不适用.这是一个例子:
public class Parent {
public doSomething() {
Parent proxyOfMe = Spring.getProxyOfMe(this); // (please)
Method method = this.class.getMethod("sayHello");
method.invoke(proxyOfMe);
}
}
public class Child extends Parent {
@Secured("president")
public void sayHello() {
System.out.println("Hello Mr. President");
}
}
Run Code Online (Sandbox Code Playgroud)
我找到了实现这一目标的方法.它有效,但我认为不是很优雅:
public class Parent implements BeanNameAware {
@Autowired private ApplicationContext applicationContext;
private String beanName; // Getter
public doSomething() {
Parent proxyOfMe = applicationContext.getBean(beanName, Parent.class);
Method method = this.class.getMethod("sayHello");
method.invoke(proxyOfMe);
}
}
Run Code Online (Sandbox Code Playgroud)