SpringConteext Holder使用エラー及び@Lazy使用
13631 ワード
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)
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");
}
}
}