我无法使用bean
创建ElasticsearchRepository
的{{1}}。我在我的代码中使用此link as a reference来启用弹簧启动的弹性搜索功能。我认为我的代码忽略了@Autowired
。
平台详情:
Elasticsearch configuration
例外:
Operating system: Windows 10
Java: 1.8.0.77 build
IDE: IntelliJ IDEA Community Edition 13.1.4
在项目POM中
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.
<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
存储库:我们从ElasticsearchRepository扩展
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;
}
数据访问服务
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);
}
}
答案 0 :(得分:0)
如果存储库与具有注释@SpringBootApplication
的类不在同一个子包或子包中,则必须使用存储库的包作为属性指定以下注释:
@EnableJpaRepositories("repository")
在AppStart课程上。 默认情况下,Spring只扫描主应用程序类的包和子包。