title: Modular @Configuration/@Bean in Spring Boot
body: <p>I'm making a MicroServices based project so I have more the one Spring Boot projects in my workspace. I need to configure restOperations in some of then but I want to configure once for all the project that needs. So I'm trying to add my <code>@Configuration</code> class to a jar and import in each MS projects.</p>
  <p>The problem is, when I execute the MS project in my server, I receive this error:</p>
  <pre><code>***************************
  APPLICATION FAILED TO START
  ***************************
  
  Description:
  
  Field restOperations in com.epavanellio.base.business.controller.BusinessController required a bean of type 'org.springframework.web.client.RestOperations' that could not be found.
  
  The injection point has the following annotations:
      - @org.springframework.beans.factory.annotation.Autowired(required=true)
  
  
  Action:
  
  Consider defining a bean of type 'org.springframework.web.client.RestOperations' in your configuration.
  </code></pre>
  <p>Here I have my Rest configuration class:</p>
  <pre><code>package com.epavanellio.base.restConfig;
  import org.apache.http.impl.client.CloseableHttpClient;
  import org.apache.http.impl.client.HttpClientBuilder;
  import org.apache.http.impl.client.LaxRedirectStrategy;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.http.client.ClientHttpRequestFactory;
  import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
  import org.springframework.stereotype.Component;
  import org.springframework.web.client.RestOperations;
  import org.springframework.web.client.RestTemplate;
  
  //@Component
  @Configuration
  public class SimpleRestConfiguration {
      
      final CloseableHttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy())
              .build();
      
      @Bean
      public RestOperations createRestTemplate(final ClientHttpRequestFactory clientHttpRequestFactory){
          return new RestTemplate(clientHttpRequestFactory);
      }
      
      @Bean
      public ClientHttpRequestFactory createHttpRequestFactory (@Value(&quot;${rest.connect.timeout}&quot;) final int connectTimeout,
              @Value(&quot;${rest.read.timeout}&quot;) final int readTimeout) {
          
          HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
          clientHttpRequestFactory.setConnectTimeout(connectTimeout);
          clientHttpRequestFactory.setReadTimeout(readTimeout);
          clientHttpRequestFactory.setHttpClient(httpClient);
          
          return clientHttpRequestFactory;
      }
      
  }
  </code></pre>
  <p>I imported the <code>.jar (dpdc-rest)</code> with has the <code>SimpleRestConfiguration</code> class in my MS project POM:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.3.4.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;groupId&gt;com.epavanellio.base&lt;/groupId&gt;
      &lt;artifactId&gt;ms-manager-business&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;ms-manager-business&lt;/name&gt;
      &lt;description&gt;Validate business logic. A microservice based project. &lt;/description&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      
      &lt;properties&gt;
          &lt;java.version&gt;11&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          
          &lt;dependency&gt;
              &lt;groupId&gt;com.epavanellio.base&lt;/groupId&gt;
              &lt;artifactId&gt;domain&lt;/artifactId&gt;
              &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
          &lt;/dependency&gt;
          
          &lt;dependency&gt;
              &lt;groupId&gt;com.epavanellio.base&lt;/groupId&gt;
              &lt;artifactId&gt;dpdc-rest&lt;/artifactId&gt;
              &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
          &lt;/dependency&gt;
          
          &lt;dependency&gt;
              &lt;groupId&gt;com.epavanellio.base&lt;/groupId&gt;
              &lt;artifactId&gt;dpdc-custom-exception-handler&lt;/artifactId&gt;
              &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
          &lt;/dependency&gt;
          
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate.validator&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
          &lt;/dependency&gt;
          
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate.validator&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-validator-annotation-processor&lt;/artifactId&gt;
          &lt;/dependency&gt;
          
          &lt;dependency&gt;
              &lt;groupId&gt;javax.el&lt;/groupId&gt;
              &lt;artifactId&gt;javax.el-api&lt;/artifactId&gt;
              &lt;version&gt;3.0.0&lt;/version&gt;
          &lt;/dependency&gt;
       
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.web&lt;/groupId&gt;
              &lt;artifactId&gt;javax.el&lt;/artifactId&gt;
              &lt;version&gt;2.2.6&lt;/version&gt;
          &lt;/dependency&gt;
          
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
                      &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;finalName&gt;ms-manager-business&lt;/finalName&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  <p>and in my MS application class is like this:</p>
  <pre><code>package com.epavanellio.base.business;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.boot.autoconfigure.domain.EntityScan;
  import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
  import org.springframework.boot.builder.SpringApplicationBuilder;
  import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Import;
  import com.epavanellio.base.restConfig.SimpleRestConfiguration;
  
  
  //@SpringBootApplication(scanBasePackages={&quot;com.epavanellio.base&quot;, &quot;com.epavanellio.base.restConfig&quot;})
  //@Import(SimpleRestConfiguration.class)
  //@ComponentScan({&quot;com.epavanellio.base&quot;, &quot;com.epavanellio.base.restConfig&quot;})
  @ComponentScan(&quot;com.epavanellio.base&quot;)
  @EntityScan(&quot;com.epavanellio.base.domain&quot;)
  @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
  @SpringBootApplication
  public class BusinessApplication extends SpringBootServletInitializer{
      
  
      public static void main(String[] args) {
          SpringApplication.run(BusinessApplication.class, args);
      }
      
       @Override
       protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(BusinessApplication.class);
       }
  }
  </code></pre>
  <p>as you can see commented, I already tried to my make my runtime &quot;see&quot; my configuration class in diffrent ways:
  First I tried to add <code>(scanBasePackages={&quot;com.epavanellio.base&quot;, &quot;com.epavanellio.base.restConfig&quot;})</code> after my annotation <code>@SpringBootApplication</code>, but the same error occurs. Then I tried to add specifically the <code>SimpleRestConfiguration</code> class package to the <code>@ComponentScan</code> annotation(for this, I uncommented the <code>@Component</code> annotation in <code>SimpleRestConfiguration</code> class), but the same error occurs. At least I tried to use <code>@Import</code>, but in this case I receive the error:</p>
  <pre><code>java.io.FileNotFoundException: class path resource [com/epavanellio/base/restConfig/SimpleRestConfiguration.class] cannot be opened because it does not exist 
  </code></pre>
  <p>does any one know how can I make my application class to &quot;see&quot; my
  <code>@Configuration</code> class?</p>