本文介绍了在Spring xml配置中创建Guava TypeToken?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够注入 Guava TypeToken 对象.有什么好方法吗?有没有人写任何课程/图书馆来简化这一过程?

I'd like to be able to inject Guava TypeToken objects by specifying them as a bean in a Spring xml configuration. Is there a good way to do this? Has anyone written any cade/library to make this easier?

TypeToken似乎通过使用反射来内省其泛型类型而起作用,因此它是使用诸如以下的匿名类构造的:

TypeToken seems to work by using reflection to introspect its generic types and is thus constructed using an anonymous class like:

new TypeToken<List<String>>() {}

Spring的xml config语法似乎根本不接受泛型,这大概是因为它是在运行时构建的,不需要需要"它们(因为泛型是编译时检查并且在运行时从技术上删除了).

Spring's xml config syntax doesn't seem to accept generics at all, presumably because it's built at runtime and doesn't "need" them (since generics are compile time checks and technically erased at runtime).

所以我知道实例化TypeToken bean的唯一方法是做到在Java中:

So the only way I know to instantiate a TypeToken bean is to do it in java:

TokenConfig.java:

TokenConfig.java:

@Configuration
public class TokenConfig {
  @Bean
  public TypeToken<List<String>> listOfStringsToken() {
      return new TypeToken<List<String>>() {};
  }
}

system-test-config.xml:

system-test-config.xml:

<beans>
  <context:annotation-config/>
  <bean class="com.acme.TokenConfig"/>
  <bean class="com.acme.Consumer">
    <property name="typeToken" ref="listOfStringsToken"/>
  </bean>
</beans>

有没有一种方法可以仅使用xml配置?

Is there a way to do this with just an xml config?

推荐答案

要回答我自己的问题:可以使用静态构造函数 TypeToken.of(Class),但这不适用于更深的泛型类型.

To answer my own question:It IS possible to create a non-generic TypeToken using the static constructor TypeToken.of(Class), but this wont work for deeper generic types.

这是Spring xml配置:

Here's the Spring xml config:

<bean class="com.google.common.reflect.TypeToken" factory-method="of">
  <constructor-arg type="java.lang.Class" value="java.lang.Integer" />
</bean>

等价于:

TypeToken.of(Integer.class)

new TypeToken<Integer>() {}

我还找到了一种使用 TypeToken.of(Type)构造函数使用 ParameterizedType > Google Guice 的类型实用程序. 番石榴有一个,但它不是公开的. :'(我不确定这是否与使用TypeToken/TypeCapture一样健壮,但似乎可行.不幸的是,这很丑陋而且很长……(也许有人可以简化它?)

I also found a way to use the TypeToken.of(Type) constructor with a ParameterizedType constructed using Google Guice's Types utility. Guava has one too, but it's not public. :'(I'm not sure if this is quite as robust as using TypeToken/TypeCapture, but it seems to work. Unfortunately it's pretty ugly and long... (maybe someone can simplify it?)

<bean class="com.google.common.reflect.TypeToken" factory-method="of">
  <constructor-arg index="0">
    <bean class="com.google.inject.util.Types" factory-method="newParameterizedType">
      <constructor-arg index="0">
        <value type="java.lang.Class">java.util.List</value>
      </constructor-arg>
      <constructor-arg index="1">
        <array><value type="java.lang.Class">java.lang.String</value></array>
      </constructor-arg>
    </bean>
  </constructor-arg>
</bean>

等价于:

new TypeToken<List<String>() {}

这篇关于在Spring xml配置中创建Guava TypeToken?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 15:44