我得到例外 -
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.muztaba.service.VerdictServiceImpl] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:372)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1066)
at com.muztaba.service.App.task(App.java:35)
at com.muztaba.service.App.main(App.java:28)
这是我从那里获得异常的课程。
@Component
public class App {
QueueService<Submission> queue;
Compiler compiler;
VerdictService verdictService;
public static void main( String[] args ) {
new App().task();
}
private void task() {
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
queue = context.getBean(QueueImpl.class);
compiler = context.getBean(CompilerImpl.class);
verdictService = context.getBean(VerdictServiceImpl.class); //here the exception thrown.
while (true) {
if (!queue.isEmpty()) {
Submission submission = queue.get();
compiler.submit(submission);
}
}
}
}
前两个变量正确注入,但verdictService不是。
这是我的VerdictService
和VerdictServiceImpl
接口和类。
public interface VerdictService {
void post(Verdict verdict);
}
==
@Service
@Transactional
public class VerdictServiceImpl implements VerdictService {
@Autowired
SessionFactory sessionFactory;
@Override
public void post(Verdict verdict) {
sessionFactory.getCurrentSession()
.save(verdict);
}
}
这是我的配置类
@Configuration
@EnableScheduling
@ComponentScan(basePackages = "com.muztaba")
public class AppConfig {
}
我还提供了我的项目目录结构。
我在这里缺少什么?谢谢。
答案 0 :(得分:1)
您需要自动装配VerdictService
@Autowired
VerdictService verdictService;
你需要省略
行verdictService = context.getBean(VerdictServiceImpl.class);
理想情况下,您的代码应通过自动装配使用服务。
答案 1 :(得分:1)
似乎你已经用@transactional注释了它,Spring正在创建一个基于JDK接口的代理。 所以spring管理bean'VerdictService'而不是'VerdictServiceImpl'。
应该是
verdictService = context.getBean(verdictService.class);
而不是
verdictService = context.getBean(VerdictServiceImpl.class);