从数据库中为动态列表注入Spring依赖项

时间:2013-09-30 10:35:21

标签: java spring

我是Spring的新手。我的问题是如何将从数据库加载的值(来自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 + "]";
    }



}

/ *某些服务,它调用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);
    }

}

Spring 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是一个实例变量而不是方法局部变量。
  3. 此致 Raghu


    我会采取上述答案。

    正如我之前所说,我对春天来说很新鲜。当我向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(&#34; bprocessor&#34;,hservice.show());在这里我从hservice.show发送动态列表,它连接到数据库并获取业务单位列表。

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

    这是一种很好的做事方式吗?

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

    My Spring 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>
    

    我是对的吗?

1 个答案:

答案 0 :(得分: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
    }
}