简体   繁体   English

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

[英]Spring dependency injection for a dynamic list from database

I'm new to Spring. 我是春天的新手。 My question is how to inject a value loaded from the database(dyanmic list of business units from db) into another bean for some processing. 我的问题是如何将从数据库加载的值(来自db的业务单位的动态列表)注入另一个bean进行某些处理。

I'm doing the following from my code. 我正在从我的代码中执行以下操作。

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

/* Load Business Units from Database using the load method */ / *使用加载方法从数据库加载业务单位* /

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

/* Business Unit Row Mapper */ / *业务单位行映射器* /

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


}

/* Business Unit VO */ / *业务单位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 + "]";
    }



}

/* Some Service , which calls the dao to load the business unit*/ / *一些服务,它调用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: 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>

/* Test application for testing the spring */ / *测试弹簧的测试应用* /

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 ****/

Question: 题:

If suppose, i want to inject the list of business units from the service HarmonyService's load method, how to do that? 如果假设,我想从服务HarmonyService的加载方法中注入业务单位列表,该怎么做?

Assume i've a processorclass, something like BatchProcessor the below one. 假设我有一个处理器类,类似下面的BatchProcessor。

public class BatchProcessor {


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

           }

}

how to inject the dynamically created businessUnit into the local method variable businessUnitList using spring xml(please explain with java code and spring xml changes). 如何使用spring xml将动态创建的businessUnit注入到本地方法变量businessUnitList中(请用java代码和spring xml更改解释)。

  1. Is it possible to inject into the dynamic list into a local method variable. 是否可以将动态列表注入本地方法变量。
  2. Give an example on how to inject the value into the instance variable, assuming the above businessUnitList is an instance varaible instead of method local variable. 举一个如何将值注入实例变量的示例,假设上面的businessUnitList是一个实例变量而不是方法局部变量。

Regards, Raghu 此致,Raghu


I'll take the above answer. 我会采取上述答案。

As i said earlier , i'm very new to spring. 正如我之前所说,我对春天很新。 I got an idea when i explored Stackoverflow another question. 我有一个想法,当我探索Stackoverflow另一个问题。

Lets assume the businessUnitList as an instance variable. 让我们假设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;
    }

From the MyApp Main program, i can do this. 从MyApp主程序,我可以做到这一点。

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

//context.getBean("bprocessor",hservice.show()); //context.getBean( “bprocessor”,hservice.show()); here i'm sending the dynamic list from hservice.show, which connects to database and get the list of business Units. 在这里,我从hservice.show发送动态列表,它连接到数据库并获取业务单位列表。

now again, i'lll call the process method of BatchProcessor. 现在再次,我将调用BatchProcessor的进程方法。

Is this a good way of doing things? 这是一种很好的做事方式吗?

Basically when we want to dyanmically pass a value, we need to call, contextbean, with the argument. 基本上当我们想要动态传递一个值时,我们需要使用参数调用contextbean。

My Spring xml: 我的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>

Am i right? 我对吗?

You can crate BatchProcessor on similar lines as HarmonyService 您可以在与HarmonyService类似的行上HarmonyService BatchProcessor

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

then inside BatchProcessor : 然后在BatchProcessor

public class BatchProcessor {
    private BusinessUnitDao budao;

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

    public List<BusinessUnit> process() {
       // process budao.load() list here
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM