我是方面的新手,这是我的第二个问题。我有一个运行超过3个类的Aspect:A是抽象的,B和C都从A继承。现在,当我有一个实现接口Testable时,Spring并没有加载我的上下文。它抛出NoSuchBeanDefinitionException:
No matching bean of type [com.test.test.impl.A] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
尽管如此,如果我对这些建议发表评论,它仍然可行。
界面可测试 :
package com.test.test;
public interface Testable {
public void doStuff();
}
测试
public class AppTest {
@Autowired
@Qualifier("a1")
private A a1;
@Autowired
private A a2;
@Autowired
@Qualifier("b")
private B b;
@Autowired
@Qualifier("c")
private C c;
@Test
public void test() {
a1.doStuff();
a2.doStuff();
a1.doMoreStuff();
a2.doMoreStuff();
b.doStuff();
b.doMoreStuff();
c.doStuff();
c.doMoreStuff();
}
}
A类 :
package com.test.test.impl;
import com.test.test.Testable;
public abstract class A implements Testable{
public abstract void doMoreStuff();
public void doStuff(){
System.err.println("Inside A.doStuff()");
}
}
B类
package com.test.test.impl;
public class B extends A{
@Override
public void doMoreStuff() {
System.err.println("Inside B.doMoreStuff()");
}
}
C类
package com.test.test.impl;
public class C extends A{
@Override
public void doMoreStuff() {
System.err.println("Inside C.doMoreStuff()");
}
}
方面
package com.test.test.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Around("execution(* com.test.test..*.*(..))")
public Object miAspecto(ProceedingJoinPoint joinPoint) throws Throwable{
System.err.println("");
System.err.println("Before the method runs");
System.err.println("Invoking " + joinPoint.getSignature());
Object o = joinPoint.proceed();
System.err.println("After the method runs");
System.err.println("Result " + o);
System.err.println("");
return o;
}
}
它的问题是什么?
提前致谢。
PS:为什么有些时候方面中的所有线都打印在方法本身的那一行之前?
答案 0 :(得分:1)
要完成这项工作,您需要在基于java的配置的proxyTargetClass=true
等配置中添加@EnableAspectJAutoProxy(proxyTargetClass=true)
,或者为基于xml的配置添加<aop:config proxy-target-class="true"></aop:config>
。这样spring aop会强有力地添加代理。