我正在尝试创建一个可以由所有 DAO 扩展的 BaseDAO 接口。该项目使用带有 mongodb 的 spring-data。问题是,如果我让所有单独的 DAO 扩展 MongoRepository 并且不编写实现类,那么一切正常。但是,如果我尝试使用泛型将 MongoRepository 添加到 BaseDAO 接口,该应用程序将不再工作,因为实例化 SimpleMongoRepository 所需的参数为空。这是我到目前为止的代码:
BaseDAO.java
import com.test.mongodb.BaseEntity;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.NoRepositoryBean;
import java.io.Serializable;
@NoRepositoryBean
public interface BaseDAO<T extends BaseEntity, ID extends Serializable> extends MongoRepository<T, ID> {
public T getTestObject(ID id);
}
BaseDAOImpl.java
import com.test.mongodb.BaseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.query.EntityInformationCreator;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;
import org.springframework.data.repository.NoRepositoryBean;
import java.io.Serializable;
@NoRepositoryBean
public class BaseDAOImpl<T extends BaseEntity, ID extends Serializable> extends SimpleMongoRepository<T,
ID> implements BaseDAO<T, ID> {
@Autowired
private static MongoTemplate mongoTemplate;
@Autowired
private static EntityInformationCreator entityInformationCreator;
public BaseDAOImpl(Class<T> type){
super((MongoEntityInformation<T, ID>) entityInformationCreator.getEntityInformation(type), mongoTemplate);
}
@Override
public T getTestObject(ID id){
return findOne(id);
}
}
用户DAO.java
import com.test.mongodb.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserDAO extends BaseDAO<User, String> {}
UserDAOImpl.java
import com.test.mongodb.User;
import org.springframework.stereotype.Repository;
@Repository
public class UserDAOImpl extends BaseDAOImpl<User, String> implements UserDAO {
public UserDAOImpl(){
super(User.class);
}
}
应用程序上下文.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd">
<!-- MongoFactoryBean instance -->
<mongo:mongo host="localhost" port="27017" />
<mongo:db-factory dbname="bank" mongo-ref="mongo" />
<!-- MongoTemplate instance -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
<bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
<bean id="entityInformationCreator" class="org.springframework.data.mongodb.repository.support.DefaultEntityInformationCreator">
<constructor-arg name="mappingContext" ref="mappingContext" />
</bean>
<mongo:repositories base-package="com.test.mongodb.repo"/>
</beans>
应用程序.java
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserRepository userRepository = context.getBean("userRepository", UserRepository.class);
User user = new User("Test User");
userRepository.save(user);
String id = user.getId();
System.out.println(id);
System.out.println(userRepository.getTestObject(user.getId()));
}
}
所以当我运行它时,我在 BaseDAOImpl 中得到一个 NPE,因为 mongoTemplate 和 entityInformationCreator 都是空的。我如何加载它们?我还查看了 Spring MongoDB 参考文档,但它主要说要遵循其他类型存储库的文档。我在网上和其他地方唯一能找到的就是创建一个工厂 bean。所以我也尝试过:
MongoRepoFactoryBean.java
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactory;
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import java.io.Serializable;
@NoRepositoryBean
public class MongoRepoFactoryBean<T extends MongoRepository<?,?>, ID extends
Serializable> extends MongoRepositoryFactoryBean {
protected RepositoryFactorySupport createRepositoryFactory(Class<T> clazz, MongoTemplate mongoTemplate) {
return new MongoRepoFactory(clazz, mongoTemplate);
}
private static class MongoRepoFactory extends MongoRepositoryFactory {
private Class clazz;
private MongoTemplate mongoTemplate;
public MongoRepoFactory(Class clazz, MongoTemplate mongoTemplate) {
super(mongoTemplate);
this.mongoTemplate = mongoTemplate;
this.clazz = clazz;
}
public Object getTargetRepository() {
return new BaseDAOImpl(clazz);
}
public Class<?> getRepositoryBaseClass() {
return BaseDAOImpl.class;
}
}
}
并将 applicationContext.xml 更改为:
<mongo:repositories base-package="com.test.mongodb.repo"
factory-class="com.test.mongodb.repo.MongoRepoFactoryBean"/>
但这也行不通。我也尝试过使用 JavaConfig,但是在使用注释进行配置时,我不知道如何设置工厂类。我究竟做错了什么?SimpleMongoRepository 没有默认构造函数。注入静态字段有问题吗?