java - 无法打开 ServletContext 资源

标签 java spring glassfish web.xml

这与一个老问题非常相似,但解决方案对我不起作用。

我有一个 WAR 包。

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:application-context.xml</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

application-context.xml

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:social.properties</value>
    </property>
</bean>

但是得到这个:

org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/social.properties]

我检查了 WAR 包 - .xml.properties 文件都在 /WEB-INF/classes

.properties 文件在 src/main/resources.xmlsrc/main/java (在默认包中)和maven在默认包中正确传输它们(我认为) WEB-INF/classes

有谁知道我为什么会得到这个异常?谢谢。

编辑:我只想补充一点,JUnit 测试正常运行(我的意思是它们从 social.properties 加载了它们应该加载的内容)但是在运行应用程序时它会忽略我的 classpath:前缀

最佳答案

不要使用类路径。这可能会导致不同的 ClassLoader(容器与应用程序)出现问题。 WEB-INF 永远是更好的选择。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="location">
         <value>/WEB-INF/social.properties</value>
     </property>
</bean>

关于java - 无法打开 ServletContext 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6035464/

相关文章:

java - 编译Java文件时出现问题

java - 组织.postgresql.util.PSQLException : ERROR: relation "app_user" does not exist

java - 将字符串错误响应转换为 Http 状态代码

使用 JWT 的 Spring Security 总是返回 401 未经授权

netbeans - GlassFish Server 4.0 与 JDK 启动错误

mysql - GlassFish 无法连接到 mysql (XAMPP)

java - 单击 TextView 中的链接时应用程序崩溃

java - java 上的 json(包含很少的对象)到 TreeMap

java - 由于@ManyToOne 关系,使用 Hibernate 模式导出器生成模式失败

web-services - 如何将 Web 服务绑定(bind)到特定的 glassfish 端口?