Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从spring实例化的POJO类(不是servlet)中读取manifest.mf?_Java_Spring_Servlets_Manifest.mf - Fatal编程技术网

Java 如何从spring实例化的POJO类(不是servlet)中读取manifest.mf?

Java 如何从spring实例化的POJO类(不是servlet)中读取manifest.mf?,java,spring,servlets,manifest.mf,Java,Spring,Servlets,Manifest.mf,我有一个名为BuildNumberService的简单服务,它将由spring实例化 我试图为类找到最干净的代码,以便从它打包到的jar文件中找到MANIFEST.MF文件 代码必须在servlet容器中运行 @Service public class BuildNumberService { private static final String IMPLEMENTATION_BUILD = "Implementation-Build"; private String ve

我有一个名为BuildNumberService的简单服务,它将由spring实例化

我试图为类找到最干净的代码,以便从它打包到的jar文件中找到MANIFEST.MF文件

代码必须在servlet容器中运行

@Service
public class BuildNumberService {

    private static final String IMPLEMENTATION_BUILD = "Implementation-Build";

    private String version = null;

    public BuildNumberService() {

      // find correct manifest.mf
      // ?????


      // then read IMPLEMENTATION_BUILD attributes and caches it.
       Attributes mainAttributes = mf.getMainAttributes();
       version = mainAttributes.getValue(IMPLEMENTATION_BUILD);
    }

    public String getVersion() {
        return this.version;
    }
}
你会怎么做


编辑:实际上,我想做的是,按名称查找与实际类位于同一个包中的资源。

如果您知道jar在文件系统中(例如,不在war中),并且您还知道安全管理器授予您访问类的保护域的权限,您可以这样做:

public static InputStream findManifest(final Class<?> clazz) throws IOException,
    URISyntaxException{
    final URL jarUrl =
        clazz.getProtectionDomain().getCodeSource().getLocation();
    final JarFile jf = new JarFile(new File(jarUrl.toURI()));
    final ZipEntry entry = jf.getEntry("META-INF/MANIFEST.MF");
    return entry == null ? null : jf.getInputStream(entry);
}
公共静态InputStream findManifest(最终类clazz)引发IOException,
URISyntaxException{
最终URL jarUrl=
clazz.getProtectionDomain().getCodeSource().getLocation();
final JarFile jf=newjarfile(新文件(jarUrl.toURI());
最终ZipEntry条目=jf.getEntry(“META-INF/MANIFEST.MF”);
返回条目==null?null:jf.getInputStream(条目);
}

我找到了另一种不用引用servlet上下文就加载MANIFEST.MF的解决方案,只需使用Spring框架即可

我在Spring3.1.2.RELEASE中使用了这种配置,但我相信它在以后的版本中应该可以正常工作

首先,在applicationContext.xml中编写以下bean


My MANIFEST.MF位于$WEBAPP_ROOT/META-INF/文件夹下,我已在Apache Tomcat 7和WildFly 8上成功测试了此解决方案。

Psshh@那太容易了。让我自己做腿部工作:-)(老实说,我不知道)这是一个类找到包装它的罐子的唯一方法吗?jdk似乎使在运行时读取这些元数据变得困难。好像这是不应该做的。我将其改写为:只有真正知道自己在做什么的人才能完成(例如,具有组件扫描功能的框架,如hibernate、spring、seam等,必须使用这样的功能)。但是,是的,我想这是我想你能看到答案的唯一方法。看看一个为你做所有这些工作的实用类
Manifest mf = new Manifest();
Resource resource = new ClassPathResource("META-INF/MANIFEST.MF");
                    InputStream is= resource.getInputStream();
                        mf.read(is);
                        if (mf != null) {
                            Attributes atts = mf.getMainAttributes();
                            implementationTitle = atts.getValue("Implementation-Title");
                        }
@Autowired
@Qualifier("manifest")
private Manifest manifest;
Manifest mf = new Manifest();
Resource resource = new ClassPathResource("META-INF/MANIFEST.MF");
                    InputStream is= resource.getInputStream();
                        mf.read(is);
                        if (mf != null) {
                            Attributes atts = mf.getMainAttributes();
                            implementationTitle = atts.getValue("Implementation-Title");
                        }