2

Spring Beanアノテーションをjarファイルとして使用してクラスを生成できますか?そのjarのクラスから別のSpringBeanを注入する方法は?可能な方法を提供できますか?私が以下のようになると、それは機能しませんか?

Jarファイル内

    package org.java.support;

    @Service("CommonService")
    public class CommonService {
    }

プロジェクト内

    package com.java.test.app;

    @Service(value = "OtherService")
    public class OtherService {
        @Resource(name = "CommonService")
        private CommonService service;
    }       

構成内

    <context:component-scan base-package="com.java.test.app, org.java.support">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>

次のエラーが発生します。

13:32:12,331 DEBUG [org.springframework.context.annotation.AnnotationConfigApplicationContext] Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [or
g.springframework.context.support.DefaultLifecycleProcessor@32dcb03b]
13:32:12,331 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] Returning cached instance of singleton bean 'lifecycleProcessor'
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'CommonService' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
    at org.ace.java.support.delete.Test.main(Test.java:18)
4

2 に答える 2

2

Eclipse から Jar を生成する場合は、エクスポート ダイアログで、[ディレクトリ エントリの追加] チェック ボックス (オプションの下) がオンになっていることを確認します。

于 2013-03-11T16:46:19.537 に答える
1

さてあなたはほとんどそこにいます:)

jarをSpringコンテキストに追加するには、includeフィルターを使用する必要があります。しかし、2つのコンポーネントのスキャンラインを使用することも機能するはずだと思います。

例:

<context:component-scan base-package="org.example">
    <context:include-filter type="regex" expression=".*Stub.*Repository"/>
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-classpath-scanningを参照してください

于 2012-09-20T12:00:32.373 に答える