将参数传递给ApplicationContext

发布于 2025-01-08 07:29:49 字数 307 浏览 3 评论 0 原文

我的应用程序有一个 application-context.xml。现在我将 ApplicationContext 实例化为:

ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");

是否可以通过此实例化传递参数,以便这些参数可用于初始化某些 bean 的某些属性?

PS:不使用属性文件。由于参数是运行时生成的,例如可执行 jar 的位置、系统架构、操作系统名称等,这些参数是可变的。

I my application I have an application-context.xml. Now I am instantiating The ApplicationContext as:

ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");

Is it possible to pass parameter through this instantiation so that those parameters could be used to initialize some properties of some beans?

PS: Not using property file. As the parameters are generated run time, like exicutable jar's location, system architecture, os name etc which is variable.

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

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

发布评论

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

评论(2

孤蝉 2025-01-15 07:29:49

您可以使用 applicationContext.xml 中的 >PropertyPlaceholderConfigurer

<context:property-placeholder location="classpath:my.properties"/>

这允许您使用语法直接在 bean 声明中引用属性${myProperty} 假设属性文件包含名为 myProperty 的属性。

如何使用此类属性的示例:

<bean id="foo" class="com.company.Foo">
   <property name="bar" value="${myProperty}"/>
</bean>

另一种替代方案可以基于 @Value 注释由 SpEL 提供支持。

You can use the PropertyPlaceholderConfigurer in your applicationContext.xml

<context:property-placeholder location="classpath:my.properties"/>

This allows you to reference properties directly in your bean declarations using syntax ${myProperty} assuming the properties file contains a property named myProperty.

A sample how you can use such a property:

<bean id="foo" class="com.company.Foo">
   <property name="bar" value="${myProperty}"/>
</bean>

Another alternative could be based on the @Value annotation powered by SpEL.

旧时模样 2025-01-15 07:29:49

这是解决方案,我将其发布,可能对将来的人有帮助:

Bean 类:

public class RunManager {

    private String jarPath;
    private String osName;
    private String architecture;

    public RunManager() {

    }

    public RunManager(String[] args) {
        this.jarPath = args[0];
        this.osName = args[1];
        this.architecture = args[2];
    }

    public String getJarPath() {
        return jarPath;
    }

    public void setJarPath(String jarPath) {
        this.jarPath = jarPath;
    }

    public String getOsName() {
        return osName;
    }

    public void setOsName(String osName) {
        this.osName = osName;
    }

    public String getArchitecture() {
        return architecture;
    }

    public void setArchitecture(String architecture) {
        this.architecture = architecture;
    }       
}

ApplicationContext 的初始化:

DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
BeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(RunManager.class).addConstructorArgValue(args).getBeanDefinition();
beanFactory.registerBeanDefinition("runManager", beanDefinition);
GenericApplicationContext genericApplicationContext = new GenericApplicationContext(beanFactory);
genericApplicationContext.refresh();
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "application-context.xml" }, genericApplicationContext);      

将此 bean 引用注入到 application-context.xml 的另一个 bean:

<bean id="configuration" class="jym.tan.movielibrary.configuration.Configuration" >     
    <property name="runManager" ref="runManager" />
</bean>

谢谢。

Here is the solution, I am posting it, might be helpful to someone in future:

The Bean class:

public class RunManager {

    private String jarPath;
    private String osName;
    private String architecture;

    public RunManager() {

    }

    public RunManager(String[] args) {
        this.jarPath = args[0];
        this.osName = args[1];
        this.architecture = args[2];
    }

    public String getJarPath() {
        return jarPath;
    }

    public void setJarPath(String jarPath) {
        this.jarPath = jarPath;
    }

    public String getOsName() {
        return osName;
    }

    public void setOsName(String osName) {
        this.osName = osName;
    }

    public String getArchitecture() {
        return architecture;
    }

    public void setArchitecture(String architecture) {
        this.architecture = architecture;
    }       
}

The initialization of the ApplicationContext:

DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
BeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(RunManager.class).addConstructorArgValue(args).getBeanDefinition();
beanFactory.registerBeanDefinition("runManager", beanDefinition);
GenericApplicationContext genericApplicationContext = new GenericApplicationContext(beanFactory);
genericApplicationContext.refresh();
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "application-context.xml" }, genericApplicationContext);      

The injection of this bean reference to another bean of application-context.xml:

<bean id="configuration" class="jym.tan.movielibrary.configuration.Configuration" >     
    <property name="runManager" ref="runManager" />
</bean>

Thanks.

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