Spring 将 xml 值注入属性 - config.xml 然后注入 bean

Spring inject xml values to properties-config.xml then to a bean

我是 Spring 的菜鸟,但还没有找到任何基于这种类型的注入的 material。

有一个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;
    }

...

基于注释的注入不起作用。 有人知道吗?

它们都在类路径中@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'

在spring中是拖曳式喷射。 您使用 setter 注入两种方式。 在 Setter 注入必须是注入对象的 setter 方法。 那么AccountDAOImpl必须添加如下方法:

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

希望能帮到你

在 web.xml 中必须声明此 xml 文件:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/[Your DIR]/[Your XMLFiles].xml 
        </param-value>
</context-param>

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

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