我正在学习Craig Walls的“Spring in Action第四版”。我试图将建议应用于接口声明的方法,我得到了Exception。当我将相同的建议应用于没有实现任何内容的类时,一切正常。
Spring版本 - 4.3.2
帮助将不胜感激。
例外:
Exception in thread "main"org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.fifczan.bean.UserService] is defined
代码:
接口:
package com.fifczan.bean;
public interface Service {
void doTask();
}
实现:
package com.fifczan.bean;
import org.springframework.stereotype.Component;
@Component
public class UserService implements Service {
public void doTask() {
System.out.println("doing task");
}
}
方面:
package com.fifczan;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class UserAspect {
//If i change Service(interface) to UserService(implementation)
//in pointcut I am getting the same exception
@Before("execution(* com.fifczan.bean.Service.doTask(..))")
public void userAdvice(){
System.out.println("doing sth before method doTask");
}
}
配置:
package com.fifczan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class AspectJAutoProxyConfig {
}
主要:
package com.fifczan;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.fifczan.bean.UserService;
public class AspectJAutoProxyTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AspectJAutoProxyConfig.class);
UserService userService= ctx.getBean(UserService.class);
userService.doTask();
}
}
答案 0 :(得分:0)
你要的是一个UserService
的bean,它是具体的类,而不是接口。检索或注入Service
类型的bean。