Spring工具类

在编写Web代码,可能需要用到工具类或者没有加入到Spring Bean工厂的类,能够访问到ApplicationContext、bean和配置项,这时,因为该类没有加入到Spring容器中,不能使用@Autowired等注解来注入Spring中的Bean,这是可以通过一个工具类完成这件事。

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
 
import javax.servlet.ServletContext;
import java.util.Collections;
import java.util.Map;
 
@Configuration
public class ApplicationContextExt implements ApplicationContextAware {
    private static ApplicationContext context = null;
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextExt.context = applicationContext;
    }
 
    /**
     * 取得存储在静态变量中的ApplicationContext.
     */
    public static ApplicationContext getContext() {
        if (context == null)
            throw new IllegalStateException("can not found application context.");
        return context;
    }
 
    /**
     * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        if (context == null)
            throw new IllegalStateException("can not find application context.");
        try {
            return (T) context.getBean(name);
        } catch (BeansException e) {
 
        }
        return (T) null;
    }
 
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name, ServletContext sc) {
        if (sc == null)
            throw new IllegalStateException("can not find servlet context.");
        try {
            return (T) WebApplicationContextUtils.getWebApplicationContext(sc).getBean(name);
        } catch (BeansException e) {
 
        }
        return (T) null;
    }
 
 
    public static <T> T getBean(Class<T> cls) {
        if (context == null)
            throw new IllegalStateException("can not find application context.");
        try {
            return (T) context.getBean(cls);
        } catch (BeansException e) {
 
        }
        return (T) null;
    }
 
    public static <T> T getBean(Class<T> cls, ServletContext sc) {
        if (sc == null)
            throw new IllegalStateException("can not find servlet context.");
        try {
            return (T) WebApplicationContextUtils.getWebApplicationContext(sc).getBean(cls);
        } catch (BeansException e) {
 
        }
        return (T) null;
    }
 
    public static <T> Map<String, T> getBeansOfType(Class<T> cls) {
        if (context == null)
            throw new IllegalStateException("can not found application context.");
        try {
            return context.getBeansOfType(cls);
        } catch (BeansException e) {
 
        }
        return Collections.emptyMap();
    }
 
    public static <T> Map<String, T> getBeansOfType(Class<T> cls, ServletContext sc) {
        if (sc == null) {
            throw new IllegalStateException("can not find servlet context.");
        }
        try {
            return WebApplicationContextUtils.getWebApplicationContext(sc).getBeansOfType(cls);
        } catch (BeansException e) {
            // TODO: handle exception
        }
        return Collections.emptyMap();
    }
 
    /**
     * 根据WebApp的虚拟路径获取文件的绝对路径
     *
     * @param path
     * @return
     */
    public static String getAbsolutePathInWeb(String path) {
        return ((WebApplicationContext) context).getServletContext().getRealPath(path);
    }
 
    public static Resource getResource(String location) {
        return context.getResource(location);
    }
 
}

比如:
策略工厂中
获取到Bean
ApplicationContextExt.getBean(AService.class);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值