简体   繁体   中英

Spring boot Custom Properties

in my project there are 2 resource properties

1. application.properties

server.port=8002

spring.data.mongodb.host=
spring.data.mongodb.port=
spring.data.mongodb.database=
spring.data.mongodb.username=
spring.data.mongodb.password=

2. application-development.properties

server.port=8002

spring.data.mongodb.host=
spring.data.mongodb.port=
spring.data.mongodb.database=
spring.data.mongodb.username=
spring.data.mongodb.password=

spring.data.solr.host

this class uses the value properties of development

@Configuration
@EnableSolrRepositories(basePackages = {
    "id.alfadigital.alfagift.service.product.v1.db.solr.repository",
    "id.alfadigital.alfagift.service.product.v2.db.solr.repository"
})
public class SolrConfiguration {

  @Value("${spring.data.solr.host}")
  private String solrUrl;

  @Bean
  public SolrClient solrClient() {
    return new HttpSolrClient.Builder(solrUrl).build();
  }

  @Bean
  public SolrTemplate solrTemplate(SolrClient client) {
    return new SolrTemplate(client);
  }
}

I use application-development.properties as my project resoure

so I run the project with the following command:

mvn spring-boot:run -D spring.profiles.active=development

but an error is attached when I run the project

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'solrConfiguration': 
           Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: 
           Could not resolve placeholder 'spring.data.solr.host' in value "${spring.data.solr.host}"
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.data.solr.host' in value "${spring.data.solr.host}"

I'm confused, where are my mistakes, and how should I do?

Can you please run your application with following command. Because of wrong use of command it's not able to pick up the development profile.

mvn spring-boot:run -Dspring.profiles.active=development

Example: how to use Spring Boot profiles

Provided you have the correct file name application-development.properties and correct Java Opts -Dspring.profiles.active=development , you must also place the profile specific properties file alongside with application.properties

Profile-specific properties are loaded from the same locations as standard application.properties

https://docs.spring.io/spring-boot/docs/2.1.12.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties

Make sure your properties file matches up with the name of your Spring profile as described here .

That is, if you are running from a "development" profile, Spring should pick up the application-development.properties file (or application-development.yml ).

Then in your application.properties file you can specify your profile by using spring.profiles.active=development . Or you can specify the profile from the command line using -Dprofile as you mention.

As mentioned in in the link, " If several profiles are specified, a last-wins strategy applies. For example, profiles specified by the spring.profiles.active property are added after those configured through the SpringApplication API and therefore take precedence. "

But also note that in your shared code you have no value for your spring.data.solr.host property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM