jar文件中的Spring上下文 [英] Spring context from within a jar file

查看:148
本文介绍了jar文件中的Spring上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring和hibernate在java中开发桌面应用程序。我想将它打包为可执行jar但我在从jar文件中加载上下文配置XML时遇到问题。

I'm developing a desktop application in java using spring and hibernate. I want to package it as a executable jar but I'm having problems loading the context configuration XML from within the jar file.

我将应用程序打包为可运行的jar文件当我运行jar文件时,它告诉我该文件不存在。我知道在jar文件中我应该加载一个InputStream,但是没有支持它的ApplicationContext实现。

I package the application as a runnable jar file and when I run the jar file it tells me that the file doesn't exist. I know that from within a jar file I should load an InputStream but there's no ApplicationContext implementation that supports it.

我相信我必须编写自己的InputStreamXmlApplicationContext和我已经尝试过了。我做了一些研究和一些编码:

I believe that I'll have to code my own InputStreamXmlApplicationContext and I already tried doing it. I did some research and a little coding:


import java.io.InputStream;

import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;

public class InputStreamXmlApplicationContext extends AbstractXmlApplicationContext {

    private Resource[] configResources;

    public InputStreamXmlApplicationContext(InputStream in) {
        InputStreamResource resource = new InputStreamResource(in);
        configResources = new InputStreamResource[] {resource};
        setConfigResources(configResources);
        refresh();
    }

    public Resource[] getConfigResources() {
        return configResources;
    }

    public void setConfigResources(Resource[] configResources) {
        this.configResources = configResources;
    }

    protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
        beanDefinitionReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
    }
}

但我无法让它发挥作用。有人可以帮帮我吗?

But I can't get it to work. Could someone help me?

推荐答案

尝试 ClassPathXmlApplicationContext

它是一个独立的XML应用程序上下文,从类路径获取上下文定义文件,将普通路径解释为包含包路径的类路径资源名称(例如mypackage / myresource.txt)。

It is a standalone XML application context, taking the context definition files from the class path, interpreting plain paths as class path resource names that include the package path (e.g. "mypackage/myresource.txt").

对于测试工具以及对JAR中嵌入的应用程序上下文非常有用

您可以这样做:

创建一个包含以下内容的Test类:

Create yourself a Test class with the following content in it:

package com.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
  public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new 
           ClassPathXmlApplicationContext("/com/test/appConfig.xml");
    Integer someIntBean = (Integer) context.getBean("testBean");
    System.out.println(someIntBean.intValue()); // prints 100, as you will see later
  }
}

现在创建名为appConfig.xml的bean应用程序配置文件:

Now create the beans application config file named appConfig.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
  <bean id="testBean" class="java.lang.Integer">
    <constructor-arg type="int" value="100" />
  </bean>
</beans>

您在名为 com.test ,彼此相邻。将类路径引用添加到您的spring jar中或将它们一起打包到您自己的单个jar文件中,如下所示:

You create these files in a package called com.test, next to each other. Add classpath references to your spring jars or pack them together into your own single jar file wich should look like this:

test.jar --- com
          |   |--- test
          |          |--- appConfig.xml
          |          |--- Test.class
          |
          |-- META-INF
          |        |--- MANIFEST.MF
          |
          |-- org
          |     |--- springframework 
          |               |--- ...
          |               |--- ...
          |-- ....

在你的清单中文件你将有这个(使用一个尾随空白行):

In your manifest file you will have this (use with a trailing blank line):

Main-Class: com.test.Test

就是这样。

当你跑步时文件(双击或 java -jar test.jar )你应该看到100在屏幕上打印。这是我从执行它得到的(注意我上面讨论的100 - 在最后一行):

When your run your file (double click or java -jar test.jar) you should see 100 printed on the screen. Here is what I get from executing it (notice the 100 I was talking about above - on the last row):

Feb 23, 2011 11:29:18 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ca2dce: 
display name [org.springframework.context.support.ClassPathXmlApplicationContext@ca2dce]; 
startup date [Wed Feb 23 23:29:18 PST 2011]; root of context hierarchy
Feb 23, 2011 11:29:18 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/test/appConfig.xml]
Feb 23, 2011 11:29:20 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@ca2dce]: 
org.springframework.beans.factory.support.DefaultListableBeanFactory@199f91c
Feb 23, 2011 11:29:20 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@199f91c: 
defining beans [testBean]; root of factory hierarchy
100

P.S。您不必将Spring jar内容包含在您自己的jar中。运行应用程序时,可以在类路径中使用它们。因为你提到了一个罐子,我把它们放在那里。基本上这就是你需要的:

P.S. You don't absolutely have to include the Spring jars content into your own jar. You can have them available on the classpath when you run your app. I placed them like that since you mentioned one jar. Basically this is what you need:

test.jar --- com
          |   |--- test
          |          |--- appConfig.xml
          |          |--- Test.class
          |
          |-- META-INF
                   |--- MANIFEST.MF

这篇关于jar文件中的Spring上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆