Spring XML配置中如何设置动态MongoDB Client URI(含认证)
解决Spring XML配置MongoDB动态注入用户名密码的问题
方法一:使用SpEL表达式引用Spring Bean
你当前XML里的{username}是属性文件占位符语法,要引用SecretImpl生成的username、password Bean,得改用SpEL表达式(#{...}语法),通过@beanName直接引用容器内的Bean:
修改XML配置:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 扫描SecretImpl所在包,让Spring加载该配置类 --> <context:component-scan base-package="com.your.package.path" /> <mongo:db-factory id="devMongoConnection" client-uri="mongodb://#{@username}:#{@password}@server_1:27000,server_2:27000,server_3:27000/db_name?replicaSet=test" /> </beans>
同时修正SecretImpl的构造逻辑(Spring需要能无参实例化该配置类):
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class SecretImpl { private final Secret secret; // 在构造方法中调用内部API获取凭证 public SecretImpl() { this.secret = Internal_API_For_Secrets(); } @Bean(name="username") public String username() { return secret.getUsername(); } @Bean(name="password") public String password() { return secret.getPassword(); } }
Spring启动时会先加载SecretImpl生成username、password Bean,再通过SpEL把值注入到MongoDB连接串中。
方法二:完全用Java配置生成MongoDbFactory
如果不想混合XML与Java配置,直接用Java代码创建MongoDbFactory Bean会更灵活,彻底规避硬编码问题:
- 修正
MongoConstructor,通过构造注入获取Secret:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MongoConstructor { private final Secret secret; private final String db = "db_name"; private final String rs = "test"; @Autowired public MongoConstructor(Secret secret) { this.secret = secret; } public String getMongoUri() { return String.format("mongodb://%s:%s@server_1:27000,server_2:27000,server_3:27000/%s?replicaSet=%s", secret.getUsername(), secret.getPassword(), db, rs); } }
- 编写MongoDB配置类生成
MongoDbFactory:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import com.mongodb.MongoClientURI; @Configuration @Import(SecretImpl.class) // 引入Secret配置类 public class MongoConfig { private final MongoConstructor mongoConstructor; @Autowired public MongoConfig(MongoConstructor mongoConstructor) { this.mongoConstructor = mongoConstructor; } @Bean(name = "devMongoConnection") public SimpleMongoDbFactory devMongoConnection() { MongoClientURI uri = new MongoClientURI(mongoConstructor.getMongoUri()); return new SimpleMongoDbFactory(uri); } }
- 加载该Java配置类:如果用XML,添加
<context:component-scan>扫描MongoConfig所在包;如果是Spring Boot,直接在启动类加@Import(MongoConfig.class)即可,无需保留XML中的<mongo:db-factory>配置。
内容的提问来源于stack exchange,提问作者SVill
相关产品推荐
相关产品推荐

