Home > Enterprise >  can't inject the values in the class from configuration created in gradle.properties
can't inject the values in the class from configuration created in gradle.properties

Time:10-21

the question is i want to use "Mapping Configuration to objects" in quarkus i have a gradle.properties so i want to declare this array of objects in the .properties file myApplicationConfig.enabled = true and i want to fetch and use this value we can do that by using @ConfigMapping(prefix = myApplicationConfig) as the annotation on top of the interface for mapping it my interface code:

    @ConfigMapping(prefix = "myApplicationConfig")
    @StaticInitSafe
    public interface ApplicationConfig {
        boolean enabled();
        List<Details> details();

          interface details{
            String name();
            int age();}

and after injecting into this enabled() i want to fetch it to other class there i want to do some operations such as printing the value so i've injected this interface there and trying to get the value but it says that

      ERROR [io.qua.run.boo.StartupActionImpl] (Quarkus Main Thread) Error running Quarkus: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at io.quarkus.runner.bootstrap.StartupActionImpl$1.run(StartupActionImpl.java:103)
        at java.base/java.lang.Thread.run(Thread.java:833)
    Caused by: java.lang.ExceptionInInitializerError
        at io.quarkus.runner.ApplicationImpl.<clinit>(Unknown Source)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
        at java.base/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:128)
        at java.base/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:347)
        at java.base/java.lang.Class.newInstance(Class.java:645)
    Caused by: java.lang.ExceptionInInitializerError
    
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:66)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:41)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:120)
        at io.quarkus.runner.GeneratedMain.main(Unknown Source)
        ... 6 more
    Caused by: java.lang.IllegalStateException: io.smallrye.config.ConfigValidationException: Configuration validation failed:
        java.util.NoSuchElementException: SRCFG00014: The config property 
Config.enabled is required but it could not be found in any config source
    Caused by: java.lang.IllegalStateException: io.smallrye.config.ConfigValidationException: Configuration validation failed:
    
        at io.smallrye.config.SmallRyeConfigBuilder.build(SmallRyeConfigBuilder.java:453)
        at io.quarkus.runtime.generated.Config.<clinit>(Unknown Source)
        ... 18 more
    Caused by: io.smallrye.config.ConfigValidationException: Configuration validation failed:
        java.util.NoSuchElementException: SRCFG00014: The config property Config.enabled is required but it could not be found in any config source
        at io.smallrye.config.ConfigMappingProvider.mapConfiguration(ConfigMappingProvider.java:805)
        at io.smallrye.config.ConfigMappingProvider.mapConfiguration(ConfigMappingProvider.java:761)
        at io.smallrye.config.SmallRyeConfigBuilder.build(SmallRyeConfigBuilder.java:450)
        ... 19 more
    
    Caused by: io.smallrye.config.ConfigValidationException: Configuration validation failed:
    
    2022-10-21 11:47:11,847 INFO  [io.qua.dep.dev.IsolatedDevModeMain] (main) Attempting to start live reload endpoint to recover from previous Quarkus startup failure
    2022-10-21 11:47:11,900 INFO  [org.jbo.threads] (main) JBoss Threads version 3.4.2.Final
    2022-10-21 11:47:11,963 ERROR [io.qua.dep.dev.IsolatedDevModeMain] (main) Failed to start quarkus: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at io.quarkus.dev.appstate.ApplicationStateNotification.waitForApplicationStart(ApplicationStateNotification.java:51)
        at io.quarkus.runner.bootstrap.StartupActionImpl.runMainClass(StartupActionImpl.java:122)
        at io.quarkus.deployment.dev.IsolatedDevModeMain.firstStart(IsolatedDevModeMain.java:146)
        at io.quarkus.deployment.dev.IsolatedDevModeMain.accept(IsolatedDevModeMain.java:480)
        at io.quarkus.deployment.dev.IsolatedDevModeMain.accept(IsolatedDevModeMain.java:67)
        at io.quarkus.bootstrap.app.CuratedApplication.runInCl(CuratedApplication.java:142)
        at io.quarkus.bootstrap.app.CuratedApplication.runInAugmentClassLoader(CuratedApplication.java:97)
        at io.quarkus.deployment.dev.DevModeMain.start(DevModeMain.java:132)
        at io.quarkus.deployment.dev.DevModeMain.main(DevModeMain.java:62)
        Suppressed: java.lang.NoClassDefFoundError: Could not initialize class io.quarkus.runtime.generated.Config
            at java.base/java.lang.Class.forName0(Native Method)
            at java.base/java.lang.Class.forName(Class.java:467)
            at io.quarkus.runner.bootstrap.StartupActionImpl.runMainClass(StartupActionImpl.java:144)
            ... 7 more
    Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    Caused by: java.lang.reflect.InvocationTargetException
    
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at io.quarkus.runner.bootstrap.StartupActionImpl$1.run(StartupActionImpl.java:103)
        at java.base/java.lang.Thread.run(Thread.java:833)
    Caused by: java.lang.ExceptionInInitializerError
    Caused by: java.lang.ExceptionInInitializerError
    
        at io.quarkus.runner.ApplicationImpl.<clinit>(Unknown Source)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
        at java.base/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:128)
        at java.base/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:347)
        at java.base/java.lang.Class.newInstance(Class.java:645)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:66)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:41)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:120)
        at io.quarkus.runner.GeneratedMain.main(Unknown Source)
        ... 6 more
    Caused by: java.lang.IllegalStateException: io.smallrye.config.ConfigValidationException: Configuration validation failed:
        java.util.NoSuchElementException: SRCFG00014: The config property Config.enabled is required but it could not be found in any config source
        at io.smallrye.config.SmallRyeConfigBuilder.build(SmallRyeConfigBuilder.java:453)
        at io.quarkus.runtime.generated.Config.<clinit>(Unknown Source)
        ... 18 more
    Caused by: io.smallrye.config.ConfigValidationException: Configuration validation failed:
        java.util.NoSuchElementException: SRCFG00014: The config property Config.enabled is required but it could not be found in any config source
        at io.smallrye.config.ConfigMappingProvider.mapConfiguration(ConfigMappingProvider.java:805)
        at io.smallrye.config.ConfigMappingProvider.mapConfiguration(ConfigMappingProvider.java:761)
        at io.smallrye.config.SmallRyeConfigBuilder.build(SmallRyeConfigBuilder.java:450)
        ... 19 more
    
    Caused by: java.lang.IllegalStateException: io.smallrye.config.ConfigValidationException: Configuration validation failed:
    
    Caused by: io.smallrye.config.ConfigValidationException: Configuration validation failed:

i'm unable to inject the data can anyone explain why i can't? or did i miss anything? or should i need to add these configs in build.gradle or somewhere else ? and also i want to declare an array of objects as

Config.details[0].name = "Sam"
Config.details[0].age = 12
Config.details[1].name = "Tim"
Config.details[1].age = "34"

but when i try to use them after injecting it throws empty list please explain it :)

CodePudding user response:

Documentation

1. Config Sources By default, Quarkus reads configuration properties from multiple sources (by descending ordinal):

(400) System properties

(300) Environment variables

(295) .env file in the current working directory

(260) Quarkus Application configuration file in $PWD/config/application.properties

(250) Quarkus Application configuration file application.properties in classpath

(100) MicroProfile Config configuration file META-INF/microprofile-config.properties in classpath

The final configuration is the aggregation of the properties defined by all these sources. A configuration property lookup starts by the highest ordinal configuration source available and works it way down to other sources until a match is found. This means that any configuration property may override a value just by setting a different value in a higher ordinal config source. For example, a property configured using an environment property overrides the value provided using the application.properties file.

gradle is used to build the project not to provide by default any properties to quarkus.

So pick one of the above destinations and there add your configuration properties which will be picked then from quarkus

For example you can add

myApplicationConfig.enabled = true

myApplicationConfig.details[0].name = "Sam"
myApplicationConfig.details[0].age = 12
myApplicationConfig.details[1].name = "Tim"
myApplicationConfig.details[1].age = "34"

in the file src/main/resources/application.properties.

  • Related