Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 517 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spring Boot app: Not picking up application.properties?

#1
I have a spring boot app I got here:

[To see links please register here]


It compiles and works fine with: `mvn spring-boot:run`

However, when I click "run as Spring Boot app" in Spring Tools Suite, I get an error about not being able to find `${solr.host}` which is set up in the application.properties file.


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.data.solr.showcase.product.ProductServiceImpl.setProductRepository(org.springframework.data.solr.showcase.product.ProductRepository); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productRepository': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'solr.host' in string value "${solr.host}"


My applications.properties file looks like this:

# SPRING MVC
spring.view.suffix=.jsp
spring.view.prefix=/WEB-INF/views/

# SOLR
solr.host=http://192.168.56.11:8983/solr

The relevant class looks like this (the only place where the $solr.host variable is used). Also, if I directly address the SOLR server's IP (as in the commented code) the app starts fine.

* Copyright 2012 - 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*

[To see links please register here]

*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.solr.showcase.config;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.repository.config.EnableSolrRepositories;
import org.springframework.data.solr.server.SolrServerFactory;
import org.springframework.data.solr.server.support.MulticoreSolrServerFactory;

/**
* @author Christoph Strobl
*/
@Configuration
@EnableSolrRepositories(basePackages = { "org.springframework.data.solr.showcase.product" })

public class SearchContext {

@Bean
public SolrServer solrServer(@Value("${solr.host}") String solrHost) {
return new HttpSolrServer(solrHost);
}

// @Bean
// public SolrServer solrServer(@Value("http://192.168.56.11:8983/solr") String solrHost) {
// return new HttpSolrServer(solrHost);
// }

@Bean
public SolrServerFactory solrServerFactory(SolrServer solrServer) {
return new MulticoreSolrServerFactory(solrServer);
}

@Bean
public SolrTemplate solrTemplate(SolrServerFactory solrServerFactory) {
return new SolrTemplate(solrServerFactory);
}

}


I'm including that "ProductRepository" -- the one mentioned in the error -- although there isn't much going on there...

* Copyright 2012 - 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*

[To see links please register here]

*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.solr.showcase.product;

import java.util.Collection;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.core.query.Query.Operator;
import org.springframework.data.solr.repository.Query;
import org.springframework.data.solr.repository.SolrCrudRepository;
import org.springframework.data.solr.showcase.product.model.Product;

/**
* @author Christoph Strobl
*/
interface ProductRepository extends SolrCrudRepository<Product, String> {

@Query(fields = { SearchableProductDefinition.ID_FIELD_NAME, SearchableProductDefinition.NAME_FIELD_NAME,
SearchableProductDefinition.PRICE_FIELD_NAME, SearchableProductDefinition.FEATURES_FIELD_NAME,
SearchableProductDefinition.AVAILABLE_FIELD_NAME }, defaultOperator = Operator.AND)
Page<Product> findByNameIn(Collection<String> names, Pageable page);

}

I've got what seems like a "standard" file structure... code in src/main/java and so on. The application.properties file resides in src/main/resources.

Any suggestions gratefully accepted.

(Quick addition: This is running Tomcat as the embedded server)
Reply

#2
Adding `PropertySourcesPlaceholderConfigurer` and `@PropertySource` should work in case you want to keep your properties file name as `applications.properties`. However, AFAIK spring boot automatically picks up the`application.properties` file. So, you can also rename your `applications.properties` file to `application.properties` and it should work then.
Reply

#3
This was obscure - and the other answers were very helpful in getting me pointed in the right direction.

After trying the suggested solutions, I poked around deeper and found this in Project Properties --> Java Build Path --> Source(tab) --> Source folders on build path: [Exclusion section]

**/application.properties

Removing the exclusion fixed the issue and the values were picked up from the application.properties file during startup.

It may be worth noting that running this from the command line (in the directory with the .project file) bypassed the exclusion problem and worked fine.

mvn spring-boot:run
Reply

#4
I also faced the same problem it is not loading the application.properties file in class path. In my case the issue was that, if in your resource folder you have more than 1 resources i.e. properties file or xml files then you need to rename the **resource** folder to **resources**. Spring do it automatically for you but if its is not happening do it manually. It solved my issue, might help yours.
Reply

#5
I have some code for importing properties in Spring boot:

@SpringBootApplication
@EnableIntegration
@EnableScheduling
@ImportResource({ "classpath*:applicationContext.xml" })
@PropertySources(value = {
@PropertySource(ignoreResourceNotFound = true, value = "classpath:properties/application.properties"),
@PropertySource(ignoreResourceNotFound = true, value = "classpath:properties/dbNhibernateConfig.properties"),
@PropertySource(ignoreResourceNotFound = true, value = "classpath:properties/mailConfiguration.properties"),
@PropertySource(ignoreResourceNotFound = true, value = "classpath:properties/errorcodes.properties") })
@IntegrationComponentScan("com.*.report.main")
public class AgilereportsApplication{

public static void main(String[] args) {
SpringApplication.run(AgilereportsApplication.class, args);
}
}

When a spring boot application is created it reads `application.properties` from the resource folder by default. You don't need to import a property file.

Let say you create another property file with different name or you have moved the `application.properties` file to another folder. In my case I moved property file to _resource\property_ folder so I am adding annotation `@PropertySource` to read these property files.
Reply

#6
I used *Spring Boot* *2.0.0* and I faced same problem.
With version *1.4.3* it worked perfectly.

**Reason** is that if you define this argument:

-Dspring.config.location=file:/app/application-prod.yml

*Spring Boot* now is not adding default locations to search.

**Solution**:

-Dspring.config.location=file:/app/application-prod.yml,classpath:application.yml

See:

1. /org/springframework/boot/context/config/ConfigFileApplicationListener.java
1.

[To see links please register here]


Reply

#7
While creating the src/test/resources folder, tick the checkbox "Update exclusion filters in other source folders to solve nesting". And also use the PropertySource to load the src

@PropertySource(value = {"classpath:application-junit.properties"},
ignoreResourceNotFound = true)
Reply

#8
Include the following in your pom.xml.
This should fix the issue.

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>

Reply

#9
For me it was due to packaging as **pom** <br>

I had something in my pom.xml as below <br>




<packaging>pom</packaging>

So if you have similar thing,
<br>

1. Remove it for spring-boot App.

2. Delete target folder or mvn clean.
3. then mvn install.
4. Watch your property under target/classes/application.properties file.
Reply

#10
I resolved this by adding resources folder in build path.

Before

[![enter image description here][1]][1]


Do the following:- <br>

1. Click on Add folder...
2. Add resources folder

[![enter image description here][2]][2]


[1]:

[2]:
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through