@ConfigurationProperties prefix not working

前端 未结 4 1270
南方客
南方客 2020-12-09 07:50

.yml file

cassandra:
    keyspaceApp:junit
solr:
    keyspaceApp:xyz

Bean

@Component
@ConfigurationProperties(prefix=\"ca         


        
相关标签:
4条回答
  • 2020-12-09 08:26

    It looks like you are trying to use Spring Boot Typesafe Configuration Properties feature.

    So in order to have it working correctly, you have to add a few changes to your code:

    First of all, your CommonDataApplication class should have @EnableConfigurationProperties annotation e.g.

    @EnableAutoConfiguration
    @ComponentScan
    @PropertySource("application.yml")
    @EnableConfigurationProperties
    public class CommonDataApplication {
    
        public static void main(String[] args) {
            // ...
        }
    }
    

    I do not believe you need @PropertySource("application.yml") annotation as application.yml (as well as application.properties and application.xml) is a default configuration file used by Spring Boot.

    Your CassandraClientNew class does not need to have @Value annotation prefixing keyspaceApp property. And your keyspaceApp has to have a setter method.

    @Component
    @ConfigurationProperties(prefix="cassandra")
    public class CassandraClientNew {
    
       private String keyspaceApp;
    
       public void setKeyspaceApp(final String keyspaceApp) {
           this.keyspaceApp = keyspaceApp;
       }
    }
    

    BTW, if you are using List's or Sets and you initialise collections (e.g. List<String> values = new ArrayList<>();), then only getter is required. If a collection is not initialised then you need to provide a setter method too (otherwise an exception will be thrown).

    I hope that will help.

    0 讨论(0)
  • 2020-12-09 08:36

    General answer

    1. In your properties file (application.properties or application.yml)

    # In application.yaml
    a:
      b:
        c: some_string
    

    2. Declare your class:

    @Component
    @ConfigurationProperties(prefix = "a", ignoreUnknownFiels = false)
    public class MyClassA {
    
      public MyClassB theB;   // This name actually does not mean anything
                              // It can be anything      
      public void setTheB(MyClassB theB) {
        this.theB = theB;
      }
    
      public static MyClassB {
    
        public String theC;
    
        public void setTheC(String theC) {
          this.theC = theC;
        }
    
      }
    
    }
    

    3. Declare public setters! And this is crucial!

    Make sure to have these public methods declared in the above classes. Make sure they have "public" modifier.

    // In MyClassA
    public void setTheB(MyClassB theB) {
      this.theB = theB;
    }
    
    // In MyClassB
    public void setTheC(String theC) {
      this.theC = theC;
    }
    

    That's it.

    Final notes

    • The property names in your classes do not mean anything to Spring. It only uses public setters. I declared them public not to declare public getters here. Your properties may have any access modifiers.
    • Pay attention to the attribute "ignoreUnknownFields". Its default value is "true". When it is "false" it will throw exception if any of your properties in file "application.yml" was not bound to any class property. It will help you a lot during debugging.
    0 讨论(0)
  • 2020-12-09 08:40

    http://www.baeldung.com/configuration-properties-in-spring-boot

    This only works for me with SB 1.5.4-RELEASE. Simple with just this. See above post for more info.

    @Configuration @ConfigurationProperties(prefix = "mail") public class ConfigProperties

    0 讨论(0)
  • 2020-12-09 08:44

    I don't know where the "xyz" came from (maybe you aren't showing your whole application.yml?). You don't normally bind with @Value in @ConfigurationProperties though (it has no way of knowing what your prefix is). Did you actually @EnableCongigurationProperties anywhere? Are you using SpringApplication to create the application context?

    0 讨论(0)
提交回复
热议问题