簡體   English   中英

使用Spring從XML屬性文件加載並注入屬性Bean

[英]Load and Inject to Property Bean from XML Property File with Spring

我試圖從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");
  }
}

我正在使用以下方法在Main應用程序中加載上下文文件:

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

我可以毫無問題地加載EmployeeDAO並調用其getEmployee()方法。 即使是datasource可以毫無問題地提取所有需要的鍵/值。 但是queryProps始終為null。

看來我正在將屬性注入兩個不同的實例中。

    <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中的預期內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM