Issue
I am trying to load SQL queries from an XML property file and inject it as a java.util.Property
into my DAO class. The SQLs.xml resides in the same directory as the application context (I'm using Maven structure, so they all under src/main/resources). My application context file looks like following:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean id="EmployeeDAO" class="pkg.dao.EmployeeDAOImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="oracle jdbc url" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<util:properties id="queryProps" location="classpath:SQLs.xml"/>
<bean id="employeeDAOProp" class="pkg.dao.EmployeeDAOImpl">
<property name="queryProps" ref="queryProps" />
</bean>
And the SQLs.xml
content is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="getEmployee">
select * from employee;
</entry>
</properties>
The DAO class:
public class EmployeeDAO {
private DataSource dataSource;
private Properties queryProps;
public Properties getQueryProps() {
return queryProps;
}
public void setQueryProps(Properties queryProps) {
this.queryProps = queryProps;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public Employee getEmployee(int id) {
String sql = queryProps.getProperty("getEmployee");
}
}
I am loading the context file in the Main application with:
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
I can load the EmployeeDAO
without an issue and call on its getEmployee()
method. Even the datasource
pulls all the needed key/values without an issue. But the queryProps
is always null.
Solution
It appears that I was injecting the properties into two different instances.
<util:properties id="queryProps" location="classpath:SQLs.xml"/>
<bean id="EmployeeDAO" class="pkg.dao.EmployeeDAOImpl">
<property name="dataSource" ref="dataSource" />
<property name="queryProps" ref="queryProps" />
</bean>
Now the queryProps Property
contains the expected content from the SQLs.xml.
Answered By - Malvon
Answer Checked By - Pedro (JavaFixing Volunteer)