【发布时间】:2017-12-23 06:34:13
【问题描述】:
我在 Spring MVC 中有一个错误。
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);
【问题讨论】:
-
你的
ProjectConfig好吗? -
永远不要对实现进行编码 - 对接口进行编码,因此不要在 impl 上使用 DI,而是使用接口。
标签: java spring hibernate spring-mvc spring-boot