我得到了异常(exception) -

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) 

这是我到那里的类(class)异常(exception)。
@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); 
        } 
    } 
} 
} 

前两个变量正确注入(inject),但 verdictService 没有。
这是我的 VerdictServiceVerdictServiceImpl接口(interface)和类。
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 { 
 
} 

我还给出了我的项目目录结构。



我在这里想念什么?谢谢你。

请您参考如下方法:

您需要自动接线 VerdictService

@Autowired 
VerdictService verdictService; 

你需要省略这一行
verdictService = context.getBean(VerdictServiceImpl.class); 

理想情况下,您的代码应该通过 Autowiring 使用服务。


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!