SpringConteext Holder使用エラー及び@Lazy使用

13631 ワード

  • 今日はSpringConteext Holderを使ってbeanを取得した時にエラーが発生しました.エラーは下記の通りです.
    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)
    
  • ソリューション:
  • spring-mvc.xmlに構成を入れて解決する(spring Conteext Holderをアクティブに注入する):
  • 
    	
    
  • Spring Contect Holder類に注釈@Service、@Lazy(false)
  • を追加します.
  • Spring Contingt Holderの具体的な実現
  • 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");
            }
        }
     
    }
    
  • @Lazy注は、ビーンがプリ初期化をキャンセルするかどうかを指定するために使用される.主にSpring Bean類を修飾するために用いられ、このBeanの初期化挙動lazyは遅延負荷を表す.1.lazy=falseは、遅延しないことを表しています.対象AにまだオブジェクトBの参照がある場合、Aのxmlマッピングファイルにbのオブジェクト参照を配置します.複数のペアまたは複数のペアで、遅延しないことは、対象Aを検索した場合、BオブジェクトもAオブジェクトの参照に入れます.AペアのBオブジェクトは値があります.2.lazy=trueは遅延を表しています.Aオブジェクトを調べる時、Bオブジェクトも調べられません.AオブジェクトのBオブジェクトを使う時だけ調べます.デフォルトはfalseのようです.バックグラウンドのsql文の変化を見てもいいです.最適化の効率が必要な時に使います.