gpt4 book ai didi

java - 未定义 [service.NewsServiceImpl] 类型的合格 bean

转载 作者:行者123 更新时间:2023-11-30 02:17:18 25 4
gpt4 key购买 nike

我在 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

我编写了具有 static void main 的 Controller ,仅用于测试。我写了这个:

ApplicationContext context = new AnnotationConfigApplicationContext(ProjectConfig.class);

然后我就用 getBean 方法获取新闻服务:

NewsServiceImpl service = context.getBean(NewsServiceImpl.class);

最佳答案

改变

NewsServiceImpl service = context.getBean(NewsServiceImpl.class);

NewsService service = context.getBean(NewsService.class);

你有 NewServiceImpl@Transactional 注解,所以默认情况下 spring 会创建一个代理,它当然实现 NewsService 而不是 NewsServiceImpl.

关于java - 未定义 [service.NewsServiceImpl] 类型的合格 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47950356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com