3

我是春天的新手。我的问题是如何将从数据库加载的值(来自 db 的业务单位的动态列表)注入另一个 bean 以进行某些处理。

我正在从我的代码中执行以下操作。

                               /*** Sample code Starts here ****/

/* 使用加载方法从数据库中加载业务单元 */

public class BusinessUnitDaoImpl implements BusinessUnitDao {

    private JdbcTemplate jdbctemplate;

    public BusinessUnitDaoImpl() {
        super();
    }

    public BusinessUnitDaoImpl(DataSource ds) {
        this.jdbctemplate=new JdbcTemplate(ds);
    }

    @Override
    public List<BusinessUnit> load() {
        String SQL = "select * from business_unit";
        List<BusinessUnit> businessunits = jdbctemplate.query(SQL,
                new BusinessUnitRowMapper());
        return businessunits;
    }
}

/* 业务单元行映射器 */

public class BusinessUnitRowMapper implements RowMapper<BusinessUnit> {

    public BusinessUnitRowMapper() {
        // TODO Auto-generated constructor stub
    }

    public BusinessUnit mapRow(ResultSet rs, int rowNum) throws SQLException {
          BusinessUnit bunit = new BusinessUnit();
          bunit.setBusinessUnitId(rs.getInt("business_unit_id"));
          bunit.setBusinessUnitDesc(rs.getString("business_unit_desc"));
          bunit.setCurrencyCode(rs.getString("currency_code"));
          return bunit;
       }


}

/* 业务单元 VO */

public class BusinessUnit {

    private int businessUnitId;
    private String businessUnitDesc;
    private String currencyCode;


    public BusinessUnit() {
        super();
    }


    public int getBusinessUnitId() {
        return businessUnitId;
    }


    public void setBusinessUnitId(int businessUnitId) {
        this.businessUnitId = businessUnitId;
    }


    public String getBusinessUnitDesc() {
        return businessUnitDesc;
    }


    public void setBusinessUnitDesc(String businessUnitDesc) {
        this.businessUnitDesc = businessUnitDesc;
    }


    public String getCurrencyCode() {
        return currencyCode;
    }


    public void setCurrencyCode(String currencyCode) {
        this.currencyCode = currencyCode;
    }


    @Override
    public String toString() {
        return "BusinessUnit [businessUnitId=" + businessUnitId
                + ", businessUnitDesc=" + businessUnitDesc + ", currencyCode="
                + currencyCode + "]";
    }



}

/* 一些Service,调用dao加载业务单元*/

public class HarmonyService {

    private BusinessUnitDao budao;
    private RequestDetails requestDetails;

    public HarmonyService(BusinessUnitDao budao,RequestDetails requestDetails) {
        this.budao=budao;
        this.requestDetails=requestDetails;
    }

    public List<BusinessUnit> show() {
        return budao.load();
    }

    public WFRequest getDetail(long requestId) {
        return requestDetails.load(requestId);
    }

}

春天的xml:

<?xml version="1.0" encoding="UTF-8"?>

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:property-placeholder location="file:${databaseConfiguration}"/>

    <bean id="AmericasDataSource" class="dell.harmony.data.HarmonyBasicDataSource" destroy-method="close" >
       <property name="url"><value>${HarmonyAmericasDb.url}</value></property>
       <property name="driverClassName"><value>${HarmonyAmericasDb.driverClassName}</value></property>
       <property name="username"><value>${HarmonyAmericasDb.username}</value></property>
       <property name="password"><value>${HarmonyAmericasDb.password}</value></property>
       <property name="removeAbandoned"><value>${HarmonyAmericasDb.removeAbandoned}</value></property>
       <property name="initialSize"><value>${HarmonyAmericasDb.initialSize}</value></property>
       <property name="maxActive"><value>${HarmonyAmericasDb.maxActive}</value></property>
    </bean>

    <bean id="EMEADataSource" class="dell.harmony.data.HarmonyBasicDataSource" destroy-method="close" >
       <property name="url"><value>${HarmonyEMEADb.url}</value></property>
       <property name="driverClassName"><value>${HarmonyEMEADb.driverClassName}</value></property>
       <property name="username"><value>${HarmonyEMEADb.username}</value></property>
       <property name="password"><value>${HarmonyEMEADb.password}</value></property>
       <property name="removeAbandoned"><value>${HarmonyEMEADb.removeAbandoned}</value></property>
       <property name="initialSize"><value>${HarmonyEMEADb.initialSize}</value></property>
       <property name="maxActive"><value>${HarmonyEMEADb.maxActive}</value></property>
    </bean>

    <bean id="budao" class="test.dao.BusinessUnitDaoImpl">
        <constructor-arg index="0"><ref bean="AmericasDataSource"/></constructor-arg>
     </bean>

     <bean id="requestdao" class="test.dao.RequestDetailImpl">
        <constructor-arg index="0"><ref bean="AmericasDataSource"/></constructor-arg>
     </bean>

    <bean id="service" class="test.service.HarmonyService">
        <constructor-arg index="0"><ref bean="budao"/></constructor-arg>
        <constructor-arg index="1"><ref bean="requestdao"/></constructor-arg>
     </bean>

</beans>

/* 用于测试弹簧的测试应用程序 */

public class MyApp {

    public static void main(String args[]) {
        ApplicationContext context = 
            new ClassPathXmlApplicationContext("Spring-All-Module.xml");
        HarmonyService hservice = (HarmonyService) context.getBean("service");
        System.out.println(hservice.show());
        System.out.println(hservice.getDetail(13090000000001L));
    }

}

                /*** Sample code ends here ****/

问题:

如果假设,我想从服务 HarmonyService 的加载方法中注入业务单位列表,该怎么做?

假设我有一个处理器类,比如下面的 BatchProcessor。

public class BatchProcessor {


          public List<BusinessUnit> proces(List<BusinessUnit> businessUnitList ) {
                 //do some processing here.

           }

}

如何使用spring xml将动态创建的businessUnit注入到本地方法变量businessUnitList中(请用java代码和spring xml更改说明)。

  1. 是否可以将动态列表注入到本地方法变量中。
  2. 举例说明如何将值注入到实例变量中,假设上面的 businessUnitList 是实例变量而不是方法局部变量。

问候, 拉古


我会接受上面的答案。

正如我之前所说,我对春天很陌生。当我探索 Stackoverflow 另一个问题时,我有了一个想法。

让我们假设 businessUnitList 作为一个实例变量。

public class BatchProcessor {

    public BatchProcessor(List<BusinessUnit> businessUnitList) {
        this.businessUnitList=businessUnitList;
    }

    private List<BusinessUnit> businessUnitList;

    public List<BusinessUnit> getBusinessUnitList() {
        return businessUnitList;
    }

    public void setBusinessUnitList(List<BusinessUnit> businessUnitList) {
        this.businessUnitList = businessUnitList;
    }

    public List<BusinessUnit> process() {
        System.out.println("Started processing the business Units" + businessUnitList);
        //do some processing
        return this.businessUnitList;
    }

在 MyApp 主程序中,我可以做到这一点。

BatchProcessor bprocess = (BatchProcessor) context.getBean("bprocessor", hservice.show());
System.out.println(bprocess.process());

//context.getBean("bprocessor",hservice.show()); 这里我从 hservice.show 发送动态列表,它连接到数据库并获取业务单位列表。

现在,我将再次调用 BatchProcessor 的处理方法。

这是做事的好方法吗?

基本上,当我们想要动态传递一个值时,我们需要使用参数调用 contextbean。

我的春天 xml:

<!-- passing a dummy list to the constructor -->

 <bean id="bprocessor" class="test.rules.BatchProcessor" scope="prototype">
  <constructor-arg type="java.util.List">
        <list>
            <ref bean="bunit"/>
        </list> 
  </constructor-arg>

我对吗?

4

1 回答 1

1

您可以BatchProcessor在类似的线路上装箱HarmonyService

<bean id="service" class="test.service.BatchProcessor">
    <constructor-arg index="0"><ref bean="budao"/></constructor-arg>
</bean>

然后在里面BatchProcessor

public class BatchProcessor {
    private BusinessUnitDao budao;

    public BatchProcessor(BusinessUnitDao dao) {
        this.budao = dao;
    }

    public List<BusinessUnit> process() {
       // process budao.load() list here
    }
}
于 2013-09-30T10:59:17.770 回答