我有一个Spring Boot应用程序,可以在这里找到: https://github.com/christophstrobl/spring-data-solr-showcase/tree/4b3bbf945b182855003d5ba63a60990972a9de72
使用mvn spring-boot:run
可以成功编译和运行。
但是,当我在Spring Tools Suite中点击“以Spring Boot应用程序运行”时,会出现一个错误,指出无法找到在application.properties文件中设置的${solr.host}
。
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地址(如注释中的代码),应用程序就可以正常启动。
相关类如下所示(唯一使用 $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等目录中。应用程序属性文件位于src/main/resources目录中。
非常感谢任何建议。
(快速补充:这是在嵌入式服务器Tomcat上运行)