4

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?

hirarqi
  • 251
  • 2
  • 14
  • Have you check, if your application is running under the development profile. It seems it is not. If you got to the beginning of the spring logs you can see the active profile. – Cool Java guy מוחמד Jul 06 '20 at 02:44
  • Is there actually a value for that property in your dev config? Because in the property file you shared, there isn't. –  Jul 06 '20 at 02:58

3 Answers3

0

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

Ramesh KC
  • 570
  • 7
  • 30
0

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

夢のの夢
  • 5,054
  • 7
  • 33
  • 63
0

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.

Woodchuck
  • 3,869
  • 2
  • 39
  • 70