微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

非spring的 jar 包里创建一个bean,但是不自动引入代码库

场景:sdk 里获取spring 的bean

 

1、先新建一个

 1 package com.yonyou.yht.bean;
 2 
 3 import com.yonyou.iuap.yms.session.YmsSessionMangager;
 4 import org.springframework.beans.BeansException;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.ApplicationContextAware;
 7 
 8 public class YmsSessionMangagerYhtApp implements ApplicationContextAware {
 9 
10     private static ApplicationContext applicationContext;
11 
12     @Override
13     public synchronized void setApplicationContext(ApplicationContext applicationContext)
14             throws BeansException {
15         YmsSessionMangagerYhtApp.applicationContext = applicationContext;
16     }
17 
18     public static YmsSessionMangager getYmsSessionMangagerBean() {
19         if (applicationContext == null) {
20             return null;
21         }
22         return applicationContext.getBean(YmsSessionMangager.class);
23     }
24 }

 

2、SDK里调用的时候

private static Map<String, String> getSession2RemoteStorage(String token) {
   try {
      YmsSessionMangager bean = YmsSessionMangagerYhtApp.getYmsSessionMangagerBean();
      if (bean == null) {
         return null;
      }
      return bean.getSession2RemoteStorage(token);
   } catch (Exception e) {
      logger.error("UserCenter saveSession2Storage error, [msg = {}]", e.getMessage(), e);
      return null;
   }
}

 

3、项目里调用的时候,需要注入这个bean

@Bean("ymsSessionMangagerYhtApp")
public YmsSessionMangagerYhtApp ymsSessionMangagerYhtApp() {
    return new YmsSessionMangagerYhtApp();
}

 

原创文章,欢迎转载,转载请注明出处!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐