All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
info.baseinsight.core.SpringUtils Maven / Gradle / Ivy
package info.baseinsight.core;
import org.springframework.beans.BeansException;
import org.springframework.context.*;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@Component
public class SpringUtils implements ApplicationContextAware, EnvironmentAware, MessageSourceAware, ResourceLoaderAware {
private static ApplicationContext applicationContext;
private static Environment environment;
private static MessageSource messageSource;
private static ResourceLoader resourceLoader;
static void init4UnitTest(ApplicationContext applicationContext,Environment environment,MessageSource messageSource,ResourceLoader resourceLoader){
SpringUtils.applicationContext=applicationContext;
SpringUtils.environment=environment;
SpringUtils.messageSource=messageSource;
SpringUtils.resourceLoader=resourceLoader;
}
public static Object doMethod(Object obj,String methodName,List> parameters) {
try {
List parameterTypes=new ArrayList();
List args=new ArrayList();
parameters.forEach(row->{
parameterTypes.add((Class)row.get("class"));
args.add(row.get("value"));
});
return obj.getClass().getMethod(methodName, parameterTypes.toArray(new Class[]{})).invoke(obj, args.toArray());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
if (SpringUtils.applicationContext == null) {
SpringUtils.applicationContext = arg0;
}
}
@Override
public void setEnvironment(Environment arg0) throws BeansException {
if (SpringUtils.environment == null) {
SpringUtils.environment = arg0;
}
}
public static Environment getEnvironment(){
return environment;
}
@Override
public void setMessageSource(MessageSource arg0) throws BeansException {
if (SpringUtils.messageSource == null) {
SpringUtils.messageSource = arg0;
}
}
@Override
public void setResourceLoader(ResourceLoader arg0) throws BeansException {
if (SpringUtils.resourceLoader == null) {
SpringUtils.resourceLoader = arg0;
}
}
// 获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
// 获取config info,application.yml中的配置信息
public static String getConfiginfo(String key) {
return environment.getProperty(key);
}
public static boolean containsConfiginfo(String key) {
return environment.containsProperty(key);
}
// 获取i18n的资源信息
public static String getI18nMessage(String code, List arguments, String defaultMessage, Locale locale) {
return messageSource.getMessage(code,arguments.toArray(),defaultMessage,locale);
}
// 获取i18n的资源信息
public static String getI18nMessage(String code,List arguments,String defaultMessage) {
return messageSource.getMessage(code,arguments.toArray(),defaultMessage, LocaleContextHolder.getLocale());
}
// 获取i18n的资源信息
public static String getI18nMessage(String code,List arguments) {
return messageSource.getMessage(code,arguments.toArray(),"",LocaleContextHolder.getLocale());
}
// 获取i18n的资源信息
public static String getI18nMessage(String code) {
return messageSource.getMessage(code, new Object[]{},"",LocaleContextHolder.getLocale());
}
// 获取resource
//ResourceLoader在进行加载资源时需要使用前缀来指定需要加载:
// “classpath:path”表示返回ClasspathResource,
// “http://path”和“file:path”表示返回UrlResource资源,
// 如果不加前缀则需要根据当前上下文来决定
public static Resource getResource(String resource) {
return resourceLoader.getResource(resource);
}
// 通过name获取 Bean.
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
public static boolean containsBean(String name) {
return getApplicationContext().containsBean(name);
}
// 通过class获取Bean.
public static T getBean(Class clazz) {
return getApplicationContext().getBean(clazz);
}
// 通过name,以及Clazz返回指定的Bean
public static T getBean(String name, Class clazz) {
return getApplicationContext().getBean(name, clazz);
}
//support event
public static boolean publishEvent(Object event){
if(SpringUtils.getApplicationContext()!=null){
SpringUtils.applicationContext.publishEvent(event);
return true;
}else{
return false;
}
}
//
/* public static boolean publishEvent(AppEvent event){
if(SpringUtils.getApplicationContext()!=null){
SpringUtils.applicationContext.publishEvent(event);
return true;
}else{
return false;
}
}*/
public static boolean addApplicationListener(ApplicationListener> listener){
if(SpringUtils.getApplicationContext()!=null){
((ConfigurableApplicationContext)SpringUtils.applicationContext).addApplicationListener(listener);
return true;
}else{
return false;
}
}
}