最近研究了mongodb获取本地连接属性的方案,场景就是mongodb的连接地址不在配置文件中配置,而是在代码中写好,代码中是从本地文件读取地址。
public class MongoConfig { @Bean public MongoDbFactory mongoDbFactory() throws UnknownHostException { StringBuffer url=new StringBuffer(); url.append("127.0.0.1");//数据库地址 url.append(":"); url.append("27017");//数据库端口 url.append("/"); url.append("dataBase");//数据库名字 MongoClientURI mongoClientURI=new MongoClientURI(url.toString(),new MongoClientOptions.Builder()); SimpleMongoDbFactory simpleMongoDbFactory=new SimpleMongoDbFactory(mongoClientURI); return simpleMongoDbFactory; } @Bean public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory) throws UnknownHostException{ MongoTemplate mongoTemplate= new MongoTemplate(mongoDbFactory); return mongoTemplate; } }
原文地址:https://www.cnblogs.com/daijiting/p/11527692.html
时间: 2024-10-21 08:04:38