使用postProcessBeanFactory动态加载JDBC属性

时间:2014-01-05 05:32:32

标签: spring-jdbc

我正在使用jdbc进行动态postProcessBeanFactory属性加载。

但我得到一个例外:

Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 11 in XML document from class path resource [BeansJdbc.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 8; cvc-complex-type.2.3: Element 'bean' must have no character [children], because the types content type is element-only.

这是我的OurLogic.java

package com.madhubanti.spring.jdbc;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

/**
 * @author Madhubanti
 *
 */
public class OurLogic {

    public static void main(String[]args){
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("BeansJdbc.xml");
        PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        ppc.setLocation(new ClassPathResource("/jdbcBund.properties"));
        ppc.postProcessBeanFactory(context.getBeanFactory());

        SpringJdbcCreateTable obj = (SpringJdbcCreateTable)context.getBean("id3");
        obj.createTable();
    }

}

BeansJdbc.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="id1"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
               
        <property name="driverClassName" value="${jdbc.className}" />
               
        <property name="url" value="${jdbc.url}" />
               
        <property name="username" value="${jdbc.user}" />
               
        <property name="password" value="${jdbc.pass}" />
    </bean>

    <bean id="id2" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg>
            <ref bean="id1" />
        </constructor-arg>
    </bean>

    <bean id="id3" class="com.madhubanti.spring.jdbc.SpringJdbcCreateTable">
        <property name="jt">
            <ref bean="id2" />
        </property>
    </bean>

</beans>

jdbcbund.properties:
jdbc.className = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:@localhost:1521:XE
jdbc.user = system
jdbc.pass = password

SpringJdbcCreateTable.java:

/**
 * 
 */
package com.madhubanti.spring.jdbc;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author Madhubanti
 *
 */
public class SpringJdbcCreateTable {

    JdbcTemplate jt ;

    public void setJt(JdbcTemplate jt){
        this.jt = jt;
    }

    public void createTable(){
        jt.execute("create table person (name varchar2(100) primary key, height number (3,2))");
        // execute returns void
        System.out.println("The table person has got created");
    }


}

请你说出问题在哪里?

0 个答案:

没有答案