Java 未定义类型为[service.NewsServiceImpl]的符合条件的bean
我在SpringMVC中有一个错误Java 未定义类型为[service.NewsServiceImpl]的符合条件的bean,java,spring,hibernate,spring-mvc,spring-boot,Java,Spring,Hibernate,Spring Mvc,Spring Boot,我在SpringMVC中有一个错误 Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [service.NewsServiceImpl] is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getB
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [service.NewsServiceImpl] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:373)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:333)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1088)
at controller.Testing.main(Testing.java:24)
NewsDAImpl代码为:
@Repository
public class NewsDAImpl implements NewsDA {
@PersistenceContext
private EntityManager context;
@Override
public News ... Some Other Codes
我的NewsServiceImpl类:
@Service
@Transactional
public class NewsServiceImpl implements NewsService{
@Autowired
private NewsDAImpl newsDa;
@Override
public News ... Some Other Codes
我编写的控制器具有静态void main,只是为了测试。
在这篇文章中,我写道:
ApplicationContext context = new AnnotationConfigApplicationContext(ProjectConfig.class);
然后我用getBean方法获取新闻服务:
NewsServiceImpl service = context.getBean(NewsServiceImpl.class);
改变
到
您使用
@Transactional
注释了NewServiceImpl
,因此默认情况下spring将创建一个代理,该代理当然实现NewsService
,而不是NewsServiceImpl
您的ProjectConfig
如何?永远不要编码到实现-编码到接口,所以不要在impl上使用DI,而是使用接口。
NewsServiceImpl service = context.getBean(NewsServiceImpl.class);
NewsService service = context.getBean(NewsService.class);