非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(); }
原创文章,欢迎转载,转载请注明出处!
标签:applicationContext,YmsSessionMangager,return,spring,YmsSessionMangagerYhtApp,jar 来源: https://www.cnblogs.com/acm-bingzi/p/jar_spring_bean.html