如何声明具有将字符串和映射为参数的构造函数的bean



我正在使用threadpoolexecutor实用程序,并通过称为类中的构造函数传递值。构造函数采用两个参数(1)地图(2)字符串。

我对如何声明bean的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;
            }

谢谢gendaful

您真的认为这是个好主意吗?

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.
}

我不确定您是否可以从弹簧中注入Processscheduler实例;在这种情况下,您确实想为每个执行人服务创建一个新的。

弹簧不必控制您应用中的每个豆子。

http://jonathanhui.com/spring-framework-xml-configuration总结了配置弹簧豆和支持标签的各种方法,以实例化和嵌入集合。

下面类似的东西应该对您有用。

<?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>

我已经解决了以下问题,

从我的呼叫类中,我在执行方法中传递了处理器类的实例。

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

因此,它的作用是,因为我将处理器作为execute()中的参数传递给处理器,因此如果我在processscheduler类的运行()中调用run()中的处理器方法。

<</p> <</p>

最新更新