2017-07-03 139 views
0

我嘗試在我的彈簧啓動應用程序中使用嵌入式solr服務器。我的配置是如下:Spring引導中的嵌入式solr服務器的類路徑是什麼?

package ch.*.rest.config; 
import java.io.IOException; 
import javax.xml.parsers.ParserConfigurationException; 
import org.apache.solr.client.solrj.SolrClient; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.data.solr.core.SolrOperations; 
import org.springframework.data.solr.core.SolrTemplate; 
import org.springframework.data.solr.repository.config.EnableSolrRepositories; 
import org.springframework.data.solr.server.support.EmbeddedSolrServerFactory; 
import org.xml.sax.SAXException; 

@Configuration 
@EnableSolrRepositories 
public class SolrContext { 

    @Bean 
    public SolrClient solrClient() throws ParserConfigurationException, IOException, SAXException { 
     EmbeddedSolrServerFactory factory = new EmbeddedSolrServerFactory("classpath:com/acme/solr"); 
     return factory.getSolrClient(); 
    } 
    @Bean 
    public SolrOperations solrTemplate() throws ParserConfigurationException, IOException, SAXException { 
     return new SolrTemplate(solrClient()); 
    } 
} 

隨着gradle這個我包括以下依存

compile('org.apache.solr:solr-core:5.5.4') 
compile('org.springframework.data:spring-data-solr:2.1.4.RELEASE') 

應用程序的執行引發以下異常:

java.io.FileNotFoundException: class path resource [com/acme/solr] cannot be resolved to absolute file path because it does not exist 
> Buildiat org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:178) 
    at org.springframework.data.solr.server.support.EmbeddedSolrServerFactory.createCoreContainer(EmbeddedSolrServerFactory.java:110) 
    at org.springframework.data.solr.server.support.EmbeddedSolrServerFactory.initCoreContainer(EmbeddedSolrServerFactory.java:85) 
    at org.springframework.data.solr.server.support.EmbeddedSolrServerFactory.getCoreContainer(EmbeddedSolrServerFactory.java:193) 
    at org.springframework.data.solr.server.support.EmbeddedSolrServerFactory.getSolrClient(EmbeddedSolrServerFactory.java:80) 
    at ch.*.rest.config.SolrContext.solrClient(SolrContext.java:23) 
    at ch.*.rest.config.SolrContext$$EnhancerBySpringCGLIB$$ca87555f.CGLIB$solrClient$1(<generated>) 
    at ch.*.rest.config.SolrContext$$EnhancerBySpringCGLIB$$ca87555f$$FastClassBySpringCGLIB$$4c9fb49c.invoke(<generated>) 
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) 
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356) 
    at ch.*.rest.config.SolrContext$$EnhancerBySpringCGLIB$$ca87555f.solrClient(<generated>) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1134) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1028) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:207) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1136) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1064) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1225) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:552) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:207) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1136) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1064) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1225) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:552) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) 
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) 
    at ch.*.rest.RestApplication.main(RestApplication.java:13) 

顯然classpath中COM/ACME/solr未找到。我如何知道實際的課程路徑是什麼?

有關設置我跟着http://docs.spring.io/spring-data/solr/docs/2.1.x/reference/html/#solr.annotation

回答

0

從文檔:

public EmbeddedSolrServerFactory(String solrHome) 
         throws ParserConfigurationException, 
          IOException, 
          SAXException 

參數

solrHome - 任何路徑表達式有效使用與ResourceUtils指向solr.solr .home directory

這意味着你應該創建一個Solr H ome(並將solr配置放在那裏)放在資源文件夾中,以便Gradle檢索它。 然後,您應該將該路徑傳遞給嵌入式Solr工廠。 希望它有幫助