Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

.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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
643 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...