Mongodb總結4-Spring環境使用Mongodb

小雷FansUnion發表於2015-10-14
前幾次的例子,要麼是Shell,要麼是普通Java應用程式的例子。
實際情況,是要在Spring的專案裡用,因此需要做一些改造。


1.配置檔案
C:\hanhai\config\mongodb.properties
mongodb.host=172.17.100.150
mongodb.port=27017
mongodb.db=zrb


2.配置檔案對應的實體類
 public class MongodbConfig {
	private String host;
	private String port;
	private String db;
}




3.Spring掃描配置檔案
<context:property-placeholder
		location="file:${zhaorongbao.config_path}/config/mongodb.properties"
		ignore-unresolvable="true" />



4.屬性到實體類
<bean id="mongodbConfig" class="com.hanhai.zrb.api.mongodb.MongodbConfig">
    <property name="host" value="${mongodb.host}"></property>
    <property name="port" value="${mongodb.port}"></property>
    <property name="db" value="${mongodb.db}"></property>
</bean>




5.Spring工具類,獲得容器中的物件
需要掃描這個類SpringContextUtil
<context:component-scan base-package="com.hanhai.zrb.api.mongodb" />
SpringContextUtil在com.hanhai.zrb.api.mongodb這個包中。


@Component
public class SpringContextUtil implements ApplicationContextAware{


	private static ApplicationContext ctx;
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.ctx = applicationContext;		
	}
	
	public static ApplicationContext getCtx(){
		return ctx;
	}
	
	public static Object getBean(String name) throws BeansException { 
        return ctx.getBean(name); 
	} 


}






6.SpringMVC中使用。
@
Controller
@RequestMapping("mongodb")
public class MongodbTestController extends BaseController {


	@RequestMapping("test")
	public void test(HttpServletResponse response) throws IOException {
		DB db = MongoUtil.db();


		ProjectDetail projectDetail = buildProjectDetail();
		DBCollection projectDetailCollection = db
				.getCollection("projectDetail");
	}



7.MongoUtil工具類。
import java.net.UnknownHostException;


import org.apache.log4j.Logger;


import com.mongodb.DB;
import com.mongodb.Mongo;


public class MongoUtil {
	public static final int DEFAULT_PORT = 27017;
	public static final String DEFAULT_HOST = "172.17.100.150";


	public static Logger log = Logger.getLogger(MongoUtil.class);
	
	private static Mongo instance;


	//@Resource(name="mongodbConfig")
	//private static MongodbConfig mongodbConfig;
	//沒有直接注入
	private static MongodbConfig config = null;
	
	public static Mongo mongo() {
		//使用工具方法獲得容器中的物件
		Object object=SpringContextUtil.getBean("mongodbConfig");
		
		if(object instanceof MongodbConfig){
			config = (MongodbConfig)object;
		}else{
			log.error("Mongodb config error~");
		}
		try {
			if (instance == null) {
				instance = new Mongo(config.getHost(), Integer.parseInt(config.getPort()));
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return instance;
	}


	public static DB db(){
		Mongo mongo = MongoUtil.mongo();
		DB db = mongo.getDB(config.getDb());
		if(db == null){
			throw new RuntimeException("Mongo db is null");
		}
		return db;
	}
	
	public static void close() {
		if (instance != null) {
			instance.close();
		}
	}
}

相關文章