Spring Boot 应用程序:没有选择 application.properties? [英] Spring Boot app: Not picking up application.properties?

查看:82
本文介绍了Spring Boot 应用程序:没有选择 application.properties?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Spring Boot 应用程序:https://github.com/christophstrobl/spring-data-solr-showcase/tree/4b3bbf945b182855003d5ba63a60990972a9de72

I have a spring boot app I got here: https://github.com/christophstrobl/spring-data-solr-showcase/tree/4b3bbf945b182855003d5ba63a60990972a9de72

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

但是,当我在 Spring Tools Suite 中单击作为 Spring Boot 应用程序运行"时,出现关于无法找到应用程序中设置的 ${solr.host} 的错误.properties 文件.

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}"

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

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

相关类看起来像这样(唯一使用 $solr.host 变量的地方).此外,如果我直接寻址 SOLR 服务器的 IP(如注释代码中所示),应用程序将正常启动.

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
 *
 * 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"——错误中提到的那个——尽管那里没有太多事情发生......

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
 *
 * 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.

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.

感谢接受任何建议.

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

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

推荐答案

这很模糊 - 其他答案非常有助于让我指明正确的方向.

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

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

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

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

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

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

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

这篇关于Spring Boot 应用程序:没有选择 application.properties?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆