使用Spring加载webserver context.xml

时间:2011-10-17 15:46:36

标签: spring

Spring相当新,所以我遇到了一些问题。我正在尝试使用Spring的LDAP安全性。我可以使用我在webapp本身内创建的属性文件。但我想要做的是加载并读取服务器的context.xml文件(它具有我和其他应用程序所需的所有值)。

这就是我所拥有的:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
    </bean>   
    <bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
        <property name="searchContextAttributes" value="true"/>
        <property name="contextOverride" value="true"/>
        <property name="ignoreResourceNotFound" value="true"/>
        <property name="locations">
            <list>

                <value>/WEB-INF/properties/dataUploadProperties.properties</value>
                <value>/WEB-INF/properties/globalProperties.properties</value>
                <value>context.xml</value>

            </list>
        </property>
    </bean>

我能够加载和读取2个属性文件,但找不到context.xml。它是否需要是服务器上的绝对路径?

由于 克里斯

1 个答案:

答案 0 :(得分:0)

所以我建议首先使用Spring Security。它已经构建了LDAP支持。


  

但找不到context.xml

通常这个(直接读context.xml)不是你应该去的方式。 相反,在context.xml中定义一些属性和/或JNDI资源,然后在弹簧配置中使用它们。

例如:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:jee="http://www.springframework.org/schema/jee"
       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-3.0.xsd
               http://www.springframework.org/schema/jee
               http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

    <!-- access via jndi -->
    <jee:jndi-lookup id="jndiEmailSession"
        jndi-name="java:comp/env/email/session/myEmailSession" />

    <!-- direct access for properties required the SERVLET contect property
         place older configurer, then it works like properties from normal
         property files -->
    <bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">      <property name="locations" value="classpath*:META-INF/spring/*.properties" />   </bean>

   <bean class=Demo>
      <property name="someString" value="${simpleValue}" />
   </bean>
</beans>

context.xml中:

<Resource name="email/session/myEmailSession"
          type="javax.mail.Session"
          auth="Container"                          
    password="secret"
    mail.debug="false"
    mail.transport.protocol="smtp"  
    mail.smtp.auth="true"
    mail.smtp.user="test@example.com"
    mail.smtp.host="mail.example.com"
    mail.smtp.from="test@example.com"/>

 <Parameter name="simpleValue" value="any" override="false" />