码迷,mamicode.com
首页 > 编程语言 > 详细

SpringUtil

时间:2014-07-30 03:15:32      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

/** SpringUtil.java

{{IS_NOTE
    Purpose:
        
    Description:
        
    History:
        Thu Jun  1 13:53:53     2006, Created by henrichen
}}IS_NOTE

Copyright (C) 2006 Potix Corporation. All Rights Reserved.

{{IS_RIGHT
}}IS_RIGHT
*/
package org.zkoss.zkplus.spring;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zkoss.zk.ui.Execution;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.UiException;

/***
 * SpringUtil, a Spring utility.
 * <p>Applicable to Spring Framework version 2.x or later</p>
 * @author henrichen
 */
public class SpringUtil {
    /***
     * Get the spring application context.
     */
    public static ApplicationContext getApplicationContext() {
        Execution exec = Executions.getCurrent();
        if (exec == null) {
            throw new UiException("SpringUtil can be called only under ZK environment!");
        }
        
        return WebApplicationContextUtils.getRequiredWebApplicationContext(
                (ServletContext)exec.getDesktop().getWebApp().getNativeContext());
    }
    
    /***
     * Get the spring bean by the specified name.
     */        
    public static Object getBean(String name) {
        Object o = null;
        try {
            if(getApplicationContext().containsBean(name)) {
                o = getApplicationContext().getBean(name);
            }
        } catch (NoSuchBeanDefinitionException ex) {
            // ignore
        }
        return o;
    }

    /***
     * Get the spring bean by the specified name and class.
     */        
    public static Object getBean(String name, Class cls) {
        Object o = null;
        try {
            if(getApplicationContext().containsBean(name)) {
                o = getApplicationContext().getBean(name, cls);
            }
        } catch (NoSuchBeanDefinitionException ex) {
            // ignore
        } catch (BeanNotOfRequiredTypeException e) {
            // ignore
        }
        return o;
    }
}

转载:http://www.oschina.net/code/explore/zk-src-5.0.5/zkplus/src/org/zkoss/zkplus/spring/SpringUtil.java

SpringUtil,布布扣,bubuko.com

SpringUtil

标签:des   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/zhao123/p/3876824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!