Spring将xml值注入properties-config.xml,然后注入bean

时间:2015-04-29 09:20:20

标签: sql xml spring dependency-injection

我是春天的菜鸟,但没有根据这种注射方式找到任何材料。

拥有 Queries.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="sql.accountdao.select">
    </entry>

    <entry key="sql.accountdao.insert"> 
    </entry>
</properties>

还有 properties-config.xml ,它包含queries.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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">


    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
        <property name="locations">
            <array>
                <value>classpath:sql/Queries.xml</value>
            </array>
        </property>
    </bean>

</beans>

自然有一个dao-config.xml,我想引用Queries.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="namedParamTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
        <constructor-arg ref="dataSource"/>
    </bean>

    <!-- Either annotations or xml will be correct -->
    <bean id="accountDao" class="dao.impl.AccountDAOImpl">
    <property name="namedParameterJdbcTemplate" ref="namedParamTemplate" />
        <property name="insertSQL" ????? />
        <property name="selectSQL" ???? />
    </bean>
</beans>

也在 AccountDAOImpl .java中尝试过这种方式:

public class AccountDAOImpl implements AccountDAO {

    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Value("${sql.accountdao.select}")
    private String selectSql;

    @Value("${sql.accountdao.insert}")
    private String insertSql;

    @Required
    public void setSelectSql(String selectSql) {
        this.selectSql = selectSql;
    }

    @Required
    public void setInsertSql(String insertSql) {
        this.insertSql = insertSql;
    }

...

基于注释的注射不起作用。 有什么想法?

它们都在classpath @ web.xml中。

异常

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountDao' defined in class path resource [META-INF/spring/root/dao-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Properties 'insertSql' and 'selectSql' are required for bean 'accountDao'

2 个答案:

答案 0 :(得分:0)

春天是两种注射方式。 你使用二次注入setter。 在Setter Injection中必须是注入对象的setter方法。 然后必须在AccountDAOImpl中添加以下方法:

public void setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate  namedParameterJdbcTemplate) {
this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
}

我希望能帮助你。

答案 1 :(得分:0)

config-dao.xml 的解决方案是简单的$ {}:

<property name="insertSql" value="${sql.accountdao.insert}" />
<property name="selectSql" value="${sql.accountdao.select}" />