代码之家  ›  专栏  ›  技术社区  ›  Yakhoob

无法将属性值设置为Spring web。xml

  •  0
  • Yakhoob  · 技术社区  · 8 年前

    因为它无法获取并将属性设置为xml

    这是我的春网。xml

        <bean id="dataSource"
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    
        <property name="driverClassName" value="com.ibm.as400.access.AS400JDBCDriver" />
        <property name="url" value="jdbc:as400://localhost/BB" />
        <property name="username" value="{as400.username}" />
        <property name="password" value="{as400.password}" />
    </bean>
    

    我的加载属性类

    public class LoadProperties implements ServletContextListener {
    
    private static Properties properties = null;
    private static Logger logger = Logger.getLogger(LoadProperties .class); 
    
    @Override
    public void contextDestroyed(ServletContextEvent arg0) { }
    
    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        properties = BBUtil.getProperties("datasource-cfg.properties");
        for (String prop : properties.stringPropertyNames()) {
            logger.info("Property Loaded :"+properties.getProperty(prop));
            if (System.getProperty(prop) == null) {            
                System.setProperty(prop, properties.getProperty(prop));
            }
        }
    }
    
    }
    

    该类正在执行并在System下设置属性。

    这是我的属性文件

    as400.username=ROOT
    as400.password=ROOT
    

    如何将这些值设置到spring web中。xml

    如果您有任何想法,我们将不胜感激。

    1 回复  |  直到 8 年前
        1
  •  0
  •   amdg    8 年前

    我们使用 context:property-placeholder

    <context:property-placeholder location="classpath:db.properties" />
    
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="dbHost" value="${db.url}"/>    
            <property name="dbPort" value="${db.number}"/>
            <property name="dbService" value="${db.name}"/>
            <property name="dbUrl" value="${db.user}"/>
            <property name="dbPassword" value="${db.password}"/>
        </bean>
    

    <property name="username" value="${as400.username}" />
    <property name="password" value="${as400.password}" />
    

    ExtendedIllegalArgumentException本质上表示传递的参数无效。