.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
@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.
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.
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.
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.
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 Set
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With