首页 > 其他分享 >SpringContextUtils

SpringContextUtils

时间:2024-05-15 17:35:03浏览次数:25  
标签:return String clazz userDir SpringContextUtils uploadPath public

@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

相关文章

  • SpringContextUtils
    importorg.springframework.beans.BeansException;importorg.springframework.beans.factory.NoSuchBeanDefinitionException;importorg.springframework.context.ApplicationContext;importorg.springframework.context.ApplicationContextAware;importorg.springfr......