madhu
madhu

Reputation: 43

Class in JAR file not Autowired

Problem Statement

The class from a JAR file is not getting autowired. Spring version used is 3.0

Classes Involved : InventoryController > ProductService > ProductManager > ProductDao

Stacktrace

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.site.dao.ProductDao] 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:949)
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)

Modules

InventoryController is a class in the admin webapp which depends on ProductService from site-service-1.0.jar (ProductService and ProductManager is in site-service-1.0.jar).

ProductService depends on ProductManager which in turn depends on ProductDao which is in site-dao-1.0.jar (ProductDao is in site-dao-1.0.jar. At runtime the ProductDao is not getting autowired).

The SimpleProductManager references ProductDao, but at runtime its not getting loaded. The stacktrace is defined above Need help/suggestions on what could be the issue

Classes involved in this dependencies

InventoryController > ProductService > ProductManager > ProductDao

web.xml

<servlet> 
<servlet-name>dispatcherServlet</servlet-name> 
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-clas‌​s>
 <init-param> <param-name>contextConfigLocation</param-name> <param-    
  value>classpath*:/**/*spring-context.xml,classpath*:com/**/*spring-conte‌​xt.xml</param-    
  value> </init-param>
 <load-on-startup>1</load-on-startup>
 </servlet> 

 <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> 
 <url-pattern>/admin/*</url-pattern> 
 </servlet-mapping> 

Upvotes: 3

Views: 6267

Answers (3)

Hardik Gajjar
Hardik Gajjar

Reputation: 1024

in Your service Implementation you should define service name in annotation i hope so it will be help full

@Service("UserService")
public class UserServiceImpl implements UserService

in your code you see below code

ProductService

@Service("ProductService")
public class ProductService {
.....

Upvotes: 2

madhu
madhu

Reputation: 43

I found the issue which was causing this. The dao context was defined in file dao-spring-service-context.xml which was not in accordance with the contextConfigLocation defined in web.xml

<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/**/*spring-context.xml,classpath*:com/**/*spring-context.xml
</param-value>

Thanks all for the help and guidance

Upvotes: 1

Surendra Poranki
Surendra Poranki

Reputation: 399

Are you missing @Component on your ProductDao

something like this

@Component public class ProductDao { .............. }

Upvotes: 0

Related Questions