The first one : Set when container is loaded
public class ProxyApplication { public static void main(String[] args) {
ApplicationContext applicationContext =
SpringApplication.run(ProxyApplication.class, args);
SpringContextUtil.setApplicationContext(applicationContext); } }
instantiation SpringContextUtil
public class SpringContextUtil { // Spring Application context private static
ApplicationContext applicationContext; /** *
realization ApplicationContextAware Interface callback method , Setting context */ public static void
setApplicationContext(ApplicationContext applicationContext)throws
BeansException { SpringContextUtil.applicationContext = applicationContext; }
public static ApplicationContext getApplicationContext() { return
applicationContext; } /** * according to beanId return Spring Examples in * @Date 2019-08-07 17:36 *
@param * @return **/ public static Object getBean(String beanId) throws
BeansException { return applicationContext.getBean(beanId); } }
Usage scenarios : Initialization information in entity , Loading class information into the container
@Data @Configuration public class AutoLogonConfig { private boolean autoLogon
= false; private String loginName = ""; private String password = ""; private
String ip = ""; public AutoLogonConfig(@Value("${loginFilePath}") String
loginFilePath){ Yaml yaml = new Yaml(); InputStream in; try { File file = new
File(loginFilePath); in = new FileInputStream(file); Map map = (Map)
yaml.load(in); Pub.loginFilePath = loginFilePath; this.autoLogon=(boolean)
map.get("autoLogon"); this.loginName=(String) map.get("loginName");
this.password=(String) map.get("password"); this.ip=(String) map.get("ip"); }
catch (IOException e) { e.printStackTrace(); } } } AutoLogonConfig
autoLogonConfig = (AutoLogonConfig)
SpringContextUtil.getBean("autoLogonConfig");
The second kind : inherit ApplicationContextAware
@Component public class ApplicationContextUtil implements
ApplicationContextAware { /** * obtain bean * @param name * @return * @throws
BeansException */ public static Object getBean(String name) throws
BeansException { Object o = applicationContext.getBean(name); return o; }
private static ApplicationContext applicationContext; /** * Set the
ApplicationContext that this object runs in. * Normally this call will be used
to initialize the object. * <p>Invoked after population of normal bean
properties but before an init callback such * as {@link
InitializingBean#afterPropertiesSet()} * or a custom init-method. Invoked after
{@link ResourceLoaderAware#setResourceLoader}, * {@link
ApplicationEventPublisherAware#setApplicationEventPublisher} and * {@link
MessageSourceAware}, if applicable. * * @param applicationContext the
ApplicationContext object to be used by this object * @throws
ApplicationContextException in case of context initialization errors * @throws
BeansException if thrown by application context methods * @see
BeanInitializationException */ @Override public void
setApplicationContext(ApplicationContext applicationContext) throws
BeansException { ApplicationContextUtil.applicationContext =
applicationContext; } public static ApplicationContext getApplicationContext(){
return applicationContext; } }
Usage scenarios : It needs to go through the reflection mechanism
public Map<String,String> functionOperation(ProxyParamDTO params,
OperationEntity operationEntity) throws Exception{ Map<String,String> m = null;
String className; String functionName; // [0]:ClassName, [1];functionName
String[] arr = getFunctionName(operationEntity); className = arr[0];
functionName = arr[1]; operationEntity.setParamDto(params); try { // getBean
Object o = ApplicationContextUtil.getBean(className); // getClass Class<?>
clazz = o.getClass(); // getMethod by class Method method =
clazz.getMethod(functionName, OperationEntity.class); // execute method and get
returned value m = (HashMap) method.invoke(o, operationEntity);
System.out.println(operationEntity.getOperationOrder() + " : " +
method.getName()); } catch (NoSuchMethodException | BeansException e) {
otherFunctions(operationEntity, arr); } catch (IllegalAccessException |
InvocationTargetException e) { e.printStackTrace(); throw e; } return m; }
 

Technology