title: Can't I @Autowire a Bean which is present in a dependent Library Jar?
body: <p>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.</p>
  
  <p>x.jar has a bean named (User.java)
  Application Y has a java class named (Department.java)</p>
  
  <p>While i try to Autowire an instance of User.java inside Department.java, i get the following error</p>
  
  <p><strong><em>Can't I @Autowire a Bean which is present in a dependent Library Jar ?</em></strong></p>
  
  <blockquote>
    <p>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)}</p>
    
    <p>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)}**</p>
  </blockquote>
  
  <p>Here is the code in the Spring Boot Application 'Y'</p>
  
  <pre><code>package myapp;
  
  @Component
  public class Department {
  
      @Autowired
      private com.User user;
  
      //has getter setters for user
  
  }
  </code></pre>
  
  <p>Here is the code of User.java in the Library x.jar</p>
  
  <pre><code> package com;
  
  @Component
  @ConfigurationProperties(prefix = "test.userproperties")
  public class User {
  
    private String name;
    //has getter setters for name    
  }
  </code></pre>
  
  <p>This is the dependency entry for x.jar in the pom.xml of application Y</p>
  
  <pre><code>      &lt;groupId&gt;com.Lib&lt;/groupId&gt;
        &lt;artifactId&gt;x&lt;/artifactId&gt;
        &lt;version&gt;001&lt;/version&gt;
      &lt;/dependency&gt;   
  </code></pre>
  
  <p>This is the Main class in the Application 'Y'</p>
  
  <pre><code>@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);
      }
  }   
  </code></pre>
  
  <p>Both Department and User are under different packages.</p>
  
  <p><strong><em>Solution: I applied the following 2 steps and now the Autowiring is working fine.</em></strong></p>
  
  <p>Step 1: Added the following class in the jar file</p>
  
  <pre><code>package com
  @Configuration
  @ComponentScan
  public class XConfiguration {
  
  }
  </code></pre>
  
  <p>Step 2: Imported this Configuration class in the Y project's main class</p>
  
  <pre><code>@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);
          }
      }
  </code></pre>