1

次のコマンドを使用してコマンド プロンプトから実行されるスタンドアロン アプリケーション (Web ベースではない) を作成しています。

java -jar myapplication.jar 

Eclipse でアプリケーションを Maven プロジェクトとして開発したので、Eclipse はすべての依存ライブラリを取得します。メインクラスを右クリックして「Run As」>「Java Application」を選択すると、正常に動作します。

今私が抱えている問題は、アプリケーションを単一の jar ファイルとしてデプロイする必要があることです。これを行うために、Eclipse を使用して、アプリケーションを「Runnable Jar」として (つまり、「export コマンド」を介して) エクスポートしました。これにより、jar ファイルが生成されました。jarファイルを調べたところ、すべてのクラスと依存jarファイルがjarファイルに含まれています。Spring アプリケーション コンテキスト ファイルも、最上位フォルダーの jar ファイルにあります。jar ファイルの「内部」は次のようになります。

- com
    - myapp
      - service
         - MyAppService.class
      - dao
         - MyAppDataDao.class   
      - MyMainClass.class


- META-INF
- org
- application-context-components.xml
- log4j.properties
- [several jar files for spring, log4j, velocity etc)

次のコマンドを使用してjarファイルを実行しようとしましたが、このエラーが発生しました:

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.myapp.MyMainClass] is defined: expected single bean but found 0:
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:257)
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1012)
        at com.myapp.MyMainClass.init(MyMainClass.java:44)
        at com.myapp.MyMainClass.main(MyMainClass.java:65)
        ... 5 more

ファイル com.myapp.MyMainClass は正しい名前の jar ファイルにあります。パッケージ内のクラスは自動配線されています。注釈の何かを見逃したか、アプリケーション コンテキスト ファイルの何かを見逃したに違いないと思います。クラスの構造と使用される注釈を以下に示します。

MyMainClass

@Component
public class MyMainClass{

    @Autowired
    MyAppService myAppService;

    public static void main(String args[]){
        try{
            context = new     ClassPathXmlApplicationContext(properties.get("app.context"));

 MyMainClass mymainClass = context.getBean(MyMainClass.class);
 mymainClass.myAppService.getData()....
 ....
            }catch(Exception e){
                throw new CWAException(fname + ":" + e);
            }
        }
    }

app.context プロパティは、アプリケーション コンテキスト ファイルの名前を返します。

MyAppService

@Service
public class MyAppService{

    @Autowired
    MyAppDataDao myAppDataDao;

    ---
    ---
    ---
}

MyAppDataDao

@Repository("myAppDataDao;")
public class MyAppDataDao {

    getData(){
    }

    ---
    ---
    ---
}

アプリケーションコンテキストファイルは次のようになります

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Auto scan the components -->
    <context:component-scan base-package="com.myapp" />

    <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean" 
          p:resourceLoaderPath="file://C:\template" 
          p:preferFileSystemAccess="true"/>  

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
        <property name="url"><value>jdbc:oracle:thin:@x.x.x.x:x</value></property>
        <property name="username"><value>xxx</value></property>
        <property name="password"><value>xxx</value></property>
    </bean>              
</beans>

エラーを見ると、オートワイヤリングが機能していないと思いますが、構成のどこで間違ったのかわかりません。アプリケーションはパッケージ化されたjarファイルにあり、使用してファイルをロードしているため、ファイルClassPathXmlApplicationContextが見つかるはずです。また、Eclipseで動作する理由もわかりませんが、エクスポートされた後では動作しません。

何か案は?

4

1 に答える 1

1

少なくとも Spring3.0 のクラス PathMatchingResourcePatternResolver は、jar 内の autowiring アノテーションでマークされたクラスを検索しません。

jar が存在するディレクトリをクラスパスに追加します。

クラスを検出し、Bean としてロードします。

于 2013-06-27T09:02:24.460 に答える