hossein badrnezhad hossein badrnezhad - 7 months ago 66
Java Question

No qualifying bean of type [service.NewsServiceImpl] is defined

I have an error in 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 Code is :

@Repository
public class NewsDAImpl implements NewsDA {

@PersistenceContext
private EntityManager context;

@Override
public News ... Some Other Codes


My NewsServiceImpl class :

@Service
@Transactional
public class NewsServiceImpl implements NewsService{

@Autowired
private NewsDAImpl newsDa;

@Override
public News ... Some Other Codes


I write controller that has static void main, just for testing.
in that i wrote this :

ApplicationContext context = new AnnotationConfigApplicationContext(ProjectConfig.class);


then i just get news service with getBean method :

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

Answer Source

Change

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

to

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

You have NewServiceImpl annotated with @Transactional, so by default spring will create a proxy which of course implements NewsService instead of NewsServiceImpl.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download