将属性传递给Spring上下文

Ric*_*ler 8 java spring rmi

我正在使用Spring来处理对某些远程服务器的RMI调用.构建应用程序上下文并从客户端获取远程调用的bean是很简单的:

ApplicationContext context = new ApplicationContext("classpath:context.xml");

MyService myService = (MyService ) context.getBean( "myService " );
Run Code Online (Sandbox Code Playgroud)

但是,我没有看到将属性传递到配置的简单方法.例如,如果我想在客户端的运行时确定远程服务器的主机名.

理想情况下,我在Spring上下文中有一个条目,如下所示:

<bean id="myService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
  <property name="serviceUrl" value="rmi://${webServer.host}:80/MyService"/>
  <property name="serviceInterface" value="com.foo.MyService"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

并将属性作为参数从客户端传递给上下文.

我可以在上下文中使用PropertyPlaceholderConfigurer来替换这些属性,但据我所知,这仅适用于从文件中读取的属性.

我有一个实现解决这个问题(作为答案添加),但我正在寻找一个标准的Spring实现,以避免我自己.是否有另一个Spring配置程序(或其他任何东西)来帮助初始化配置,或者我最好看看java配置来实现这一点?

小智 13

http://forum.springsource.org/showthread.php?t=71815

TestClass.java

package com.spring.ioc;

public class TestClass {

    private String first;
    private String second;

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }
}
Run Code Online (Sandbox Code Playgroud)

SpringStart.java

package com.spring;

import java.util.Properties;

import com.spring.ioc.TestClass;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class SpringStart {
    public static void main(String[] args) throws Exception {
    PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
    Properties properties = new Properties();
    properties.setProperty("first.prop", "first value");
    properties.setProperty("second.prop", "second value");
    configurer.setProperties(properties);

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
    context.addBeanFactoryPostProcessor(configurer);

    context.setConfigLocation("spring-config.xml");
    context.refresh();

    TestClass testClass = (TestClass)context.getBean("testBean");
    System.out.println(testClass.getFirst());
    System.out.println(testClass.getSecond());
    }
}
Run Code Online (Sandbox Code Playgroud)

spring-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="testBean" class="com.spring.ioc.TestClass">
        <property name="first" value="${first.prop}"/>
        <property name="second" value="${second.prop}"/>
    </bean>

</beans>
Run Code Online (Sandbox Code Playgroud)

输出:

first value
second value
Run Code Online (Sandbox Code Playgroud)


Rob*_*anu 1

更新

根据问题更新,我的建议是:

  1. 创建一个ServiceResolverbean,根据客户端输入处理您需要处理的任何内容;
  2. 将此 bean 声明为相关服务的依赖项;
  3. 在运行时,您可以按照您认为合适的方式更新/使用此 bean。

ServiceResolver然后,可以在每次init-method调用时基于 JNDI 查找或环境变量确定要返回给客户端的值。

但在此之前,您可能需要查看可用的配置选项。您可以:

  • 添加编译时不必存在的属性文件;
  • 从 JNDI 查找值;
  • 从 System.properties 获取值。

如果您需要从自定义位置查找属性,请查看org.springframework.beans.factory.config.BeanFactoryPostProcessororg.springframework.beans.factory.config.PropertyPlaceholderConfigurer实现方式。

基本思想是,您获得具有“原始”属性的 bean,例如,${jdbcDriverClassName}然后您可以解析它们并将它们替换为所需的值。