gpt4 book ai didi

java - MongoDb 通过 jndi

转载 作者:IT老高 更新时间:2023-10-28 13:32:02 26 4
gpt4 key购买 nike

您知道是否可以像其他任何数据库一样通过来自 jndi 的数据源在 spring 中设置 mongodb 实例?

谢谢

最佳答案

是的,有可能,当您可以创建自己的 JNDI 工厂时,为什么还要依赖其他人的代码?只需创建一个实现 javax.naming.spi.ObjectFactory 的类和一个从 JNDI 上下文中提取 mongo 的 bean,我为 spring data-mongo MongoTemplate 对象配置了它。

public class CustomMongoJNDIFactory implements ObjectFactory {

public Object getObjectInstance(Object obj, Name name, Context nameCtx,
Hashtable<?, ?> environment) throws Exception {

validateProperty(obj, "Invalid JNDI object reference");

MongoTemplate mongoTemplate = null;
String db = null;
String host = null;
String username = null;
String password = null;
int port = 27017;

Reference ref = (Reference) obj;
Enumeration<RefAddr> props = ref.getAll();
while (props.hasMoreElements()) {
RefAddr addr = (RefAddr) props.nextElement();
String propName = addr.getType();
String propValue = (String) addr.getContent();
if (propName.equals("db")) {
db = propValue;
} else if (propName.equals("host")) {
host = propValue;
} else if (propName.equals("username")) {
username = propValue;
} else if (propName.equals("password")) {
password = propValue;
} else if (name.equals("port")) {
try {
port = Integer.parseInt(propValue);
} catch (NumberFormatException e) {
throw new NamingException("Invalid port value " + propValue);
}
}

}

// validate properties
validateProperty(db, "Invalid or empty mongo database name");
validateProperty(host, "Invalid or empty mongo host");
validateProperty(username, "Invalid or empty mongo username");
validateProperty(password, "Invalid or empty mongo password");

//create mongo template
mongoTemplate = new MongoTemplate(new Mongo(host, port), db,
new UserCredentials(username, password));

return mongoTemplate;
}


/**
* Validate internal String properties
*
* @param property
* @param errorMessage
* @throws NamingException
*/
private void validateProperty(String property, String errorMessage)
throws NamingException {
if (property == null || property.trim().equals("")) {
throw new NamingException(errorMessage);
}
}

/**
* Validate internal Object properties
*
* @param property
* @param errorMessage
* @throws NamingException
*/
private void validateProperty(Object property, String errorMessage)
throws NamingException {
if (property == null) {
throw new NamingException(errorMessage);
}
}

}

Spring Bean :

@Configuration
@Qualifier("mongoTemplate")
public class CustomMongoTemplate {


public @Bean MongoTemplate mongoTemplate() throws Exception {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
return (MongoTemplate) envCtx.lookup("bean/MyMongoBean");
}
}

上下文.xml:

<Resource name="bean/MyMongoBean" auth="Container"
type="org.springframework.data.mongodb.core.MongoTemplate"
factory="com.package.CustomMongoJNDIFactory"
host="" db="" username="" password=""/>

Web.xml

    <resource-env-ref>
<description>Mongo JNDI configuration</description>
<resource-env-ref-name>comp/env/bean/MyMongoBean</resource-env-ref-name>
<resource-env-ref-type>org.springframework.data.mongodb.core.MongoTemplate</resource-env-ref-type>
</resource-env-ref>

关于java - MongoDb 通过 jndi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4076254/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com