Java 使用具有泛型参数的类定义 spring bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/800313/
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-08-11 19:37:40  来源:igfitidea点击:

Defining spring bean using a class with generic parameters

javagenericsspring

提问by digiarnie

If I have a class that looks something like this:

如果我有一个看起来像这样的课程:

public class MyClass<T extends Enum<T>> {
  public void setFoo(T[] foos) {
    ....
  }
}

How would I go about declaring this as a bean in my context xml so that I can set the Foo array assuming I know what T is going to be (in my example, let's say T is an enum with the values ONE and TWO)?

我将如何在我的上下文 xml 中将它声明为一个 bean,以便我可以设置 Foo 数组,假设我知道 T 将是什么(在我的示例中,假设 T 是一个枚举值 ONE 和 TWO)?

At the moment, having something like this is not enough to tell spring what the type T is:

目前,拥有这样的东西还不足以告诉 spring T 是什么类型:

<bean id="myClass" class="example.MyClass">
  <property name="foo">
    <list>
      <value>ONE</value>
      <value>TWO</value>
    </list>
  </property>
</bean>

Edit: Forgot the list tag.

编辑:忘记了列表标签。

采纳答案by Arne Burmeister

Spring has no generic support for that case, but the compiler just creates a class cast in this case. So the right solution is:

Spring 没有对这种情况的通用支持,但编译器只是在这种情况下创建了一个类。所以正确的解决办法是:

<bean id="myClass" class="example.MyClass">
  <property name="foo">
    <list value-type="example.MyEnumType">
      <value>ONE</value>
      <value>TWO</value>
    </list>
  </property>
</bean>

回答by cletus

<bean id="myClass" class="example.MyClass">
  <property name="foo">
    <list>
      <value>ONE</value>
      <value>TWO</value>
    </list>
  </property>
</bean>

Alternatively, you can define a custom editor.

或者,您可以定义自定义编辑器

回答by Rod McDonald

Consider working example.

考虑工作示例。

<bean id="simpleInt" 
      class="org.nipr.gateway.service.transaction_assistant.GenericSimple">
    <constructor-arg>
        <!-- this can be any full path to a class -->
        <value>java.lang.Integer</value> 
    </constructor-arg>
</bean>

and

<bean id="simpleString"  
      class="org.nipr.gateway.service.transaction_assistant.GenericSimple">
    <constructor-arg>
        <value>java.lang.String</value>
    </constructor-arg>
</bean>

Simple generic class:

简单的泛型类:

public class GenericSimple<T> {
    private Class<T> type;
    public GenericSimple(Class<T> type) {
        this.type = type;
    }
    public T get( T t) {
        return t;
    }
}

And finally, the test method (using factory):

最后,测试方法(使用工厂):

public void testGeneric(){
    Factory factory = new Factory(new String[]{"config/beanForGenericTest.xml"});

    GenericSimple<Integer> simpleInt 
        = (GenericSimple<Integer>)factory.getClass("simpleInt");
    System.out.println(simpleInt.get(Integer.valueOf(100)));
    Assert.assertTrue(simpleInt.get(Integer.valueOf(100)).equals(100));

    GenericSimple<String> simpleString = 
        (GenericSimple<String>)factory.getClass("simpleString");
    System.out.println(simpleString.get(new String("Rockets go fast.")));
    Assert.assertTrue(simpleString.get("Rockets go fast.")
        .equals("Rockets go fast."));
}