使用Spring从XML属性文件加载和注入属性Bean

时间:2014-02-21 16:28:39

标签: java xml spring spring-bean

我正在尝试从XML属性文件加载SQL查询,并将其作为java.util.Property注入到我的DAO类中。 SQLs.xml 与应用程序上下文位于同一目录中(我使用Maven结构,所以它们都在src / main / resources下)。我的应用程序上下文文件如下所示:

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

SQLs.xml内容是:

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

DAO课程:

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");
  }
}

我在主应用程序中加载上下文文件:

ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

我可以毫无问题地加载EmployeeDAO并调用其getEmployee()方法。甚至datasource也可以毫无问题地提取所有必需的键/值。但queryProps始终为空。

1 个答案:

答案 0 :(得分:0)

似乎我将属性注入两个不同的实例。

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

现在queryProps Property包含来自SQLs.xml的预期内容。