私はJavaとSpringに不慣れで、C#と.NETの世界から来ているので、我慢してください-私がやろうとしていることは、的外れかもしれません...
XML構成ではなく、Java構成とアノテーションを使用してSpring DIを構成しようとしていますが、いくつかの問題があります。これはスタンドアロンアプリケーション用であり、Webアプリ用ではありません。私はspringsourceのドキュメントに目を通しましたが、私の非常に基本的な構成が正しいはずだと言える限り、そうではありません。以下のコードをご覧ください。
Java構成注釈付きクラス:
package birdalerter.common;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import birdalerter.process.ISightingsProcessor;
import birdalerter.process.SightingsProcessor;
@Configuration
@ComponentScan({"birdalerter.process", "birdalerter.common"})
public class AppConfig {
@Bean
@Scope("prototype")
public ISightingsProcessor sightingsProcessor(){
return new SightingsProcessor();
}
}
ISightingsProcessorインターフェースを実装するコンポーネントを構成します。
package birdalerter.process;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import org.springframework.stereotype.Component;
import birdalerter.domainobjects.IBirdSighting;
@Component
public class SightingsProcessor implements ISightingsProcessor{
private LinkedBlockingQueue<IBirdSighting> queue;
private List<ISightingVisitor> sightingVisitors = new ArrayList<ISightingVisitor>();
public SightingsProcessor(){
}
...
}
ファクトリコンポーネントの構成:
package birdalerter.process;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
public class ProcessorFactory {
private ISightingsProcessor sightingsProcessor;
@Autowired
@Required
private void setSightingsProcessor(ISightingsProcessor sightingsProcessor){
this.sightingsProcessor = sightingsProcessor;
}
public ISightingsProcessor getSightingsProcessor(){
return this.sightingsProcessor;
}
}
AnnotationConfigApplicationContextを接続し、テストします。
@Test
public void testProcessingDI(){
@SuppressWarnings("resource")
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(AppConfig.class);
context.refresh();
ISightingsProcessor processor = new ProcessorFactory().getSightingsProcessor();
System.out.println(processor);
Assert.assertTrue(processor != null);
}
SightingsProcessorはセッター注入されておらず、返されたオブジェクトがnullであるため、アサートは失敗しています。うまくいけば、私は非常に明白な何かを逃しました。
前もって感謝します。
メリトンに応えて編集:
メリトンの答えをありがとう。
Springが新しく作成されたオブジェクトを知らないのはなぜですか?Springは、アプリケーションのライフサイクル全体を通じて依存関係を維持せず、Beanとして構成された新しいオブジェクトが作成されたときに適切に注入しませんか?
正直に言うと、直接使用したくありませんcontext.getBean(ISightingsProcessor.class)
。手動で介入することなく、setterメソッドに依存性を注入したいと思います。
私はインターフェースが拡張するときにを使用しProcessorFactory
ています-実装オブジェクトはスレッドとして開始されます。アプリケーションは、n *個のスレッドを持つように構成可能であり、各スレッドはループ反復内で開始されます。メソッド宣言内にアノテーションを含めることは不可能だと思います(間違っている可能性がありますので、教えてください) 。そのため、ファクトリを使用して、注入された具象クラスの新しいインスタンスを提供します。ISightingsProcessor
Runnable
@Autowired
ISightingsProcessor
はい、@Scope
注釈について調べたところです。その通りです。宣言に移動する必要がありますAppConfig
@Bean
(この編集で行いました)。ありがとうございます。