2016-05-21 76 views
0

我無法使用@Autowired創建ElasticsearchRepositorybean。我在我的代碼中使用這個link as a reference來啓用彈簧引導的彈性搜索功能。我認爲我的代碼忽略了Elasticsearch configurationspring boot:忽略Elasticsearch配置並以異常結束org.springframework.beans.factory.NoSuchBeanDefinitionException

平臺詳細信息:

Operating system: Windows 10 
Java: 1.8.0.77 build 
IDE: IntelliJ IDEA Community Edition 13.1.4 

例外:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [repository.PostRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. 

在項目POM

<parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.3.5.RELEASE</version> 
    </parent> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-elasticsearch</artifactId> 
      <version>1.3.5.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.projectlombok</groupId> 
      <artifactId>lombok</artifactId> 
      <version>1.16.8</version> 
     </dependency> 
     <dependency> 
      <groupId>com.h2database</groupId> 
      <artifactId>h2</artifactId> 
      <version>1.4.191</version> 
     </dependency> 

    </dependencies> 
</project> 

Elasticsearch configuration

package config; 

import org.elasticsearch.client.Client; 
import org.elasticsearch.client.transport.TransportClient; 
import org.elasticsearch.common.transport.InetSocketTransportAddress; 
import org.elasticsearch.common.transport.TransportAddress; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.core.env.Environment; 
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; 
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; 
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; 
import org.springframework.stereotype.Service; 

import javax.annotation.Resource; 

    @Configuration 
    @PropertySource(value = "classpath:config/elasticsearch.properties") 
    @EnableElasticsearchRepositories(basePackages = "repository") 
    @Service("searchConfiguration") 
    public class ElasticsearchConfiguration { 
     @Resource 
     private Environment environment; 
     @Bean 
     public Client client() { 
      TransportClient client = new TransportClient(); 
      TransportAddress address = new InetSocketTransportAddress(environment.getProperty("elasticsearch.host"), Integer.parseInt(environment.getProperty("elasticsearch.port"))); 
      client.addTransportAddress(address); 
      return client; 
     } 

     @Bean 
     ElasticsearchOperations elasticsearchTemplate() { 
      return new ElasticsearchTemplate(client()); 
     } 
    } 

應用屬性文件

elasticsearch.host = localhost 
# if you use you local elasticsearch host 
elasticsearch.port = 9300 

數據映射對象:

package core; 

import org.springframework.data.elasticsearch.annotations.Document; 

import java.util.UUID; 


@Document(indexName = "explore", type = "explore", shards = 1, replicas = 0) 
public class FileProperties { 

    String id= UUID.randomUUID().toString(); 

    public String getFilename() { 
     return filename; 
    } 

    public void setFilename(String filename) { 
     this.filename = filename; 
    } 

    String filename; 

    public String getSize(long length) { 
     return size; 
    } 

    public void setSize(String size) { 
     this.size = size; 
    } 



    public String getPath(String absolutePath) { 
     return path; 
    } 

    public void setPath(String path) { 
     this.path = path; 
    } 

    public String getMimeType() { 
     return mimeType; 
    } 

    public void setMimeType(String mimeType) { 
     this.mimeType = mimeType; 
    } 

    String size; 
    String path; 
    String mimeType; 

} 

存儲庫:從我們ElasticsearchRepository延伸

package repository; 

import core.FileProperties; 
import org.springframework.data.domain.Page; 
import org.springframework.data.domain.Pageable; 
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; 
import org.springframework.stereotype.Service; 


@Service("elasticsearchRepository") 
public interface PostRepository extends ElasticsearchRepository<FileProperties, String> { 

    Page<FileProperties> findByTagsName(String name, Pageable pageable); 
} 

數據訪問服務

public interface FilePropertiesService { 
    FileProperties save(FileProperties fileProperties); 
    FileProperties findOne(String id); 
    Iterable<FileProperties> findAll(); 

package core; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
import repository.PostRepository; 



@Service("filePropertiesServiceIMPL") 
public class FilePropertiesServiceIMPL implements FilePropertiesService { 

    @Autowired(required = true) 
    private PostRepository elasticsearchRepository; 

    @Override 
    public FileProperties save(FileProperties fileProperties) { 
     return elasticsearchRepository.save(fileProperties); 
    } 

    @Override 
    public FileProperties findOne(String id) { 
     return elasticsearchRepository.findOne(id); 
    } 

    @Override 
    public Iterable<FileProperties> findAll() { 
     return elasticsearchRepository.findAll(); 
    } 

} 

測試,結果

package core; 

import lombok.extern.slf4j.Slf4j; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.context.web.SpringBootServletInitializer; 
import org.springframework.context.ApplicationContext; 

import javax.annotation.PostConstruct; 


@Slf4j 
@EnableAutoConfiguration 
@SpringBootApplication 
public class AppStart extends SpringBootServletInitializer { 
    @Autowired(required = true) 
    public FileSystem exploreFileSystem; 



    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(AppStart.class); 
    } 

    @PostConstruct 
    @Autowired(required = true) 
    public void listen() { 
     exploreFileSystem.LetsExploreFileSystem(); 

    } 

    public static void main(String[] args) { 
     ApplicationContext context = SpringApplication.run(AppStart.class, args); 

    } 
} 

結果:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [repository.PostRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
     ... 45 common frames omitted 

回答

0

如果庫不在同一或子包作爲帶班註釋@SpringBootApplication,您必須指定以下注釋以及存儲庫的包裝es屬性:

@EnableJpaRepositories("repository") 

對你的AppStart類。 默認情況下,Spring將只掃描主應用程序類的包和子包。