gpt4 book ai didi

java - 来自数据库的动态列表的 Spring 依赖注入(inject)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:43 24 4
gpt4 key购买 nike

我是 Spring 的新手。我的问题是如何将从数据库加载的值(来自 db 的动态业务单元列表)注入(inject)另一个 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);
}

}

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>

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

问题:

假设,我想从服务 HarmonyService 的加载方法中注入(inject)业务单元列表,该怎么做?

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

public class BatchProcessor {


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

}

}

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

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

问候,拉古


我会接受上面的答案。

正如我之前所说,我对 Spring 还很陌生。当我探索 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 的 process 方法。

这是做事的好方法吗?

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

我的 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>

我说的对吗?

最佳答案

您可以在与 HarmonyService 类似的行上创建 BatchProcessor

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

关于java - 来自数据库的动态列表的 Spring 依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19092037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com