sea*_*eal 5 spring ioc-container autowired
我遇到异常 -
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)
Run Code Online (Sandbox Code Playgroud)
这是我得到异常的班级。
@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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
前两个变量已正确注入,但 verdictService 未正确注入。这是我的接口VerdictService和VerdictServiceImpl类。
public interface VerdictService {
void post(Verdict verdict);
}
Run Code Online (Sandbox Code Playgroud)
==
@Service
@Transactional
public class VerdictServiceImpl implements VerdictService {
@Autowired
SessionFactory sessionFactory;
@Override
public void post(Verdict verdict) {
sessionFactory.getCurrentSession()
.save(verdict);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的配置类
@Configuration
@EnableScheduling
@ComponentScan(basePackages = "com.muztaba")
public class AppConfig {
}
Run Code Online (Sandbox Code Playgroud)
我还给出了我的项目目录结构。
我在这里缺少什么?谢谢。
你需要自动装配VerdictService
@Autowired
VerdictService verdictService;
Run Code Online (Sandbox Code Playgroud)
并且你需要省略该行
verdictService = context.getBean(VerdictServiceImpl.class);
Run Code Online (Sandbox Code Playgroud)
理想情况下,您的代码应该通过自动装配使用这些服务。
| 归档时间: |
|
| 查看次数: |
11693 次 |
| 最近记录: |