`

Arch-03-28 Jndi 配置 Mongodb jetty 实现

 
阅读更多

使用 springdata mongodb,直接使用 spring 配置,很简单,但如果一定要使用 jndi 方式配置,则需要费一翻功夫了。

 

<!-- spring 中配置 springdata mongodb-->
	<mongo:mongo id="replicaSetMongo" replica-set="192.168.1.24:30001,192.168.1.25:30001,192.168.1.27:30001"/>	
	<mongo:db-factory id="mongoDbFactory" dbname="plussns" mongo-ref="replicaSetMongo"/>
	
 

 

1. jetty-evn.xml

 

 

<New id="springMongoDb"  class="org.eclipse.jetty.plus.jndi.Resource">
  <Arg>bean/plusMongoFactory</Arg>
  <Arg>
    <New class="com.plusrun.datasource.SpringMongoDbFactory">
		<Set name="serverName">192.168.1.24:30001,192.168.1.25:30001,192.168.1.27:30001</Set>		
		<Set name="databaseName">plussns</Set>
		<Set name="user" />
		<Set name="password" />
    </New>
  </Arg>
</New>

 

2. web.xml

 

 

	<resource-ref>
		<res-ref-name>bean/plusMongoFactory</res-ref-name>
		<res-type>com.plusrun.datasource.SpringMongoDbFactory</res-type>
		<res-auth>Container</res-auth>
	</resource-ref>

 

3. applicationContext.xml

 

 

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mongo="http://www.springframework.org/schema/data/mongo"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	 http://www.springframework.org/schema/data/mongo
     http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
  	 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
	 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
  	"
	default-autowire="byName" default-lazy-init="false">

 	<bean id="mongoDao" abstract="true">
		<property name="daoType" value="1"></property>
		<property name="dbFactory" ref="mongoDbFactory"></property>
	</bean>
	
	<!-- mongodb -->
	<jee:jndi-lookup id="jndiMongoDbFactory" jndi-name="bean/plusMongoFactory" />
	<bean id="mongoDbFactory" class="org.springframework.data.mongodb.MongoDbFactory"  factory-bean="jndiMongoDbFactory" factory-method="mongoDbFactory"/>
...

 

4. SpringMongoDbFactory.java

 

 

package com.plusrun.datasource;

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.authentication.UserCredentials;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import com.mongodb.Mongo;
import com.mongodb.ServerAddress;

public class SpringMongoDbFactory {

	private String serverName;
	private String databaseName;
	private String user;
	private String password;
	private static int DEFAULT_PORT = 27017;

	public MongoDbFactory mongoDbFactory() throws Exception {
		List<ServerAddress> addrs = new ArrayList<ServerAddress>();
		for (String addressStr : serverName.split(",")) {
			String server = addressStr;
			int port = DEFAULT_PORT;
			if (addressStr.indexOf(":") > 0) {
				server = addressStr.split(":")[0];
				port = Integer.parseInt(addressStr.split(":")[1]);
			}
			addrs.add(new ServerAddress(server, port));

		}

		UserCredentials userCredentials = new UserCredentials(user, password);
		return new SimpleMongoDbFactory(new Mongo(addrs), databaseName, userCredentials);
	}

	public MongoTemplate mongoTemplate() throws Exception {
		return new MongoTemplate(mongoDbFactory());
	}

	public void setServerName(String serverName) {
		this.serverName = serverName;
	}

	public void setDatabaseName(String databaseName) {
		this.databaseName = databaseName;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

 

5. BaseSpringDataDao.java

 

 

...

	/**
	 * 设置 mongodb factory.
	 * @param mongoDbFactory -
	 */
	public void setDbFactory(final MongoDbFactory mongoDbFactory) {
		this.mongoDbFactory = mongoDbFactory;

	}


...
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics