【问题标题】:Configuring PropertySourcesPlaceholderConfigurer for WAR为 WAR 配置 PropertySourcesPlaceholderConfigurer
【发布时间】:2013-06-10 09:26:32
【问题描述】:

我正在尝试创建一个可以使用弹簧属性文件的 WAR,但我似乎遇到了一些问题。

我可以在目标的基本目录结构中看到我的属性文件,当我进行爆炸战争时,我可以在 WAR 中看到我的属性文件。但是,当我尝试制作正常的 WAR 并进行部署时,我发现资源未找到异常,具体来说:

org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [service.properties] cannot be opened because it does not exist

从码头我没有这样的问题。

这是我对资源的spring java配置:

@Bean
    public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {

        PropertySourcesPlaceholderConfigurer props = new PropertySourcesPlaceholderConfigurer();
        props.setLocations(new Resource[] { new ClassPathResource("service.properties") });
        return props;
    }

这是我的 POM 的相关部分。我在 WAR 配置中添加了因为 maven 似乎要这样做here,但它似乎没有帮助。

<build>
                <resources>
                    <resource>
                        <directory>src/main/config/local</directory>
                        <includes>
                            <include>**/*.properties</include>
                        </includes>
                    </resource>
                </resources>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.3</version>
                        <goals>
                            <goal>war</goal>
                        </goals>
                        <configuration>
                            <webResources>
                                <resource>
                                    <!-- this is relative to the pom.xml directory -->
                                    <directory>src/main/config/local</directory>
                                    <includes>
                                        <include>**/*.properties</include>
                                    </includes>
                                </resource>
                            </webResources>
                        </configuration>
                    </plugin>

有人知道我做错了什么吗?谢谢!

【问题讨论】:

    标签: java spring properties war


    【解决方案1】:

    您确定您的属性文件在WEB-INF 文件夹中吗?如果它在外部并且只包含在 war 文件的根目录中,那么它将不会在类路径中找到。请参阅maven-war-plugin documentation - 覆盖您需要添加的默认位置(war 文件的根目录)

    <targetPath>WEB-INF</targetPath>
    

    【讨论】:

      猜你喜欢
      • 2020-08-19
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      • 2019-10-13
      • 2012-11-23
      • 1970-01-01
      • 2020-01-21
      • 2016-07-19
      相关资源
      最近更新 更多