你好我有Spring Data ElasticSearch的问题我可以用elasticSearch连接但是在创建存储库后我的程序停止工作这是我的代码。提前感谢您帮助解决此问题
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.storyworld.repository.elastic")
public class ElasticSearchConfig {
private static final String PROPERTY_ELASTIC_HOST = "localhost";
private static final int PROPERTY_ELASTIC_PORT = 9300;
private static final String PROPERTY_ELASTIC_CLUSTER_NAME = "elasticsearch";
@Bean
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
return new ElasticsearchTemplate(client());
}
@Bean
public Client client() throws Exception {
Settings esSettings = Settings.settingsBuilder().put("cluster.name", PROPERTY_ELASTIC_CLUSTER_NAME).build();
return TransportClient.builder().settings(esSettings).build().addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName(PROPERTY_ELASTIC_HOST), PROPERTY_ELASTIC_PORT));
}
}
这是我的存储库:
public interface ChatRepository extends ElasticsearchRepository<Chat, String>{}
和课堂聊天:
@Document(indexName = "chatest", type = "chat")
public class Chat {
@Id
private String id;
private String message;
}
创建存储库后,我收到错误:
479 [localhost-startStop-1] ERROR org.springframework.web.context.ContextLoader [] - Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'chatRepository': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.Class<org.springframework.data.repository.Repository<?, ?>>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1154) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1056) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:740) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444) ~[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326) [spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) [spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4725) [catalina.jar:?]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189) [catalina.jar:?]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:?]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724) [catalina.jar:?]
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700) [catalina.jar:?]
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734) [catalina.jar:?]
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:596) [catalina.jar:8.5.6-dev]
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1805) [catalina.jar:8.5.6-dev]
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_121]
at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_121]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_121]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_121]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.Class<org.springframework.data.repository.Repository<?, ?>>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1474) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1102) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1064) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
... 28 more
我认为出现此问题是因为JPARepository这是JPA的配置
@Configuration
@EnableJpaRepositories(basePackages = "com.storyworld.repository.sql")
public class MySQLConfig {
private static final String PROPERTY_NAME_DATABASE_DRIVER = "com.mysql.jdbc.Driver";
private static final String PROPERTY_NAME_DATABASE_PASSWORD = "root";
private static final String PROPERTY_NAME_DATABASE_URL = "jdbc:mysql://localhost:3306/storyworld?useSSL=false";
private static final String PROPERTY_NAME_DATABASE_USERNAME = "root";
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "org.hibernate.dialect.MySQL5Dialect";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "true";
private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "com.storyworld.domain.sql";
private static final String PROPERTY_NAME_HIBERNATE_AUTO = "update";
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(PROPERTY_NAME_DATABASE_DRIVER);
dataSource.setUrl(PROPERTY_NAME_DATABASE_URL);
dataSource.setUsername(PROPERTY_NAME_DATABASE_USERNAME);
dataSource.setPassword(PROPERTY_NAME_DATABASE_PASSWORD);
return dataSource;
}
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.show_sql", PROPERTY_NAME_HIBERNATE_SHOW_SQL);
properties.put("hibernate.dialect", PROPERTY_NAME_HIBERNATE_DIALECT);
properties.put("hibernate.hbm2ddl.auto", PROPERTY_NAME_HIBERNATE_AUTO);
return properties;
}
@Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN);
factory.setDataSource(dataSource());
factory.setJpaProperties(getHibernateProperties());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
}
答案 0 :(得分:1)
我认为,可能是因为依赖冲突,因为Spring数据弹性搜索和Spring数据JPA共享的共同依赖。
我正在使用此代码,尝试使用它。我相信它会解决你的问题: -
<!-- Spring ElasticSearch Data JPA-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<!-- Spring Data JPA-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.10.1.RELEASE</version>
</dependency>
答案 1 :(得分:0)
您好尝试将@Repository添加到您的界面。