如何以编程方式获得入门级课程?

时间:2019-02-15 16:52:20

标签: spring-boot

如何从BOOT-INF / lib内的jar中的代码以编程方式获取Start-Class清单属性?

我知道我可以获取LaunchURLClassLoader,调用getResourceAsStream(“ META-INF / MANIFEST.MF”),然后在MANIFEST.MF中查找属性,但我不确定这是最好的方法

2 个答案:

答案 0 :(得分:0)

您的方法很好,可能会起作用,但是您可以将抽象水平提高一点,并使用Spring的Resource Loader

Resource template = ctx.getResource("classpath:/path/to/manifest");

答案 1 :(得分:0)

public class UserAgentSpringBootApplicationNameDiscoverer {

    protected static final String BOOT_INF_DIR = "BOOT-INF/";

    protected static final String MANIFEST_DIR = "META-INF/MANIFEST.MF";

    public String getStartClassFromClassLoader(ClassLoader cl) {

        URL bootInfUrl = cl.getResource(BOOT_INF_DIR);
        if (bootInfUrl == null || "".equals(bootInfUrl.toString().trim())) {
            return null;
        }

        String rootMetaInfUrl = bootInfUrl.toString().replace(BOOT_INF_DIR, MANIFEST_DIR);
        InputStream is = cl.getResourceAsStream(rootMetaInfUrl.toString());
        if (is == null) {
            return null;
        }
        Manifest manifest = null;

        try {
            manifest = new Manifest(is);
        } catch (IOException e) {
            return null;
        }
        return manifest.getMainAttributes().getValue("Start-Class");

    }
}