1

私は ThreadPoolExecutor ユーティリティを使用しており、呼び出されたクラスのコンストラクターを介して値を渡しています。コンストラクターは、2 つの引数 (1) マップ (2) 文字列を取ります。

2 つの引数 (マップと文字列) を取る呼び出されたクラスの Bean を宣言する方法について混乱しています。私のコードは以下の通りです。

***Calling Class***

    public class Starter {

        ProcessScheduler deleteBatch;
        public ProcessScheduler getDeleteBatch() {
            return deleteBatch;
            }

        public void setDeleteBatch(ProcessScheduler deleteBatch) {
            this.deleteBatch = deleteBatch;
            }


        public void start() {

        ThreadPoolExecutor executor = testThreadPoolExecutorService.createNewThreadPool();


            for (int i=0;i<=5;i++)
            {
            Map m4 = arrayRecords.get(i);
            executor.execute(new ProcessScheduler("Thread #"+i,m4));                     // Comment - started 
The above line executes fine but it gives null pointer error if I will call any other method from the run() inside  called class(ProcessScheduler). So I have use a Bean such as executor.execute(getDeleteBatch("Thread #"+i,m4)) to get the instance of the bean. But I dont know how to do this in this case?

// Comment - ended


            }

***Called Class***



public class ProcessScheduler implements Runnable {

         public ProcessScheduler(String taskName, Map m) {
            this.taskName = taskName;
            this.deleteRecordsMap = (HashMap) m;
            }
        Processor processor;
        public Processor getProcessor() 
        {
            return processor;
        }


        public void setProcessor(Processor mappProcessor) {
            this.mappProcessor = mappProcessor;
        }


        public void run() 
        {
        // This returns null 
        processor.getNumbers();
        }

        }



I have some confusions as below.
(1) How to declare a bean for ProcessScheduler in this case. 
(2) Is the declaration of getDeleteBatch is correct in this case like below?

public ProcessScheduler getDeleteBatch() {
            return deleteBatch;
            }

ありがとうゲンダフル

4

3 に答える 3

2

これは本当に良い考えだと思いますか?

public ProcessScheduler(String taskName, Map m) {
    this.taskName = taskName;
    this.RecordsMap = (HashMap) m;
}

私はそれがもっとこのようであるべきだと思います:

public ProcessScheduler(String taskName, Map m) {
    this.taskName = taskName;
    this.recordsMap = new HashMap(m);  // You don't want changes to the Map passed in to show up in your private data member.
}

Spring から ProcessScheduler インスタンスを Bean として注入できるかどうかはわかりません。これは、エグゼキュータ サービスごとに新しいものを本当に作成したい場合かもしれません。

Spring は、アプリ内のすべての Bean を制御する必要はありません。

于 2012-11-06T21:00:06.073 に答える
0

私は以下のようにこの問題を解決しました、

呼び出し元のクラスから、execute メソッドで Processor クラスのインスタンスを渡しています。

executor.execute
(newProcessScheduler("Thread#"+Thread.currentThread().getName(),hm,processor))

つまり、execute() でプロセッサを引数として渡しているので、ProcessScheduler クラスの run() 内でプロセッサのメソッドを呼び出しても、nullpointerexception はスローされません。

于 2012-11-15T19:41:52.863 に答える
0

http://jonathanhui.com/spring-framework-xml-configurationには、Spring Bean を構成するさまざまな方法と、コレクションのインスタンス化と埋め込みのためにサポートされているタグがまとめられています。

以下のようなものがうまくいくはずです。

<?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-3.0.xsd">

  <bean id="processscheduler"
        class="ProcessScheduler">
     <constructor-arg  index="0" value="task1"/>
     <constructor-arg  index="1">
         <map>
              <entry key="key1" value="v1"/>
              <entry key ="key2" value-ref="someBean"/>
          </map>
     </constructor-arg>
  </bean>
</beans>
于 2012-11-06T21:03:32.303 に答える