Java Spring Boot 应用程序:没有选择 application.properties?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33022827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot app: Not picking up application.properties?
提问by jb62
I have a spring boot app I got here: https://github.com/christophstrobl/spring-data-solr-showcase/tree/4b3bbf945b182855003d5ba63a60990972a9de72
我有一个 Spring Boot 应用程序:https: //github.com/christophstrobl/spring-data-solr-showcase/tree/4b3bbf945b182855003d5ba63a60990972a9de72
It compiles and works fine with: mvn spring-boot:run
它编译并正常工作: 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.
但是,当我在 Spring Tools Suite 中单击“作为 Spring Boot 应用程序运行”时,我收到一个关于无法找到${solr.host}
application.properties 文件中设置的错误。
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:
我的 applications.properties 文件如下所示:
# 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.
相关类看起来像这样(使用 $solr.host 变量的唯一地方)。此外,如果我直接寻址 SOLR 服务器的 IP(如注释代码中所示),应用程序将正常启动。
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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...
我包括了“ProductRepository”——错误中提到的那个——尽管那里没有太多事情发生......
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
我有看起来像“标准”文件结构的东西...... src/main/java 中的代码等等。application.properties 文件位于 src/main/resources 中。
Any suggestions gratefully accepted.
任何建议都感激地接受。
(Quick addition: This is running Tomcat as the embedded server)
(快速补充:这是运行Tomcat作为嵌入式服务器)
采纳答案by jb62
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]
在尝试了建议的解决方案后,我深入研究并在 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.
删除排除修复了问题,并且在启动期间从 application.properties 文件中提取了值。
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.
值得注意的是,从命令行(在包含 .project 文件的目录中)运行它绕过了排除问题并且工作正常。
mvn spring-boot:run
回答by Dani
Declare the PropertySourcesPlaceholderConfigurer in your @Configuration class.
在 @Configuration 类中声明 PropertySourcesPlaceholderConfigurer。
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
And your property resource path with the proper annotation.
以及带有正确注释的属性资源路径。
@PropertySource("classpath:your.properties")
回答by cpd214
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 theapplication.properties
file. So, you can also rename your applications.properties
file to application.properties
and it should work then.
添加PropertySourcesPlaceholderConfigurer
和@PropertySource
应该在你要保持你的属性文件名的情况下工作applications.properties
。但是,AFAIK spring boot 会自动拾取该application.properties
文件。因此,您也可以将applications.properties
文件重命名为application.properties
,然后它应该可以工作。
回答by pawan
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 resourcefolder to resources. Spring do it automatically for you but if its is not happening do it manually. It solved my issue, might help yours.
我也遇到了同样的问题,它没有在类路径中加载 application.properties 文件。在我的情况下,问题是,如果在您的资源文件夹中您有 1 个以上的资源,即属性文件或 xml 文件,那么您需要将资源文件夹重命名为resources。Spring 会自动为您执行此操作,但如果没有发生,请手动执行。它解决了我的问题,可能对你有帮助。
回答by user3396256
I have some code for importing properties in Spring boot:
我有一些用于在 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.
创建 Spring Boot 应用程序时,它application.properties
默认从资源文件夹中读取。您不需要导入属性文件。
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\propertyfolder so I am adding annotation @PropertySource
to read these property files.
假设您创建了另一个具有不同名称的属性文件,或者您已将该application.properties
文件移动到另一个文件夹。在我的例子中,我将属性文件移动到了资源\属性文件夹,所以我添加了注释@PropertySource
来读取这些属性文件。
回答by Vova Perebykivskyi
I used Spring Boot2.0.0and I faced same problem. With version 1.4.3it worked perfectly.
我使用了Spring Boot 2.0.0并且遇到了同样的问题。在1.4.3版本中,它运行良好。
Reasonis that if you define this argument:
原因是,如果您定义此参数:
-Dspring.config.location=file:/app/application-prod.yml
Spring Bootnow is not adding default locations to search.
Spring Boot现在没有添加默认位置进行搜索。
Solution:
解决方案:
-Dspring.config.location=file:/app/application-prod.yml,classpath:application.yml
See:
看:
- /org/springframework/boot/context/config/ConfigFileApplicationListener.java
- https://docs.spring.io/spring-boot/docs/2.0.1.BUILD-SNAPSHOT/reference/htmlsingle/#appendix
- /org/springframework/boot/context/config/ConfigFileApplicationListener.java
- https://docs.spring.io/spring-boot/docs/2.0.1.BUILD-SNAPSHOT/reference/htmlsingle/#appendix
回答by Popeye
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
在创建 src/test/resources 文件夹时,勾选复选框“更新其他源文件夹中的排除过滤器以解决嵌套问题”。并且还使用 PropertySource 加载 src
@PropertySource(value = {"classpath:application-junit.properties"},
ignoreResourceNotFound = true)
回答by Captain.Karthick
Include the following in your pom.xml. This should fix the issue.
在 pom.xml 中包含以下内容。这应该可以解决问题。
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
回答by Levant Alejandro
In my case, the resource foulder was not registered a a resource. I use IntelliJ, so I went to the module settings section, selected the resources folder and then clicked on resource on the upper part of the window. it started taking the application.properties file after that
就我而言,资源文件夹未注册为资源。我使用 IntelliJ,所以我转到模块设置部分,选择资源文件夹,然后单击窗口上部的资源。之后它开始使用 application.properties 文件
回答by Kundan Atre
For me it was due to packaging as pom
对我来说,这是由于包装为pom
I had something in my pom.xml as below
我的 pom.xml 中有如下内容
<packaging>pom</packaging>
So if you have similar thing,
所以如果你有类似的事情,
Remove it for spring-boot App.
Delete target folder or mvn clean.
- then mvn install.
- Watch your property under target/classes/application.properties file.
为 spring-boot 应用程序删除它。
删除目标文件夹或 mvn clean。
- 然后 mvn 安装。
- 在 target/classes/application.properties 文件下观察您的属性。