Spring Boot app:没有拿起application.properties?

jb6*_*b62 18 java spring solr spring-mvc maven

我有一个我在这里的春季启动应用程序:https: //github.com/christophstrobl/spring-data-solr-showcase/tree/4b3bbf945b182855003d5ba63a60990972a9de72

它编译并正常工作: mvn spring-boot:run

但是,当我在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}"
Run Code Online (Sandbox Code Playgroud)

我的applications.properties文件如下所示:

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

# SOLR
solr.host=http://192.168.56.11:8983/solr
Run Code Online (Sandbox Code Playgroud)

相关类看起来像这样(唯一使用$ 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);
    }

}
Run Code Online (Sandbox Code Playgroud)

我包括那个"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);

}
Run Code Online (Sandbox Code Playgroud)

我有一个看起来像"标准"文件结构... src/main/java中的代码等等.application.properties文件驻留在src/main/resources中.

任何建议都感激不尽.

(快速添加:这是运行Tomcat作为嵌入式服务器)

jb6*_*b62 49

这是模糊不清的 - 其他答案对于让我指向正确的方向非常有帮助.

在尝试了建议的解决方案之后,我深入探讨并在项目属性 - > Java构建路径 - >源(选项卡) - >构建路径上的源文件夹中找到了这个:[排除部分]

**/application.properties
Run Code Online (Sandbox Code Playgroud)

删除排除修复了问题,并在启动期间从application.properties文件中选取了值.

值得注意的是,从命令行(在.project文件的目录中)运行此操作会绕过排除问题并且工作正常.

mvn spring-boot:run
Run Code Online (Sandbox Code Playgroud)


Vov*_*kyi 9

我使用Spring Boot 2.0.0,我遇到了同样的问题.1.4.3版本完美运行.

原因是如果你定义这个参数:

-Dspring.config.location=file:/app/application-prod.yml
Run Code Online (Sandbox Code Playgroud)

Spring Boot现在不会添加默认位置进行搜索.

方案:

-Dspring.config.location=file:/app/application-prod.yml,classpath:application.yml
Run Code Online (Sandbox Code Playgroud)

看到:

  1. /org/springframework/boot/context/config/ConfigFileApplicationListener.java
  2. https://docs.spring.io/spring-boot/docs/2.0.1.BUILD-SNAPSHOT/reference/htmlsingle/#appendix

  • 这个答案似乎是正确的。应警告用户,文档(在[第 76.3 节](https://docs.spring.io/spring-boot/docs/2.1.2.RELEASE/reference/htmlsingle/#howto-change-the-location-of -external-properties)) 状态(在解释如何覆盖 `spring.config.location` 之后)“无论您在环境中设置什么,Spring Boot 总是如上所述加载 application.properties。” 另一种选择是使用“spring.config.additional-location”。 (4认同)

Aya*_*fov 9

不幸的是,上述方法对我没有帮助。将资源文件夹添加到类路径解决了我的问题。

完成步骤:

  1. 选择您的 Spring 应用程序并打开运行配置
  2. 选择类路径选项卡
  3. 选择用户条目
  4. 单击高级按钮
  5. 选择添加文件夹并单击确定按钮
  6. 选择您的资源文件夹(/src/main/resources)并单击“确定”按钮

在此输入图像描述


小智 8

在 pom.xml 中包含以下内容。这应该可以解决问题。

<build>
    <resources>     
        <resource>
            <directory>src/main/resources</directory>
            <includes>                      
                <include>**/*.properties</include>                  
            </includes>
        </resource>            
    </resources>
</build>
Run Code Online (Sandbox Code Playgroud)


Kun*_*tre 7

对我来说,这是因为包装为pom

我在pom.xml中有如下内容

<packaging>pom</packaging>
Run Code Online (Sandbox Code Playgroud)

因此,如果您有类似的事情,

  1. 删除它以用于Spring-boot App。

  2. 删除目标文件夹或mvn clean。

  3. 然后MVN安装。
  4. 在target / classes / application.properties文件下观察您的属性。


Ada*_*hal 7

我通过在构建路径中添加资源文件夹解决了这个问题。

在此处输入图片说明

请执行下列操作:-

  1. 点击添加文件夹...
  2. 添加资源文件夹

在此处输入图片说明


Dan*_*ani 5

在@Configuration类中声明PropertySourcesPlaceholderConfigurer。

@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {

      return new PropertySourcesPlaceholderConfigurer();
}
Run Code Online (Sandbox Code Playgroud)

以及带有正确注释的属性资源路径。

@PropertySource("classpath:your.properties")
Run Code Online (Sandbox Code Playgroud)