• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Spring Dependency injection for a dyanmically created list from database

Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

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() {

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

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();
return bunit;


/* Business Unit VO */

public class BusinessUnit {

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

public BusinessUnit() {

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;

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


/* Some Service , which calls the dao to load the business unit*/

public class HarmonyService {

private BusinessUnitDao budao;
private RequestDetails requestDetails;

public HarmonyService(BusinessUnitDao budao,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"

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

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

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


/* 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");


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


If suppose, i want to inject the list of business units from the service HarmonyService's load method, how to do that?

Assume i've a processorclass, something like BatchProcessor the below one.

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).

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.


I'm sure glad that he's gone. Now I can read this tiny ad in peace!
a bit of art, as a gift, the permaculture playing cards
    Bookmark Topic Watch Topic
  • New Topic