Crud存储库的Spring bean配置 [英] Spring bean configuration for Crud Repositories
问题描述
我正在开发一种向弹性搜索注入数据的应用程序。使用弹簧数据弹性搜索jar来注入数据。
I'm developing an application which injects data to elastic search. Used spring-data-elastic-search jar to inject data.
package com.customer;
import javax.annotation.Resource;
import com.customer.repositories.CustomerRepo;
@SuppressWarnings("restriction")
public class CustomerService {
@Resource
CustomerRepo custRepo;
public void save(Customer cust) {
custRepo.save(cust);
}
}
=========== ==================================================
==================================================================================
package com.customer;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(
indexName = "Customer", type = "cust"
)
public class Customer{
private String name;
public Customer(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
=========== ================================================== package code $ c
==================================================================================
package com.customer.repositories;
import com.customer.Customer;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface CustomerRepo extends ElasticsearchRepository<Customer, String> {
}
=========== ==================================================
==================================================================================
package com.customer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainClass {
public static void main(String args[]) {
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"spring-customer.xml"});
CustomerService cust = (CustomerService)context.getBean("customerService");
Customer customer = new Customer("appu");
cust.save(customer);
}
}
Spring-customer.xml
}
Spring-customer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.customer" />
<import resource="spring-repository.xml"/>
<bean id="customerService" class="com.customer.CustomerService" scope="prototype" >
<property name="custRepo" ref="custRepo"></property>
</bean>
</beans>
=================== ================================================== ===========
==================================================================================
Spring-repository.xml
Spring-repository.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<elasticsearch:transport-client id="client" cluster-nodes="localhost:9300" />
<bean name="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client" />
</bean>
<elasticsearch:repositories
base-package="com.customer.repositories" />
The issue is that, I'm getting the following exception:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepo': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:313)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105)
at com.customer.MainClass.main(MainClass.java:12)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepo': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1442)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:871)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:813)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:438)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:550)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:155)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
... 7 more
Caused by: java.lang.NullPointerException
at org.springframework.data.elasticsearch.repository.support.MappingElasticsearchEntityInformation.<init>(MappingElasticsearchEntityInformation.java:57)
at org.springframework.data.elasticsearch.repository.support.MappingElasticsearchEntityInformation.<init>(MappingElasticsearchEntityInformation.java:49)
at org.springframework.data.elasticsearch.repository.support.ElasticsearchEntityInformationCreatorImpl.getEntityInformation(ElasticsearchEntityInformationCreatorImpl.java:46)
at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactory.getEntityInformation(ElasticsearchRepositoryFactory.java:57)
at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactory.getTargetRepository(ElasticsearchRepositoryFactory.java:64)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:147)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:162)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:44)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
我的bean配置似乎有些错误
It seems like some thing wrong with my bean configuration
<bean id="customerService" class="com.customer.CustomerService" scope="prototype" >
<property name="custRepo" ref="custRepo"></property>
</bean>
CustomerRepository是一个接口,我如何强制弹簧自动查找实现类。
CustomerRepository being an interface, how can I enforce spring to lookup the implementation classes automatically.
注意:当容器执行自动布线时,这可以正常工作。但是在从bean xml文件初始化时,问题仍然存在。
Note: This is working fine when container does the 'autowiring' . But while initializing from bean xml file, the issue persist.
有没有人可以帮助解决这个问题?
Can any one help to resolve this issue ?
推荐答案
问题不在您的bean配置中,但必须在Customer类中声明@Id如下:
Problem is not in your bean configuration but you have to declare @Id as below in Customer class
@Document(
indexName = "Customer", type = "cust"
)
public class Customer{
@Id
private String id;
private String name;
//setters and getters
}
您需要拥有ID才能对一个实体进行索引,SD elasticsearch不支持任何没有ID的索引的实体。
You need to have Id in order to index an entity, SD elasticsearch does not support any entity to be indexed without an id.
========================
==========================
您不需要将服务定义为bean,并将其注入到仓库中。
只需使用@Service标记它,并使用上述@Resource的资源库
You don't need to define service as bean and inject manually a repository into it. Just mark it with @Service and use repository as @Resource as above
您的文件中的更改
1)CustomerService.java
package com.customer;
1) CustomerService.java package com.customer;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.customer.repositories.CustomerRepo;
@SuppressWarnings("restriction")
@Service
public class CustomerService {
@Resource
CustomerRepo custRepo;
public void save(Customer cust) {
custRepo.save(cust);
}
}
2)spring-customer.xml
2) spring-customer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.customer" />
<import resource="spring-repository.xml"/>
</beans>
我没有测试,但应按预期工作。
I have not tested it but should work as expected.
这篇关于Crud存储库的Spring bean配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!