使用 Spring XML 实体在带注释的文件中设置属性?

发布于 2024-08-18 02:49:07 字数 857 浏览 4 评论 0原文

我有一个 Spring MVC 控制器类,需要从 config.properties 文件设置属性。我在想,既然我已经使用这个配置文件来设置一些数据库属性,我可以以类似的方式访问它来设置这个属性。不同之处在于我的 Controller 类是带注释的,而不是在 XML 文件中声明,因此我无法以通常的方式设置属性。我已准备好在 XML 文件中使用 config.properties,如下所示:

    <context:property-placeholder location="/WEB-INF/config.properties" />

我想通过此属性文件中的条目在我的控制器类中设置以下属性:

@Controller
public class SampleUploadController {

    private String audioFilePath;
    public String getAudioFilePath() {
        return audioFilePath;
    }
    // I want this set from the properties file I've declared in my 
    // XML file: e.g. ${props.audioFilePath}
    public void setAudioFilePath(String audioFilePath) {
        this.audioFilePath = audioFilePath;
    } 
}

这可能吗?如果没有,有人可以建议如何从配置文件中获取我需要的属性吗?它位于我的根 WEB-INF 中。问题是我目前无法访问 ServletContext 来获取该文件所在位置的引用。

I have a Spring MVC Controller class that needs a property set from a config.properties file. I was thinking that since I already use this config file to set some db properties, I could access it in a similar way to set this property. The difference is that my Controller class is annotated and not declared in an XML file, so I can't set the property that usual way. I have my config.properties ready to be used in my XML file as so:

    <context:property-placeholder location="/WEB-INF/config.properties" />

I would like to set the following property in my controller class from an entry in this properties file:

@Controller
public class SampleUploadController {

    private String audioFilePath;
    public String getAudioFilePath() {
        return audioFilePath;
    }
    // I want this set from the properties file I've declared in my 
    // XML file: e.g. ${props.audioFilePath}
    public void setAudioFilePath(String audioFilePath) {
        this.audioFilePath = audioFilePath;
    } 
}

Is this possible. If not, can somebody suggest how to get the property I need from the configuration file? It is located in my root WEB-INF. The problem is that I do not have access to ServletContext at this point to get a reference to where this file is.

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

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

发布评论

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

评论(2

相守太难 2024-08-25 02:49:07

在 Spring 3 中,您可以使用 @Value 注释来执行此操作,例如

@Value("${props.audioFilePath}")
public void setAudioFilePath(String audioFilePath) {
    this.audioFilePath = audioFilePath;
} 

In Spring 3, you can use the @Value annotation to do this, e.g.

@Value("${props.audioFilePath}")
public void setAudioFilePath(String audioFilePath) {
    this.audioFilePath = audioFilePath;
} 
无法回应 2024-08-25 02:49:07

Skaffman 是正确的,但请注意,通过加载使用的属性只能在其自己的上下文中工作。例如,如果您有一个加载属性的应用程序上下文父模块,则不能在具有这些属性的 servlet 上下文中分配的 bean 中使用 @Value...它们只是不可见。

Skaffman is correct, but be aware that properties loaded for use via only work in their own context. E.g., if you have an application context parent module which loads the properties, you cannot use @Value in beans allocated in the servlet context with those properties... they're just not visible.

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