Spring Boot应用程序:无法获取application.properties?

jb62:

我在这里有一个春季启动应用程序: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}"

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

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

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

相关类如下所示(使用$ 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);
    }

}

我包括该“ 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);

}

我有一个看起来像“标准”文件结构的文件……src / main / java中的代码,依此类推。application.properties文件位于src / main / resources中。

任何建议表示感谢。

(快速补充:这是将Tomcat作为嵌入式服务器运行)

jb62:

这很晦涩-其他答案对帮助我指出正确的方向很有帮助。

在尝试了建议的解决方案之后,我进行了更深入的研究,并在项目属性-> Java构建路径-> Source(tab)->构建路径上的Source文件夹中找到了这一点:[排除部分]

**/application.properties

删除排除项解决了该问题,并且在启动期间从application.properties文件中获取了值。

可能值得注意的是,从命令行(在带有.project文件的目录中)运行此命令可以绕过排除问题并可以正常工作。

mvn spring-boot:run

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用主要方法访问Spring Boot应用程序中application.properties文件中的值

如何在Spring Boot应用程序中使用配置(properties / yml)文件中的属性?

如何在我的application.properties文件的Spring Boot应用程序中配置HikariCP?

无论在何处定义,如何在Spring Boot应用程序中获取有效的Properties?

对于应用程序配置定义的application.properties,Spring是否遵循任何命名约定?

如何在ReactJs应用程序中读取Spring Boot application.properties?

Intelij Spring Boot 2 application.properties

在Spring Boot应用程序中传递常量消息的正确方法(messages.properties或Constant类)

访问Tomcat中用于Spring Boot应用程序的externalize application.properties?

Spring Boot应用程序:无法解析application.properties中的占位符?

使用特定的application.properties文件将Spring Boot应用程序部署到Heroku

Spring Boot应用程序中特定于环境的application.properties文件

Spring-Boot应用程序的application- {profile} .properties的父属性

如何在Spring Boot应用程序的application.properties文件中设置MyBatis配置属性?

在Spring Boot应用程序中更改application.properties名称

我可以在Spring Boot应用程序中使用xml属性文件而不是application.properties吗?

Spring Boot邮件忽略application.properties

在Spring应用程序中使用application.properties

如何通过Spring Boot应用程序中的application.properties禁用WARN消息并仅在日志中启用INFO消息?

无法在Spring Boot应用程序的application.properties中获取环境变量

Spring Data Cassandra无法启动的Spring Boot应用程序

无法从 Spring Boot 服务中的 application.properties 加载值

Spring Boot 应用程序无法启动?

在 spring boot 中从 application.properties 获取属性

我想在 spring boot 应用程序启动之前向 application.properties 写入一个属性

Spring Boot 从 application.properties 读取值

Spring Boot - application.properties 中的程序参数

为什么我的 Spring Boot 应用程序不在 mustache 模板中呈现 messages.properties

在创建 Spring 应用程序上下文之前阅读 application.properties