Home:ALL Converter>NoSuchBeanException while defining immutable configuration in Micronaut

NoSuchBeanException while defining immutable configuration in Micronaut

Ask Time:2022-11-16T00:21:46         Author:Hari Krishna

Json Formatter

I am trying to define an immutable configuration using an interface annotated with @ConfigurationProperties. But when I try to ran the applicaiton, I am getting below error.

Exception in thread "main" io.micronaut.context.exceptions.NoSuchBeanException: No bean of type [com.sample.app.configuration.AppConfig] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
    at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2805)
    at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1617)
    at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:867)
    at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:859)
    at com.sample.app.App.main(App.java:13)

application.yml

my:
  app:
    name: chat server
    version: 1.23.45

AppConfig class

import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.core.bind.annotation.Bindable;

import jakarta.annotation.Nonnull;

@ConfigurationProperties("my.app")
public interface AppConfig {

    @Bindable(defaultValue = "my chat server")
    @Nonnull
    String getName();

    @Bindable(defaultValue = "1.0.0")
    String getVersion();

    @Bindable(defaultValue = "chat server send and receive messages")
    String getDescription();

}

pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sample.app</groupId>
  <artifactId>micronaut-app</artifactId>
  <version>1</version>

  <parent>
    <groupId>io.micronaut</groupId>
    <artifactId>micronaut-parent</artifactId>
    <version>3.7.3</version>
  </parent>


  <properties>
    <maven.compiler.target>15</maven.compiler.target>
    <maven.compiler.source>15</maven.compiler.source>
  </properties>

  <dependencies>

    <dependency>
      <groupId>io.micronaut</groupId>
      <artifactId>micronaut-inject-java</artifactId>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <annotationProcessorPaths>
            <path>
              <groupId>io.micronaut</groupId>
              <artifactId>micronaut-inject-java</artifactId>
            </path>
          </annotationProcessorPaths>
        </configuration>
      </plugin>

      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>com.sample.app.App</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>

        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>


    </plugins>
  </build>
</project>

References https://docs.micronaut.io/latest/guide/#immutableConfig

Author:Hari Krishna,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74448959/nosuchbeanexception-while-defining-immutable-configuration-in-micronaut
yy