在普通類中調用Service,Dao的方法

本方法摘取自開源項目:ThinkGem 王震 / JeeSitejava

https://git.oschina.net/thinkgem/jeesitegit

首先引入jar包 spring

建立SpringContextHolder類apache

/**
 * Copyright © 2012-2013  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.code.platform.common.utils;

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 以靜態變量保存Spring ApplicationContext, 可在任何代碼任何地方任什麼時候候取出ApplicaitonContext.
 * 
 * @author Zaric
 * @date 2013-5-29 下午1:25:40
 */
//@Service
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {

	private static ApplicationContext applicationContext = null;

	private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);

	/**
	 * 取得存儲在靜態變量中的ApplicationContext.
	 */
	public static ApplicationContext getApplicationContext() {
		assertContextInjected();
		return applicationContext;
	}

	/**
	 * 從靜態變量applicationContext中取得Bean, 自動轉型爲所賦值對象的類型.
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		assertContextInjected();
		return (T) applicationContext.getBean(name);
	}

	/**
	 * 從靜態變量applicationContext中取得Bean, 自動轉型爲所賦值對象的類型.
	 */
	public static <T> T getBean(Class<T> requiredType) {
		assertContextInjected();
		return applicationContext.getBean(requiredType);
	}

	/**
	 * 清除SpringContextHolder中的ApplicationContext爲Null.
	 */
	public static void clearHolder() {
		logger.debug("清除SpringContextHolder中的ApplicationContext:"
				+ applicationContext);
		applicationContext = null;
	}

	/**
	 * 實現ApplicationContextAware接口, 注入Context到靜態變量中.
	 */
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) {
//		logger.debug("注入ApplicationContext到SpringContextHolder:{}", applicationContext);

		if (SpringContextHolder.applicationContext != null) {
			logger.warn("SpringContextHolder中的ApplicationContext被覆蓋, 原有ApplicationContext爲:" + SpringContextHolder.applicationContext);
		}

		SpringContextHolder.applicationContext = applicationContext; // NOSONAR
	}

	/**
	 * 實現DisposableBean接口, 在Context關閉時清理靜態變量.
	 */
	@Override
	public void destroy() throws Exception {
		SpringContextHolder.clearHolder();
	}

	/**
	 * 檢查ApplicationContext不爲空.
	 */
	private static void assertContextInjected() {
		Validate.validState(applicationContext != null, "applicaitonContext屬性未注入, 請在applicationContext.xml中定義SpringContextHolder.");
	}
}

在applicationContext.xml中加入配置app

<!-- 獲取bean工具注入 -->
    <bean id="springContextHolder" class="com.code.platform.common.utils.SpringContextHolder" lazy-init="false"/>

調用方法框架

// Service
SystemConfigService srv = SpringContextHolder.getBean(SystemConfigService.class);

// Dao
DictDao dictDao = SpringContextHolder.getBean(DictDao.class);

這樣就能夠在非spring託管對象獲取到spring託管對象了ide

====================================================================================工具

以上的方式尚未在其餘框架中嘗試,若是總結的不全能夠參考原框架的代碼 ThinkGem 王震 / JeeSiteui

相關文章
相關標籤/搜索