没有定义类型为[service.NewsServiceImpl]的限定bean

时间:2017-12-23 06:34:13

标签: java spring hibernate spring-mvc spring-boot

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

1 个答案:

答案 0 :(得分:2)

更改

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

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

NewServiceImpl注明了@Transactional,因此默认情况下,spring会创建一个代理,当然会实现NewsService而不是NewsServiceImpl