【问题标题】:Not able to set property values to Spring-web.xml无法将属性值设置为 Spring-web.xml
【发布时间】:2017-10-31 08:01:32
【问题描述】:

我有一个上下文监听器,我在其中加载所有属性。我试图在我的 spring-web.xml 中设置这些属性,但它会引发异常

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

这是我的 spring-web.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));
        }
    }
}

}

这个类正在执行并设置系统下的属性。

这是我的属性文件

as400.username=ROOT
as400.password=ROOT

如何将值设置到我的 spring-web.xml 中

任何想法将不胜感激。

【问题讨论】:

    标签: java spring spring-mvc properties


    【解决方案1】:

    我们使用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>
    

    属性文件也可以使用前缀加载,例如 http:,file:

    占位符应与 $ 符号一起使用,如下所示

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

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

    【讨论】:

      猜你喜欢
      • 2011-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      • 2014-08-25
      • 2013-04-21
      • 2013-08-16
      相关资源
      最近更新 更多