Springboot移除⾃动配置Mongodb
最近由于项⽬开发需要,需要抽取⼀层MongoBaseDao:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.db.core.MongoTemplate;
import org.db.core.query.Criteria;
import org.db.core.query.Query;
import org.db.core.query.Update;
import flect.Field;
import flect.ParameterizedType;
import java.util.List;
public abstract class MongoBaseDao<T> {
private static final String ID_FIELD = "_id";
private static final String DELETE_FIELD = "deleted";
@Autowired
@Qualifier("mongoTemplate")
protected MongoTemplate mongoTemplate;
//保存⼀个对象到mongodb
public void save(T bean) {
mongoTemplate.save(bean);
}
// 根据id删除对象
public void deleteById(T t) {
}
// 通过条件查询更新数据
public void update(Query query, Update update) {
mongoTemplate.updateMulti(query, update, EntityClass());
}
// 根据id进⾏更新
public void updateById(String id, T t) {
Query query = new Query();
query.addCriteria(Criteria.where(ID_FIELD).is(id));
Update update = buildBaseUpdate(t);
update(query, update);
}
// 查询集合
public List<T> findAll() {
Query query = Query.query(Criteria.where(DELETE_FIELD).is(false));
return mongoTemplate.find(query, EntityClass());
}
// 通过id查询⼀个实体
public T findById(String id) {
Query query = Query.query(Criteria.where(ID_FIELD).is(id).and(DELETE_FIELD).is(false));
return mongoTemplate.findOne(query, EntityClass());
}
private Update buildBaseUpdate(T t) {
Update update = new Update();
Field[] fields = t.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
Object value = (t);
if (value != null) {
update.Name(), value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return update;
}
// 获取需要操作的实体类class
protected Class<T> getEntityClass() {
return ((Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
}
public MongoTemplate getMongoTemplate() {
return mongoTemplate;
}
}
由于是通⽤的代码,所以想放⾄common包中供其他服务依赖使⽤,此时出现了问题。部分服务依赖于common包中的部分内容,但并不需要连接mongo,此时这些项⽬启动时
会报错:
01-07 15:21:15.657 INFO -- [cluster-ClusterId{value='5c32fdebe4e45a2cdae06391', description='null'}-localhost:27017] db.driver.cluster:76:[] [] Exception in monitor thread while connecting to server localhost:db.MongoSocketOpenException: Exception opening socket
db.connection.SocketStream.open(SocketStream.java:63)
db.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
db.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.ConnectException: Connection refused (Connection refused)
at java.PlainSocketImpl.socketConnect(Native Method)
at java.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at tToAddress(AbstractPlainSocketImpl.java:206)
at t(AbstractPlainSocketImpl.java:188)
at t(SocksSocketImpl.java:392)
springframework和springbootat t(Socket.java:589)
db.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57)
db.connection.SocketStream.open(SocketStream.java:58)
... 3 common frames omitted
查了相关资料发现如果在springboot项⽬中存在spring-boot-starter-data-mongodb的依赖,springboot会在项⽬启动时⾃动实例化⼀个mongo实例,因此需要在项⽬启动时禁⽤mongo的⾃动配置。
看了⽹上说在Springboot启动类上增加 @SpringBootApplication(exclude = MongoAutoConfiguration.class) 配置,但是我试了之后发现仍然报错:
01-07 15:52:49.717 ERROR -- [main] o.s.b.d.LoggingFailureAnalysisReporter:42:[] []
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method mongoDbFactory in org.springframework.boot.MongoDataAutoConfiguration required a bean of type 'db.MongoClient' that could not be found.
- Bean method 'mongo' not loaded because auto-configuration 'MongoAutoConfiguration' was excluded
Action:
Consider revisiting the conditions above or defining a bean of type 'db.MongoClient' in your configuration.
继续查资料发现配置中还需要加⼊MongoDataAutoConfiguration.class才⾏,最终配置如:@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class} ) ,项⽬可以正常启动。
如果单独只配置 @SpringBootApplication(exclude = MongoDataAutoConfiguration.class),仍然会出现最开始的报错情况。
针对我的项⽬情况,还想到有另外⼀种⽅案可供实现:可以在common包中单独抽取出common-mongo模块供其他需要连接mongo的服务依赖。
参考资料:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论