java Springframework 构造函数-arg

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7602205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:38:47  来源:igfitidea点击:

Springframework constructor-arg

javaspringdependency-injectionconstructor-injection

提问by user12121

I have a MainClass which have 2 variables. I would like to pass those 2 values to a springframework bean class "Test". how do I define that in applicationContext.xml and also how do I pass those 2 variable values to the bean "Test".

我有一个有 2 个变量的 MainClass。我想将这 2 个值传递给 springframework bean 类“Test”。我如何在 applicationContext.xml 中定义它,以及如何将这 2 个变量值传递给 bean“Test”。

Ex:

前任:

class MainClass {
       public int var1;
      public int var2;
      public Test test;

   public void setVar1(int var11) {
    var1 = var11;
   }

    public void setVar2(int var22) {
        var2 = var22;
    }

   public static void main(String args[]) {

       ApplicationContext context = 
           new FileSystemXmlApplicationContext("applicationContext.xml");
      Test = context.getBean("test");
   }
  }

------------ TEST class ------------

------------ 测试班------------

public class Test {

 public Test (int var1, int var2) {}
}

------------- applicationContext.xml -------------

------------- applicationContext.xml -------------

   <bean id="test" class="com.path.test">
       <constructor-arg index="0" type="int" value="????"/>
       <constructor-arg index="1" type="int" value="????"/>
   </bean>

回答by nicholas.hauschild

You can pass values in like this:

您可以像这样传递值:

<bean id="test" class="com.path.test.Test">
   <constructor-arg index="0" type="int" value="123"/>
   <constructor-arg index="1" type="int" value="456"/>
</bean>

You should remember to put your fully-qualified class name as the value of the classattribute.

您应该记住将完全限定的类名作为class属性的值。

That said, your Testclass is not holding onto its state. If you want to get a hold of the values you specified in your applicationContext.xml, you should create some members of Test:

也就是说,您的Test班级没有保持其状态。如果您想获得您在您的 中指定的值applicationContext.xml,您应该创建以下的一些成员Test

public class Test {
    private int v1;
    private int v2;

    public Test (int var1, int var2) {v1 = var1; v2 = var2;}

    public int getVOne() {
        return v1;
    }

    public int getVTwo() {
        return v2;
    }
}

You should then be able to access these in your mainmethod like this:

然后,您应该能够main像这样在您的方法中访问这些:

public static void main(String args[]) {

    ApplicationContext context = 
       new FileSystemXmlApplicationContext("applicationContext.xml");
    Test test = context.getBean("test");
    int v1 = test.getVOne();
    int v2 = test.getVTwo();

    System.out.println("V1: " + v1 + " V2: " + v2); //output: V1: 123 V2: 456
}

回答by laher

As Nicholas says, it's a bit of a weird thing to do, but you could achieve it using PropertyPlaceholderConfigurer.

正如 Nicholas 所说,这样做有点奇怪,但您可以使用 PropertyPlaceholderConfigurer 来实现它。

Something like this:

像这样的东西:

 public static void main(String args[]) {
        PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
        Properties properties = new Properties();
        properties.setProperty("var1", Integer.toString(var1));
        properties.setProperty("var2", Integer.toString(var2));
        configurer.setProperties(properties);

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

        context.setConfigLocation("applicationContext.xml");
        context.refresh();
        Test test = (Test) context.getBean("test");
 }

NOTE: var1 and var2 should be static if you want to reference them from main()

注意:如果你想从 main() 引用 var1 和 var2 应该是静态的

Then reference the placeholders like so:

然后像这样引用占位符:

<bean id="test" class="com.path.test">
   <constructor-arg index="0" type="int" value="${var1}"/>
   <constructor-arg index="1" type="int" value="${var2}"/>
</bean>

See also this forum thread: http://forum.springsource.org/showthread.php?71815-Passing-Bean-properties-using-java.util.Properties

另请参阅此论坛主题:http: //forum.springsource.org/showthread.php?71815-Passing-Bean-properties-using-java.util.Properties

API doc: http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html

API 文档:http: //static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html

HTH

HTH