2016-05-21 12 views
0

ElasticsearchRepositorybean@Autowiredで作成することはできません。私はこのコードをスプリングブートで弾性検索機能を有効にするためにlink as a referenceとしています。私のコードはElasticsearch configurationを無視していると思います。spring 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()); 
     } 
    } 

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; 

} 

リポジトリ:の

アプリケーションプロパティは

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

データマッピングオブジェクトファイル我々は

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); 
} 

ElasticsearchRepositoryから延びデータアクセスサービス

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の場合は、リポジトリのパッケージに次の注釈を指定する必要があります属性として

@EnableJpaRepositories("repository") 

をAppStartクラスに設定します。 デフォルトでは、Springはメインアプリケーションクラスのパッケージとサブパッケージのみをスキャンします。

関連する問題