title: How to autowire a component in a dependency/external jar in Spring?
body: <p>I have a Spring Boot project and I can't get components from an <strong>external jar</strong> to be autowired. When I try to, I got a <code>org.springframework.beans.factory.NoSuchBeanDefinitionException</code> saying that can't find a bean with that name available.</p>
  
  <p>I tried some solutions found in similar questions, like these ones:</p>
  
  <p><a href="https://stackoverflow.com/questions/26451192/how-to-autowire-service-from-external-jar-in-spring">How to autowire @service from external Jar in Spring</a></p>
  
  <p><a href="https://stackoverflow.com/questions/34367316/spring-boot-autowired-does-not-work-classes-in-different-package">Spring Boot @autowired does not work, classes in different package</a></p>
  
  <p><a href="https://stackoverflow.com/questions/29571304/how-can-i-autowire-a-spring-bean-that-was-created-from-an-external-jar">How can I @Autowire a spring bean that was created from an external jar?</a></p>
  
  <p>..but still can't managed it to work.</p>
  
  <p>Here is an example of what I'm trying to accomplish:</p>
  
  <p><a href="https://i.stack.imgur.com/LKvO0.png" rel="noreferrer"><img src="https://i.stack.imgur.com/LKvO0.png" alt="enter image description here"></a></p>
  
  <p>Here is <strong>boot class</strong> in the Spring Boot project <strong>spring-project-example</strong></p>
  
  <pre><code>package com.springdi.example;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.ConfigurableApplicationContext;
  import org.springframework.context.annotation.ComponentScan;
  
  import com.dependency.example.DependencyBasePackageClass;
  import com.dependency.example.somepackage.SomeBean;
  
  @SpringBootApplication
  @ComponentScan(basePackages = {"com.springdi.example"}, basePackageClasses = DependencyBasePackageClass.class)
  public class SpringProjectExampleApplication {
  
      public static void main(String[] args) {
          ConfigurableApplicationContext context = SpringApplication.run(SpringProjectExampleApplication.class, args);
  
          String beanName = SomeBean.class.getName();
          System.out.printf("%s can be autowired: %s\n", beanName, String.valueOf(context.containsBean(beanName)).toUpperCase());
      }
  }
  </code></pre>
  
  <p>It's just a simple Spring Boot project checking if it is possible to autowire a component present in the dependency jar.</p>
  
  <p>Here is the <strong>component</strong> in the <strong>jar</strong> (dependency-example-1.0.0.jar)</p>
  
  <pre><code>package com.dependency.example.somepackage;
  
  import org.springframework.stereotype.Component;
  
  @Component
  public class SomeBean {
  
      public void someMethod() {
          System.out.println("Some process...");
      }
  }
  </code></pre>
  
  <p>And here is the <strong>base package class</strong> of this same <strong>jar</strong></p>
  
  <pre><code>package com.dependency.example;
  
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  
  /**
   * Just a class to serve as the root for component
   * scanning in "com.dependency.example" and its sub-packages
   */
  @Configuration
  @ComponentScan
  public class DependencyBasePackageClass {
  
  }
  </code></pre>
  
  <p>I've already tried <code>@Import(DependencyBasePackageClass.class)</code> in SpringProjectExampleApplication and <code>@ComponentScan</code> with <code>basePackages</code> and <code>basePackageClasses</code>, but no success.</p>
  
  <p>I also tried using <code>@SpringBootApplication(scanBasePackageClasses = {SpringProjectExampleApplication.class, DependencyBasePackageClass.class})</code></p>
  
  <p>and the not type safe <code>@SpringBootApplication(scanBasePackages = {"com.springdi.example", "com.dependency.example"})</code>.</p>
  
  <p><code>@Configuration @ComponentScan({"com.dependency.example"})</code> also fails, <code>context.containsBean("com.dependency.example.somepackage.SomeBean")</code> still returns <strong>false</strong>.</p>
  
  <p>This jar is included in classpath and in the pom.xml as a dependency</p>
  
  <pre><code>&lt;dependencies&gt;
      &lt;!-- other dependencies --&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;com.rbaggio&lt;/groupId&gt;
          &lt;artifactId&gt;dependency-example&lt;/artifactId&gt;
          &lt;version&gt;1.0.0&lt;/version&gt;
          &lt;scope&gt;system&lt;/scope&gt;
          &lt;systemPath&gt;${basedir}/lib/dependency-example-1.0.0.jar&lt;/systemPath&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>Could it be the location of the jar, the way it is included or some extra configuration needed?</p>
  
  <p>I'd appreciate any help! Thanks in advance.</p>