Spring get bean object helper class

Used when the bean object cannot be obtained by annotation (in thread or otherwise)

 

package com.flc.util;

import java.util.Locale;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
* bean object instantiation tool
* used for thread manipulation of bean objects
* @author Administrator
*
*/
public class SpringContextUtil implements ApplicationContextAware{

private static ApplicationContext context = null;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
this.context = applicationContext;
}

@SuppressWarnings("unchecked")
public static <T> T getBean(String beanName){
return (T) context.getBean(beanName);
}

public static String getMessage(String key){
return context.getMessage(key, null, Locale.getDefault());
}

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325669261&siteId=291194637