Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@ConfigurationProperties prefix not working

.yml file

cassandra:     keyspaceApp:junit solr:     keyspaceApp:xyz 

Bean

@Component @ConfigurationProperties(prefix="cassandra") public class CassandraClientNew {    @Value("${keyspaceApp:@null}") private String keyspaceApp; 

Main method file

@EnableAutoConfiguration @ComponentScan @PropertySource("application.yml") public class CommonDataApplication {     public static void main(String[] args) {         ConfigurableApplicationContext context = new SpringApplicationBuilder(CommonDataApplication.class)                 .web(false).headless(true).main(CommonDataApplication.class).run(args);     } } 

TestCase

@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = CommonDataApplication.class) @IntegrationTest @EnableConfigurationProperties public class CassandraClientTest {      @Autowired     CassandraClientNew cassandraClientNew;      @Test     public void test(){         cassandraClientNew.getSession();         System.out.println(" **** done ****");     } } 

Instead of setting junit as the keyspaceApp it sets xyz.

Looks like prefix="cassandra" not working

like image 324
plzdontkillme Avatar asked Dec 11 '14 05:12

plzdontkillme


People also ask

What is prefix in @ConfigurationProperties?

@ConfigurationProperties works best with hierarchical properties that all have the same prefix; therefore, we add a prefix of mail. The Spring framework uses standard Java bean setters, so we must declare setters for each of the properties.

What is the use of @configuration in spring boot?

Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.

How do you call a properties file in spring boot?

Another method to access values defined in Spring Boot is by autowiring the Environment object and calling the getProperty() method to access the value of a property file.

How do I read properties file in spring boot main class?

Another very simple way to read application properties is to use @Value annotation. Simply annotation the class field with @Value annotation providing the name of the property you want to read from application. properties file and class field variable will be assigned that value.


1 Answers

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.

like image 66
Tom Avatar answered Oct 12 '22 13:10

Tom