2

Spring 構成を介して配線しようとしている Bean が機能しないという問題が発生していますか? テスト ケース内のチェック中に Bean アクセサーが null として表示されます (ただし、ログで、セッターが実際に有効な値に設定していることを確認します。実際に助けを借りることができます...

コードは次のとおりです。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/test-context.xml")
public abstract class HistoryCompress extends TestBase {

    private CompressionArchive compressArchive;
    public void setCompressionArchive(CompressionArchive value)
    {
        this.logInfo("setting compression archive: %s, name: %s",value,value.getcompressionFormat());
        this.compressArchive=value;
    }

    protected CompressionArchive getCompressionArchive()
    {
        return this.compressArchive;
    }

    @Test
    public void getArchiveTypeName()
    {
        logInfo("test");
        this.assertNotNull(this.getCompressionArchive(),"compression archive is null");     
        this.assertNotNull(this.getCompressionArchive().getcompressionFormat(), "format name is null");
        logInfo("format: %s",this.getCompressionArchive().getcompressionFormat());
    }

}

public class HistoryZipCompress extends HistoryCompress 
{
    public HistoryZipCompress()
    {
        this.logInfo("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
        this.logInfo("-=-=-=--=- C'Tor HistoryZipCompress");
        this.logInfo("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
    }

}

public interface CompressionArchive {
    public String getcompressionFormat();
    public Map<String,String> getArchiveHashes(InputStream stream,String password) throws Exception;
}
public class CompressionArchiveZip extends IntegrationBase implements CompressionArchive
{
   /* implementation methods*/
}


そして、次の構成でこれを接続しようとしています

<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:jaxws="http://cxf.apache.org/jaxws"
    xmlns:util="http://www.springframework.org/schema/util" 
    default-autowire="byName"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <context:annotation-config />       
    <bean class="gdg.scc.integration.testcases.file.history.HistoryZipCompress">
        <property name="compressionArchive"  ref="zipArchive" />
    </bean>     
    <bean id="zipArchive" class="gdg.scc.integration.global.helpers.compression.impl.CompressionArchiveZip" />
</beans>

これを実行すると、HistoryZipCompress が 3 回インスタンス化されることがわかります。HistoryZipCompress で getArchiveTypeName を実行した結果の末尾を次に示します。

16-Jan 13:21:09 - INFO - メイン - org.springframework.context.support.GenericApplicationContext - org.springframework.context.support.GenericApplicationContext@2bfe605c の更新: 起動日 [Wed Jan 16 13:21:09 MST 2013] ; コンテキスト階層のルート
16-Jan 13:21:09 - INFO - メイン - org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' アノテーションが検出され、オートワイヤリングがサポートされました
zipArchive,extensionToMimeType]; ファクトリ階層のルート
16-Jan 13:21:09 - INFO - メイン - integration.testcases.file.history.HistoryZipCompress - -=-=-=-=-=-=-=-=-=-=-=-=-=- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
16-Jan 13:21:09 - 情報 - メイン - integration.testcases.file.history.HistoryZipCompress - -=-=-=--=- C'Tor HistoryZipCompress
16-Jan 13:21:09 - INFO - メイン - integration.testcases.file.history.HistoryZipCompress - -=-=-=-=-=-=-=-=-=-=-=-=-=- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
16-Jan 13:21:09 - INFO - メイン - integration.testcases.file.history.HistoryZipCompress - 圧縮アーカイブの設定: integration.global.helpers.compression.impl.CompressionArchiveZip@6a367507、名前: zip
16-1 月 13:21:09 - 情報 - メイン - integration.testcases.file.history.HistoryZipCompress - テスト
16-1 月 13:21:09 - 情報 - スレッド 0 - org.springframework.context.support.GenericApplicationContext - org.springframework.context.support.GenericApplicationContext@2bfe605c を閉じる: 開始日 [1 月 16 日水曜日 13:21:09 MST 2013]; コンテキスト階層のルート
16-Jan 13:21:09 - INFO - Thread-0 - org.springframework.beans.factory.support.DefaultListableBeanFactory - org.springframework.beans.factory.support.DefaultListableBeanFactory@578bb31a でシングルトンを破棄: Bean の定義 [fileSystemAPIUrl,fileTypeGenerator ,regexToExtensionMap,activeMQMessageProperties,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,integration.testcases.file .history.HistoryZipCompress#0,zipArchive,extensionToMimeType]; ファクトリ階層のルート
4

1 に答える 1

1

@Autowired でフィールドに注釈を付けます。

@Autowired
private CompressionArchive compressArchive;

xml 構成で HistoryZipCompress を破棄します - そこで何をしていますか? テスト クラスの別のインスタンスを作成する必要はありません。ランナーが作成します。

CompressionArchive インスタンスを挿入するには、テスト オブジェクトが必要です。Spring ランナーはテスト オブジェクト (HistoryCompress) を管理しますが、CompressionArchive インスタンスを注入する必要があることを知りません。

于 2013-01-16T21:10:58.963 に答える