@Component
public class SpringContextUtils implements ApplicationContextAware, EnvironmentAware {
private static ApplicationContext applicationContext;
private static Environment environment;
@Override
public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
SpringContextUtils.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setEnvironment(@NonNull Environment environment) {
SpringContextUtils.environment = environment;
}
/**
* 获取文件存储filePath
* @return String
*/
public static String getUploadPath(){
String uploadPath = environment.getProperty("hs-qybm.uploadPath");
if(StrUtil.isNotBlank(uploadPath)){
return uploadPath;
}
//如果不配置,用约定的目录
String userDir = System.getProperty("user.dir");
userDir = userDir.substring(0, userDir.length() - 3) + "uploadFile/";
return userDir + "other/";
}
/**
* 获取报表文件存储filePath
* @return String
*/
public static String getReportUploadPath(){
String uploadPath = environment.getProperty("hs-qybm.uploadPath");
if(StrUtil.isNotBlank(uploadPath)){
return uploadPath;
}
//如果不配置,用约定的目录
String userDir = System.getProperty("user.dir");
userDir = userDir.substring(0, userDir.length() - 3) + "uploadFile/";
return userDir + "report/";
}
/**
* 获取Bean
* @param clazz clazz
* @param <T> 泛型
* @return <T>
*/
public static <T> T getBean(Class<T> clazz){
try {
return applicationContext.getBean(clazz);
}catch (BeansException e){
return null;
}
}
/**
* 获取Bean
* @param clazz clazz
* @param <T> 泛型
* @return <T>
*/
public static <T> T getBean(Class<T> clazz,String name){
try {
return applicationContext.getBean(name,clazz);
}catch (BeansException e){
return null;
}
}
}
标签:return,String,clazz,userDir,SpringContextUtils,uploadPath,public
From: https://www.cnblogs.com/xyarch/p/18194338