Announcement Announcement Module
Collapse
No announcement yet.
Bean not found if classes are in a JAR in WEB-INF/lib Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bean not found if classes are in a JAR in WEB-INF/lib

    I have a simple Maven + Spring + JPA app.

    In the business Maven module:

    Code:
    @Service
    public class OrderManagerImpl implements OrderManager {
    	
    	@Autowired
    	private OrderDao orderDao;
    
    ...................
    In the Web Maven module:

    Code:
    @Controller
    @RequestMapping("/order")
    public class OrderController {
    	
    	@Autowired
    	private OrderManager orderManager;
    
    ..................
    If the classes of the business module are packed in a JAR that is put in WEB-INF/lib, I get:

    Code:
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [service.OrderManager] 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)}
    If I move the classes to the WEB-INF/classes directory, it works fine.

    Can anyone please explain why and what can I do so that I can deploy the classes inside a JAR ?

    Thank you.

  • #2
    Hi

    Are you testing in Eclipse or after fully packaging your application with "mvn package"? If it's in Eclipse then please check if the JARs are really published to server.
    Also if you forget to declare <packaging /> element in your service Maven module and you have parent POM, then you will get <packaging>pom</packaging> in your "JAR" which will contain not your classes but src/main/java/... folder .

    regards
    Grzegorz Grzybek

    Comment


    • #3
      I am using SpringSource Tool Suite (Eclipse + Spring Tools) to develop and deploy.
      I got the following projects:

      parent

      Code:
      	<groupId>com.j</groupId>
      	<artifactId>j</artifactId>
      	<version>0.0.1-SNAPSHOT</version>
      	<packaging>pom</packaging>
      	<name>J Master</name>
      
      	...
      
      	<modules>
      		<module>j.business</module>
      		<module>j.web</module>
      	</modules>
      web

      Code:
      	<parent>
      		<artifactId>j</artifactId>
      		<groupId>com.j</groupId>
      		<version>0.0.1-SNAPSHOT</version>
      	</parent>
      	<groupId>com.j</groupId>
      	<artifactId>j.web</artifactId>
      	<version>0.0.1-SNAPSHOT</version>
      	<packaging>war</packaging>
      	<dependencies>
      		<dependency>
      			<artifactId>j.business</artifactId>
      			<groupId>com.j</groupId>
      			<version>0.0.1-SNAPSHOT</version>
      		</dependency>
      	</dependencies>
      business

      Code:
      	<parent>
      		<artifactId>j</artifactId>
      		<groupId>com.j</groupId>
      		<version>0.0.1-SNAPSHOT</version>
      	</parent>
      	<groupId>com.j</groupId>
      	<artifactId>j.business</artifactId>
      	<version>0.0.1-SNAPSHOT</version>
      	<packaging>jar</packaging>

      The JAR is deployed in webapps/j.web/WEB-INF/lib

      However, it's contents are a bit bizarre. When opening it, I see it contains the following:

      Code:
      \com (yes, including the backslash in the name of the directory)
      com
      main
      META-INF
      test
      application-context.xml
      Any idea what causes this to happen ?
      Any help is much appreciated.

      Thank you.

      Comment


      • #4
        What versions of m2e and m2e-wtp do you use?
        I have:
        - org.eclipse.m2e.feature_1.0.100.201108040103
        - org.maven.ide.eclipse.wtp.feature_0.13.1.20110728-1800
        - com.springsource.sts_2.8.0.201108101000-M1

        Comment


        • #5
          I have STS version 2.7.1 (the current version):
          http://www.springsource.com/downloads/sts

          I just downloaded the 2.8.0.M1 and I get the same.

          I suspect this is a Maven issue (the structure of the JAR), but the question still remains: since the classes are in the JAR, why are they not found ? I can clean up the JAR (remove the things that have no business there), the problem stays. But the JAR seems to be read, because I have the persistence.xml in there and the application-context.xml and they're loaded. It's just the service classes that the server seems to be unable to find in the JAR.

          Comment


          • #6
            Hi

            Once I also had problems with badly structured JARs. After standard cleans/refreshes/deletes of projects/reimporting them after deleting [.classpath|.project|.settings/] files the problem went away.
            What m2e versions do you use?

            Comment


            • #7
              Hi,

              I managed to find the problem. The issue was in the Business project -> Properties -> Deployment Assembly. Somehow that got messed up and instead of packaging /src/main/java and /src/main/resources it was packaging those + /src. I suppose the workspace got corrupted at a certain point. Now that I fixed it, everything seems to be working fine. I guess you had a similar issue, from what you're describing.

              Thanks for the help.
              Regards,
              C

              Comment

              Working...
              X