我在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
My 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);
答案 0 :(得分:2)
更改
NewsServiceImpl service = context.getBean(NewsServiceImpl.class);
到
NewsService service = context.getBean(NewsService.class);
您NewServiceImpl
注明了@Transactional
,因此默认情况下,spring会创建一个代理,当然会实现NewsService
而不是NewsServiceImpl
。