title: Why am I not able to get my Bean definition available in my Spring context with Spring Boot?
body: <p>I'm feeling stupid to ask this, but I can't understand where I'm wrong with my code.</p>
  
  <p>The context is :</p>
  
  <ul>
  <li>a Spring Boot application (1.5.7) with an embedded Jetty server and a
  controller to expose some endpoints </li>
  <li>a unique @Configuration class, where some of my beans are defined (Singleton and Prototype scopes)</li>
  <li>a @Service that uses some beans defined in my @Configuration class</li>
  </ul>
  
  <p>The problem is:</p>
  
  <ul>
  <li>a NoSuchBeanDefinitionException for one of my @Configuration bean.</li>
  </ul>
  
  <p>Now the details:</p>
  
  <p>My SpringBootApplication :</p>
  
  <pre><code>@SpringBootApplication
  public class HbbTVApplication {
      public static void main(String[] args) {
          SpringApplication.run(HbbTVApplication.class, args);
      }
  }
  </code></pre>
  
  <p>My @Configuration class:</p>
  
  <pre><code>@Configuration
  @Profile(value = { "dev", "int", "pre", "pro" })
  public class StandaloneFrontalConfig extends WebMvcConfigurerAdapter {
  
  @Value("${kafka.bootstrap-servers}")
  private String bootstrapServers;
  
  @Bean
  public Map&lt;String, Object&gt; producerConfigs() {
      Map&lt;String, Object&gt; props = new HashMap&lt;&gt;();
      props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
      props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
      props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
      return props;
  }
  
  @Bean
  public ProducerFactory&lt;String, String&gt; producerFactory() {
      return new DefaultKafkaProducerFactory&lt;&gt;(producerConfigs());
  }
  
  @Bean
  public KafkaTemplate&lt;String, String&gt; kafkaTemplate() {
      return new KafkaTemplate&lt;&gt;(producerFactory());
  }
  
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
      registry.addResourceHandler("/**").addResourceLocations("classpath:/standalone/");
  }
  
  @Bean
  public WebMvcConfigurer corsConfigurer() {
      return new WebMvcConfigurerAdapter() {
          @Override
          public void addCorsMappings(CorsRegistry registry) {
              registry.addMapping("/**").allowedOrigins("*").allowedHeaders("*");
          }
      };
  }
  
  @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
  @Bean
  public Security securityManager() {
      return new Security();
  }
  
  @Bean
  @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
  public KngAflow getTechnicalCookie() {
      return new KngAflow();
  }
  
  @Bean
  public EmbeddedServletContainerCustomizer customizer() {
      return new EmbeddedServletContainerCustomizer() {
  
          @Override
          public void customize(ConfigurableEmbeddedServletContainer container) {
              if (container instanceof JettyEmbeddedServletContainerFactory) {
                  customizeJetty((JettyEmbeddedServletContainerFactory) container);
              }
          }
  
          private void customizeJetty(JettyEmbeddedServletContainerFactory jetty) {
              jetty.addServerCustomizers(new JettyServerCustomizer() {
  
                  @Override
                  public void customize(Server server) {
                      for (Connector connector : server.getConnectors()) {
                          if (connector instanceof ServerConnector) {
                              HttpConnectionFactory connectionFactory = ((ServerConnector) connector)
                                      .getConnectionFactory(HttpConnectionFactory.class);
                              connectionFactory.getHttpConfiguration().setCookieCompliance(CookieCompliance.RFC2965);
                          }
                      }
                  }
              });
          }
      };
  }
  }
  </code></pre>
  
  <p>My @Service:</p>
  
  <pre><code>@Service
  public class CookieService implements services.CookieService, InitializingBean {
  
      /**
       * Serializable
       */
      private static final long serialVersionUID = -1997257884335775587L;
  
      @Autowired
      ApplicationContext app;
  
      @Override
      public Cookie createTechnicalCookie() {
          return new Cookie(app.getBean(KngAflow.class), null);
      }
  
      @Override
      public void afterPropertiesSet() throws Exception {
          if (app != null) {
              for (String bean : app.getBeanDefinitionNames()) {
                  System.out.println("Bean: " + bean);
              }
          }
      }
  }
  </code></pre>
  
  <p>And the "non defined" bean:</p>
  
  <pre><code>@JsonInclude(Include.NON_NULL)
  @JsonIgnoreProperties({ "security", "maxAge", "domain", "updated" })
  public class KngAflow implements Serializable, InitializingBean {
  
      @JsonProperty(value = "did")
      private String did;
      @JsonProperty(value = "checksum")
      private String checksum;
      @Autowired
      private Security security;
      private Integer maxAge;
      private String domain;
      private boolean updated = false;
  
      public KngAflow() {
          domain = ".mydomain.com";
      }
  
      @Override
      public void afterPropertiesSet() throws Exception {
          did = UUID.randomUUID().toString();
          maxAge = 365 * 24 * 60 * 60;
          checksum = security.encrypt(did + security.md5(did));
      }
  }
  </code></pre>
  
  <p>NB: Classes are not complete, and there are more classes in my project. I only put what I saw as relevant information.
  If something else is needed, just ask me please.
  By the way, all the endpoints are defined into a unique @Controller class, and all the endpoints are working except those needing the getTechCookie @Bean.</p>
  
  <p>So, my problem occurs in runtime execution. When I start my Spring Boot app, Jetty is started and listening on the configured port.
  Nevertheless, if you look at the CookieService @Service, I'm listing all the bean names defined in the autowired context and my getTechnicalCookie (a.k.a KngAflow) @Bean is missing. I can't understand why.</p>
  
  <p>Of course, when I invoke my @controller to execute my @Service code, the NoSuchBeanDefinitionException is thrown executing the line app.getBean(KngAflow.class).</p>
  
  <p>I tried to use a bean name instead of bean type, no change.
  For testing purpose (as it doesn't make sense from a logical point of view), I defined my bean getTechCookie @Bean  as a Singleton scoped bean, and the name is still missing from the ApplicationContext.</p>
  
  <p>And the last but not least thing is: Everything works fine with Eclipse!</p>
  
  <p>I mean, all my devs are done using Eclipse IDE. My Spring Boot app is built with Maven and executing it inside Eclipse works correctly (and my getTechCookie Bean is defined and listed). </p>
  
  <p>When I package my app using the Maven Spring Boot plugin and execute it using java -jar, my getTechCookie (KngAflow.class) bean is missing. Nevertheless, this class is present inside the jar.</p>
  
  <p>Spring parameters to launch the spring boot app are spring default values (port 8080, no SSL, ...) and the active.profiles are always between dev, int, pre or pro (those defined in my @Configuration class)</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Thanks!</p>
  
  <p>If it helps, I add my POM definition:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;artifactId&gt;my-app&lt;/artifactId&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;com.mydomain.bigdata&lt;/groupId&gt;
          &lt;artifactId&gt;mybigapp&lt;/artifactId&gt;
          &lt;version&gt;1.1-SNAPSHOT&lt;/version&gt;
      &lt;/parent&gt;
  
      &lt;dependencies&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-jetty&lt;/artifactId&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;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
              &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.kafka&lt;/groupId&gt;
              &lt;artifactId&gt;spring-kafka&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-configuration-processor&lt;/artifactId&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;resources&gt;
              &lt;resource&gt;
                  &lt;directory&gt;${basedir}/src/main/resources&lt;/directory&gt;
                  &lt;filtering&gt;true&lt;/filtering&gt;
                  &lt;includes&gt;
                      &lt;include&gt;**/*&lt;/include&gt;
                      &lt;include&gt;application.yml&lt;/include&gt;
                  &lt;/includes&gt;
              &lt;/resource&gt;
          &lt;/resources&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;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p><strong><em>EDIT:</em></strong> I changed my @Service class to "force" spring to accept my class as a prototype bean, and it works. It's very ugly but it works. But if someone could help me to find what's wrong, I don't like this workaround:</p>
  
  <pre><code>@Override
      public void afterPropertiesSet() throws Exception {
          if (!context.containsBeanDefinition(KngAflow.class.getName()))
              context.registerBeanDefinition(KngAflow.class.getName(),
                      BeanDefinitionBuilder.genericBeanDefinition(KngAflow.class).setScope("prototype").getBeanDefinition());
      }
  </code></pre>