I have four packages:
com.spring.org
Files: HomeController.java
com.spring.org.dao
Files: SubscriberDao.java
, SubscriberDaoImpl.java
com.spring.org.model
Files: Subscriber.java
com.spring.org.service
Files: SubscriberService.java
, SubscriberServiceImpl.java
I put all my controller classes in com.spring.org package and others in different packages based on its type. If I run my application I get this error message :
HTTP Status 500 - Servlet.init() for servlet appServlet threw exception No qualifying bean of type [com.spring.org.service.SubscriberService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.....
FYI: I am using autowired annoation in my Controller like following:
@Autowired
private SubscriberService subService;
But if I put all my classes and interfaces in com.spring.org package then my application works perfectly.
I have tried using these tags in my servlet-context.xml file to solve the problem, but still it did not work:
<annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.spring.org.**" />
<context:component-scan base-package="com.spring.org.dao" />
<context:component-scan base-package="com.spring.org.model" />
<context:component-scan base-package="com.spring.org.service" />
I also tried only this:
<context:component-scan base-package="com.spring.org" />
You can see the code of my servlet-context.xml file here http://postimg.org/image/s6bnjccrn/
Could you please tell me how to solve this problem ?
Please let me know if you need to see any other files.
My Code for SubscriberService :
@Service
public interface SubscriberService {
public void addSubscriber(Subscriber subscriber);
public void updateSubscriber(Subscriber subscriber);
public Subscriber getSubscriberById(int subId);
public List<Subscriber> listSubs();
public int removeSubscriber(int subId);
}
Root Cause
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.spring.service.SubscriberService com.spring.org.HomeController.subService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spring.service.SubscriberService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=)} org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
It would be very difficult to follow if I paste all my codes here, so I have upload my entire project here https://www.mediafire.com/?crxe7vt7uwyqwtl . I am using Eclipse IDE.
your structure should be like this
SubscriberService Interface
package com.spring.org.service;
public interface SubscriberService {
}
SubscriberServiceImpl.java
package com.spring.org.service;
@Component
@Qualifier("Subscriber")
public class SubscriberServiceImpl implements SubscriberService {
}
‘SubscriberServiceImpl1’ is a component and it implements ‘SubscriberService’.
SubscriberServiceImpl1.java
package com.spring.org.service;
@Component
@Qualifier("Subscriber1")
public class SubscriberServiceImpl1 implements SubscriberService {
}
I setup a Spring context that scans both of these packages for beans marked with ‘@Component’.
servlet-context.xml
<annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.spring"/>
HomeController.java
@Controller
public class HomeController {
@Autowired
@Qualifier("Subscriber")
private SubscriberService subService;
}
refer from this link. hope this will help you....
EDIT
as per your package structure your SubscriberServiceImpl class is under package com.spring.org.service just change your base package with com.spring this will work for you
<context:component-scan base-package="com.spring"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With