Win a copy of Head First Android this week in the Android forum!
  • 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 ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Spring Dependency injection for a dyanmically created list from database

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

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

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

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>


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

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.

Regards,
Raghu



 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic