简体   繁体   English

Spring类路径资源被覆盖

[英]Spring classpath resource overwritten

I have a class which uses the following code: 我有一个使用以下代码的类:

@Value(value = "classpath:mail_template.html")
private Resource template;

According to this tutorial this would allow me to get data from a file inside my jar file using template.getInputStream() . 根据教程,这将允许我使用template.getInputStream()从jar文件中的文件获取数据。 This worked correctly until another mail_template.html was added in another module. 这可以正常工作,直到在另一个模块中添加了另一个mail_template.html

My application has a main_application who's POM has the other modules as dependencies. 我的应用程序有一个main_application ,其POM具有其他模块作为依赖项。 The structure is as follows: 结构如下:

main_application
    |
    - modulue_1 (another module)
        |
        - src/main/java
        - src/main/resources
            |
            - mail_template.html
    - modulue_2 (my module)
        |
        - src/main/java
        - src/main/resources
            |
            - mail_template.html

So now my class uses module_1's mail_template.html instead of the one I have inside my module. 因此,现在我的班级使用module_1的mail_template.html而不是模块内部的类。 As I understand this is the correct behavior since module_1 is loaded first and @Value gets injected with the first mail_template.html that it finds, but this would not be correct in my case since it overrides the resource in module_2 (and subsequent modules as well) that has the same name in module_1. 据我了解,这是正确的行为,因为首先加载了mail_template.html ,并且@Value注入了它找到的第一个mail_template.html ,但这在我的情况下是不正确的,因为它会覆盖module_2(以及后续模块)中的资源)在module_1中具有相同的名称。

Is it possible to specify to use the current class (or module) to find a resource in the src/main/resources directory in the @Value annotation? 是否可以指定使用当前类(或模块)在@Value批注的src/main/resources目录中找到资源? Or must I uniquely name every resource in every module to avoid this problem? 还是必须为每个模块中的每个资源唯一地命名,以避免出现此问题?

I have tried many different "classpath:" routes including: 我尝试了许多不同的“ classpath:”路由,包括:

@Value(value = "classpath:module_2/mail_template.html")
....
@Value(value = "classpath*:mail_template.html")
...
@Value(value = "classpath*:module_2/mail_template.html")

But all of these result in a FileNotFoundException . 但是所有这些都会导致FileNotFoundException

EDIT 编辑

Each module is composed as follows: 每个模块的组成如下:

Maven POM Project
    Maven Java Application
        src/main/java
            base.package.with.module.name.as.suffix.package
        src/main/resources
            resource_files_if_needed

As stated, each module is declared as a dependency under the Maven Java Application main_application and each Maven Java Application uses a "base package" with the module's name as a suffix. 如前所述,每个模块在Maven Java应用程序main_application下都被声明为依赖main_application并且每个Maven Java应用程序使用带有模块名称后缀的“基本包”。

For example: 例如:

// main_application:
    package my.main.application
// module_1:
    package my.main.application.module1
// module_2:
    package my.main.application.module2

This is used so Spring can load the classes with @Component annotations using <context:component-scan base-package="my.main.application" /> in the config file. 使用它是为了使Spring可以使用config文件中的<context:component-scan base-package="my.main.application" />加载带有@Component批注的类。

You can only have one mail_template.html in the classpath. 您在类路径中只能有一个mail_template.html。 This is also true for classes with the same package and class name. 对于具有相同程序包和类名称的类也是如此。

I would suggest to us a different directory name in the resource directory. 我会建议我们在资源目录中使用其他目录名称。

I beleive, it actually skips classpath in the @Value expression. 我相信,它实际上会跳过@Value表达式中的classpath I think the problem is : after classpath . 我认为问题是: classpath之后。 The : in the @Value refers the separator character to separate property key and default value. @Value:表示分隔符,用于分隔属性键和默认值。 For example, below code looks for key project.123.gender and if it is not available then returns Male as default. 例如,下面的代码查找关键project.123.gender ,如果不可用,则默认返回Male

@Value("${project.123.gender:Male}")

Could you try something like below? 您可以尝试以下类似的方法吗? I have not tested it, but looks like that could be one possible issue. 我没有测试过,但是看起来可能是一个可能的问题。 (note the extra : at start) (请注意:开始时)

@Value(value = ":classpath:mail_template.html")

OR 要么

@Value(value = "${:classpath:mail_template.html"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM