非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(); }
原创文章,欢迎转载,转载请注明出处!
把每一件简单的事情做好,就是不简单;把每一件平凡的事情做好,就是不平凡!相信自己,创造奇迹~~