如何从Main类将属性注入spring bean [英] How to inject properties into a spring bean from Main class

查看:171
本文介绍了如何从Main类将属性注入spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有我的应用程序的spring,并且我能够将类路径上的某个文件中的一些属性注入到我的应用程序中,一切都运行良好。 ie

I'm using spring with my application, and I'm able to inject some properties from some file on a class path into my app and everything works perfectly. i.e.

<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>classpath:application.properties</value>
            </list>
        </property>
    </bean>

现在我可以使用 $ {any.property.from.application.properties我的春天语境中的。在我的主要课程中:

Now I can use ${any.property.from.application.properties} in my spring context. And in my main class :

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("appContext.xml");

它也有效,我的问题是如何在没有它的情况下在spring上下文中注入属性文件位置最初在那里,我想让我的应用程序可配置。如果我正在从 C:\ dir / user / home / dir 执行我的应用程序,我认为在应用程序上下文的值应为 C:\ application.properties /user/home/dir/application.properties

It works as well, my question is how do I inject property file location in the spring context without it being there at first, I want to make my app configurable. If I'm executing my app from C:\dir or /user/home/dir I assume that in the application context the value should be either C:\application.properties or /user/home/dir/application.properties

推荐答案

我有时会遇到类似的问题。我的要求是属性文件没有捆绑在应用程序内(因此不在类路径中)。该文件可以位于文件系统中的任何位置。
以下是我解决它的方法,

I had a similar problem sometime back. My requirement was the the property files is not bundled inside the application (and hence not in classpath). The file could be at any location in file system. Here is how I solved it,


  1. 定义一个环境变量,其值指向application.properties的位置。

  2. 假设我们有一个env变量APP_PROP_HOME,其值为/ user / home / dir /

  3. 现在定义ServletContextPropertyPlaceholderConfigurer时,定义如下位置

我正在重用你的例子

<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>file://${APP_PROP_HOME}/application.properties</value>
            </list>
        </property>
    </bean>

Spring将$ {APP_PROP_HOME}解析为存储在相应env属性中的值,并且您的应用程序配置为运行时。

The Spring resolves ${APP_PROP_HOME} to the value stored in corresponding env property and your application is configured at runtime.

这篇关于如何从Main类将属性注入spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆