使用 Spring 加载 webserver context.xml

发布于 2024-12-10 09:21:26 字数 1242 浏览 0 评论 0原文

对 Spring 相当陌生,所以我在这方面遇到了一些麻烦。我正在尝试将 LDAP 安全性与 Spring 结合使用。我可以使用我在 web 应用程序本身内部创建的属性文件。但我想做的是加载并读取服务器的 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。是否需要是服务器上的绝对路径?

谢谢 克里斯

Fairly new to Spring, so I'm having some trouble with this. I'm trying to use LDAP security with Spring. I can use a properties file I created inside the webapp itself. But what I would like to do is load and read the context.xml file of the server (it has all the values I need for this and other applications).

This is what I have:

<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>

I'm able to load and read the 2 properties files, but the context.xml is not found. Does it need to be the absolute path on the server?

Thanks
Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从来不烧饼 2024-12-17 09:21:26

所以我建议的第一件事是使用 Spring Security。它已经内置了 LDAP 支持。


但是没有找到context.xml

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

例如:

<?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="[email protected]"
    mail.smtp.host="mail.example.com"
    mail.smtp.from="[email protected]"/>

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

So the first thing I would recommend is to use Spring Security. It has an already build in LDAP support.


but the context.xml is not found

Normally this (reading the context.xml directly) is not the way you should go.
Instead, define some properties and or JNDI resources in the context.xml and then use them in the spring configuration.

For example:

<?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="[email protected]"
    mail.smtp.host="mail.example.com"
    mail.smtp.from="[email protected]"/>

 <Parameter name="simpleValue" value="any" override="false" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文