20

I have a Spring Boot application (Y) which relies upon a set of Library files packed as x.jar and mentioned as a dependency in the pom.xml of the application Y.

x.jar has a bean named (User.java) Application Y has a java class named (Department.java)

While i try to Autowire an instance of User.java inside Department.java, i get the following error

Can't I @Autowire a Bean which is present in a dependent Library Jar ?

Could not autowire field: private com.User user; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.User] 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)}

No qualifying bean of type [com.User] 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)}**

Here is the code in the Spring Boot Application 'Y'

package myapp;

@Component
public class Department {

    @Autowired
    private com.User user;

    //has getter setters for user

}

Here is the code of User.java in the Library x.jar

 package com;

@Component
@ConfigurationProperties(prefix = "test.userproperties")
public class User {

  private String name;
  //has getter setters for name    
}

This is the dependency entry for x.jar in the pom.xml of application Y

      <groupId>com.Lib</groupId>
      <artifactId>x</artifactId>
      <version>001</version>
    </dependency>   

This is the Main class in the Application 'Y'

@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableZuulProxy
@EnableGemfireSession(maxInactiveIntervalInSeconds=60)
@EnableCircuitBreaker
@EnableHystrixDashboard
@EnableDiscoveryClient
public class ZuulApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(ZuulApplication.class).web(true).run(args);
    }
}   

Both Department and User are under different packages.

Solution: I applied the following 2 steps and now the Autowiring is working fine.

Step 1: Added the following class in the jar file

package com
@Configuration
@ComponentScan
public class XConfiguration {

}

Step 2: Imported this Configuration class in the Y project's main class

@Configuration
    @EnableAutoConfiguration
    @ComponentScan
    @EnableZuulProxy
    @EnableGemfireSession(maxInactiveIntervalInSeconds=60)
    @EnableCircuitBreaker
    @EnableHystrixDashboard
    @EnableDiscoveryClient
    @Import(XConfiguration.class) 
    public class ZuulApplication {

        public static void main(String[] args) {
            new SpringApplicationBuilder(ZuulApplication.class).web(true).run(args);
        }
    }
yathirigan
  • 5,619
  • 22
  • 66
  • 104
  • 2
    Show your application context please. – Jens Jun 12 '15 at 06:09
  • 1
    You probably don't have component scanning for the packages of that library – Wim Deblauwe Jun 12 '15 at 06:11
  • we have not used application context XML, only annotations. The application Y is a Spring Boot Application. – yathirigan Jun 12 '15 at 06:13
  • You say it's a "bean", but what does "bean" mean? It looks like it's just a POJO. How are you acquiring a reference to it now? – chrylis -cautiouslyoptimistic- Jun 12 '15 at 06:15
  • 2
    So, show us your configuration class. The one annotated with @Configuration. – JB Nizet Jun 12 '15 at 06:15
  • yes , it is just a Pojo. since i have mentioned the @Component annotation to the User pojo class, i assumed it is a bean which spring is aware of. am i correct ? we do not do appcontext.getBean() , if the User pojo class was within the Application 'Y' , just this same code was working fine. When i moved User to x.jar, it started throwing this error. – yathirigan Jun 12 '15 at 06:18
  • >>> component scanning for the packages of that library << - How and where do mention this ? – yathirigan Jun 12 '15 at 06:19
  • i have updated the Question with the main class from the Spring Boot application 'Y'. Only this Main class has the @ Configuration and @ ComponentScan annotations. None of the library files have these annotations because there wasn't a main class for the library. – yathirigan Jun 12 '15 at 06:22
  • I had the same issue. Nothing else would work. The `@Import` did it for me. – Ravi-A-Doer Feb 02 '17 at 17:02

1 Answers1

25

You would need to add the package names of your main class and the User class to be 100% sure, but more then likely, the User class is not in the same package (or a subpackage) of your main class. This means that component scanning will not pick it up.

You can force spring to look at other packages like this:

@ComponentScan(basePackages = {"org.example.main", "package.of.user.class"})
Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
  • your componentScan mention was the reason but, i have used a slightly different approach. updated the question with the final working solution . thank you. – yathirigan Jun 12 '15 at 07:29
  • Additionally, if you want to avoid importing/scanning the configuration class. You can create a folder called 'META-INF' in the 'resources' folder of your library and add a file called 'spring.factories' with the content org.springframework.boot.autoconfigure.EnableAutoConfiguration= . This will autoconfigure your library – Vishal Maral Jun 04 '21 at 07:07