ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-06-30 14:35:28  阅读:187  来源: 互联网

标签:applicationContext YmsSessionMangager return spring YmsSessionMangagerYhtApp jar


场景: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();
}

 

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

标签:applicationContext,YmsSessionMangager,return,spring,YmsSessionMangagerYhtApp,jar
来源: https://www.cnblogs.com/acm-bingzi/p/jar_spring_bean.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有