springboot如何加载jar包内配置文件

发布时间:2021-07-07 16:02:42 作者:chen
来源:亿速云 阅读:1666

本篇内容主要讲解“springboot如何加载jar包内配置文件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“springboot如何加载jar包内配置文件”吧!

1、springboot启动 如何加载jar包内注解类?

    springboot的 Application类会自动扫描加载其package 及其子package的类,但是其他package 或 jar中的类不会自动被扫描到, 这时需要配置 扫描路径:

@ComponentScan(basePackages = {"com.ysma.oschina", "com.ysma.csdn"},//指定package
		excludeFilters = {@ComponentScan.Filter(value = Other.class, 
                          type= FilterType.ASSIGNABLE_TYPE)})//排除csdn jar包中个别类
public class OschinaApplication {

	public static void main(String[] args) {
		SpringApplication.run(Oschina.class, args);
	}

}

    basePackages:参数是用于扫描带注释组件的基本包

    basePackageClasses:对basepackages()指定扫描注释组件包类型安全的替代

    excludeFilters:指定不适合组件扫描的类型

    includeFilters:指定哪些类型有资格用于组件扫描

    lazyInit:指定是否应注册扫描的beans为lazy初始化

    useDefaultFilters:指示是否自动检测类的注释

    ANNOTATION:注解类型

    ASSIGNABLE_TYPE:ANNOTATION:指定的类型

    ASPECTJ:按照Aspectj的表达式,基本上不会用到

    REGEX:按照正则表达式

    CUSTOM:自定义规则  需实现自定义FilterType 如下:

 public class MyFilterType implements TypeFilter {
 
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
 
        if (metadataReader.getClassMetadata().getClassName().contains("Department")){
            //获取当前类注解的信息
            AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
            for (String s : annotationMetadata.getAnnotationTypes()) {
                System.out.println("当前正在被扫描的类注解类型" + s);
            }
            //获取当前正在扫描类的信息
            ClassMetadata classMetadata = metadataReader.getClassMetadata();
            System.out.println("当前正在被扫描的类的类名" + classMetadata.getClassName());
            //获取当前类的资源信息(类存放的路径...)
            Resource resource = metadataReader.getResource();
            System.out.println("当前正在被扫描的类存放的地址" + resource.getURL());
            return true;
        }
        return false;
    }
}

2、jar包内加载资源

     resource工程路径不行,会自动指定到jar包所在工程资源路径,如 /config/overview.properties

     resource 类路径不行,会自动指定到jar包所在工程类路径,如 config/overview.properties

     classpath路径可行,会自动识别并加载类路径,如classpath:config/overview.properties   此时配置文件放在父工程的classpath目录下

    ResourceUtil实例如下

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.net.URL;

/**
 * 资源加载工具
 * 本应用为jar包应用,主要使用classpath路径
 * @see this getClassPathFile
 */
@Slf4j
public class ResourceUtil {

    private static final String PATH_WINDOWS = "\\\\";

    private static final String PATH_LINUX = "/";

    /**
     * 读取jar包文件
     * @param pathName 路径
     * @return inputStream
     * @throws FileNotFoundException
     */
    public static URL getJarURL(String pathName) throws FileNotFoundException {
        try {
            ClassPathResource classPathResource = new ClassPathResource(pathName);
            return classPathResource.getURL();
        } catch (IOException e) {
            log.error("ResourceUtil.getResourcePath 文件不存在, path:{}", pathName);
            throw new FileNotFoundException(pathName + "文件不存在");
        }
    }

    /**
     * jar包内文件的获取
     * @param classPath 因为性对路径问题,通过classpath获取比较好
     * @return 文件 classpath:config/overview.properties
     * @throws FileNotFoundException ex
     */
    @Deprecated
    public static File getClassPathFile(String classPath) throws FileNotFoundException {
        try {
            return ResourceUtils.getFile(classPath);
        } catch (FileNotFoundException e) {
            log.error("ResourceUtil.getResourcePath 文件不存在, path:{}", classPath);
            throw new FileNotFoundException(classPath + "文件不存在");
        }
    }

    /**
     * 获取文件路径
     * @param path 文件路径
     * @return URL
     */
    public static URL getResourcePath(String path) throws FileNotFoundException {
        try {
            //1.以Linux路径为准
            path = path.replaceAll(PATH_WINDOWS, PATH_LINUX);

            /*
              2.依据开头自主选择加载方法
              第一:前面有 "/" 代表了工程的根目录,例如工程名叫做myproject,"/"代表了myproject
              第二:前面没有 "" 代表当前类的目录
             */
            return path.startsWith(PATH_LINUX) ?
                    ResourceUtil.class.getResource(path) :
                    ResourceUtil.class.getClassLoader().getResource(path);
        } catch (Exception e) {
            log.error("ResourceUtil.getResourcePath 文件不存在, path:{}", path);
            throw new FileNotFoundException(path + "文件不存在");
        }
    }

    /**
     * 获取文件
     * @see #getJarURL(String path)
     */
    public static File getJarFile(String path) throws FileNotFoundException {
        try {
            ClassPathResource classPathResource = new ClassPathResource(path);
            InputStream is = classPathResource.getInputStream();
            File tempFile = File.createTempFile("groovy", null);
            IOUtils.copy(is, new FileOutputStream(tempFile));
            return tempFile;
        } catch (IOException e) {
            log.error("ResourceUtil.getResourcePath 文件不存在, path:{}", path);
            throw new FileNotFoundException(path + "文件不存在");
        }
    }
}

3、classpath路径问题   如上2中如果配置文件放置在jar包中,则class地址应为:classpath*:config/overview.properties

    classpath和classpath*区别: 

    A:classpath:只会到你的class路径中查找找文件。

    B:classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找。

    C:在多个classpath中存在同名资源,都需要加载时,那么用classpath:只会加载第一个,这种情况下也需要用classpath*:前缀。

    注意: 用classpath*:需要遍历所有的classpath,所以加载速度是很慢的;因此,在规划的时候,应该尽可能规划好资源文件所在的路径,尽量避免使用classpath*。

到此,相信大家对“springboot如何加载jar包内配置文件”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. springboot加载多个配置文件
  2. SpringBoot加载子模块配置文件的案例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring spring boot

上一篇:C#中怎么自定义事件

下一篇:C#中怎么生成自定义事件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》