Java 数据库中动态列表的Spring依赖项注入
我是春天的新手。我的问题是如何将从数据库加载的值(来自db的业务单元动态列表)注入到另一个bean中进行一些处理 我正在从我的代码中执行以下操作Java 数据库中动态列表的Spring依赖项注入,java,spring,Java,Spring,我是春天的新手。我的问题是如何将从数据库加载的值(来自db的业务单元动态列表)注入到另一个bean中进行一些处理 我正在从我的代码中执行以下操作 /*** Sample code Starts here ****/ /*使用Load方法从数据库加载业务单元*/ public class BusinessUnitDaoImpl implements BusinessUnitDao { private JdbcTemplate
/*** Sample code Starts here ****/
/*使用Load方法从数据库加载业务单元*/
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;
}
}
/*一些服务,它调用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);
}
}
问题:
如果假设,我想从服务HarmonyService的load方法中注入业务单元列表,如何做到这一点
假设我有一个processorclass,类似下面的BatchProcessor
public class BatchProcessor {
public List<BusinessUnit> proces(List<BusinessUnit> businessUnitList ) {
//do some processing here.
}
}
//getBean(“bprocessor”,hsservice.show());这里我从hservice.show发送动态列表,它连接到数据库并获取业务单位列表
现在,我再次调用BatchProcessor的process方法
这是一种好的做事方式吗
基本上,当我们想要动态地传递一个值时,我们需要使用参数调用contextbean
我的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>
<!-- 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>
我说得对吗 您可以将
BatchProcessor
包装在与HarmonyService
<bean id="service" class="test.service.BatchProcessor">
<constructor-arg index="0"><ref bean="budao"/></constructor-arg>
</bean>
我建议改为使用Spring很棒的AOP特性在非容器管理的类上获得DI。这真的很简单,而且非常通用。@Jasperbluse:根据OP,他是Spring和DI的新手,所以我想他在学习基础知识之前不应该深入AOP。你不需要知道AOP就可以使用此功能-Spring会为你解决这个问题。最终结果是对非容器管理的类进行DI。这与使用“@Transactional”注释不需要了解AOP是同一回事,它也是基于AOP构建的。@Jasperbluse:如果基本知识不清楚,我不建议有人使用高级功能。但是我将把这个留给OP来适应AOP来做这个简单的事情。事实证明我看错了这个问题。我认为OP需要对从数据库发出的对象使用DI。所以你是100%对的,而我的建议是100%错的:)哦,对不起!:)
public class BatchProcessor {
public List<BusinessUnit> proces(List<BusinessUnit> businessUnitList ) {
//do some processing here.
}
}
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;
}
BatchProcessor bprocess = (BatchProcessor) context.getBean("bprocessor", hservice.show());
System.out.println(bprocess.process());
<!-- 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>
<bean id="service" class="test.service.BatchProcessor">
<constructor-arg index="0"><ref bean="budao"/></constructor-arg>
</bean>
public class BatchProcessor {
private BusinessUnitDao budao;
public BatchProcessor(BusinessUnitDao dao) {
this.budao = dao;
}
public List<BusinessUnit> process() {
// process budao.load() list here
}
}