使用SpringContextHolder獲取bean實例

1,介紹與使用
SpringContextHolder工具類的方法getBean能夠獲取bean的實例。可是工具類的方法是否是static的,等同於@Autowired 注入。

解決問題:java

類中方法是static,方法中使用的對象也必須是static,但正常狀況下@Autowired沒法注入靜態的bean,因而發現項目中用到了springContextHolder,經過使用;spring

UserService userService = SpringContextHolder.getBean(UserService.class);   // 獲取bean

 

2.異常  

使用SpringContextHolder來獲取一個bean報異常:apache

Java.lang.IllegalStateException: applicaitonContext屬性未注入, 請在applicationContext.xml中定義SpringContextHolder.
    at org.apache.commons.lang3.Validate.validState(Validate.java:826)
    at com.jituan.common.util.SpringContextHolder.assertContextInjected(SpringContextHolder.java:79)
    at com.jituan.common.util.SpringContextHolder.getBean(SpringContextHolder.java:41)
    at com.jituan.common.message.util.sms.SmsUtil.querySmsTemplate(SmsUtil.java:206)
    at com.jituan.common.message.util.sms.SmsUtil.send(SmsUtil.java:76)
    at com.jituan.common.message.processer.SmsProcesser.send(SmsProcesser.java:37)
    at com.jituan.batch.msghandler.MessageHandler.smsSend(MessageHandler.java:106)
    at com.jituan.batch.msghandler.MessageHandler$SmsTread.run(MessageHandler.java:185)

1)配置spring-mvc.xmlspring-mvc

<!-- 設全局變量以即可以得到對應的注入bean -->
<bean id="springContextHolder" class="com.jituan.common.util.SpringContextHolder" />

2)在SpringContextHolder類上加入註解@Service、@Lazy(false)mvc

import java.util.Map;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 
/**
 *
 *以靜態變量保存Spring ApplicationContext, 可在任何代碼任何地方任什麼時候候中取出ApplicaitonContext.
 * @author Bucky
 */
@Service
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware{
 
    private static ApplicationContext applicationContext;
 
     
    //實現ApplicationContextAware接口的context注入函數, 將其存入靜態變量.
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextHolder.applicationContext = applicationContext;
    }
 
    
    //取得存儲在靜態變量中的ApplicationContext.
    public static ApplicationContext getApplicationContext() {
        checkApplicationContext();
        return applicationContext;
    }
     
    //從靜態變量ApplicationContext中取得Bean, 自動轉型爲所賦值對象的類型.
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        checkApplicationContext();
        return (T) applicationContext.getBean(name);
    }
 
     
    //從靜態變量ApplicationContext中取得Bean, 自動轉型爲所賦值對象的類型.
    //若是有多個Bean符合Class, 取出第一個.
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
        checkApplicationContext();
        @SuppressWarnings("rawtypes")
                Map beanMaps = applicationContext.getBeansOfType(clazz);
        if (beanMaps!=null && !beanMaps.isEmpty()) {
            return (T) beanMaps.values().iterator().next();
        } else{
            return null;
        }
    }
 
    private static void checkApplicationContext() {
        if (applicationContext == null) {
            throw new IllegalStateException("applicaitonContext未注入,請在applicationContext.xml中定義SpringContextHolder");
        }
    }
 
}
相關文章
相關標籤/搜索