English 中文(简体)
Java Generics Issue (w/ Spring)
原标题:Java Generics Issue (w/ Spring)

我认为,我可能是那种时代的受害者,但认为我首先与这里的其他人核对。

我必须这样做:

public interface FooFactory {
   public <T extends Bar> Foo<T> createFoo( Class<T> clazz );
}

起草这一法典是完全有效的。 然而,Im试图通过一个春季<代码>BeanFactory来实施这一功能,我可以这样做。

我想做的是......

public class FooFactoryImpl implements BeanFactoryAware {
   private BeanFactory beanFactory;

   public <T extends Bar> Foo<T> createFoo( Class<T> clazz ) {
      return beanFactory.getBean( ????????? );
   }

   public void setBeanFactory( BeanFactory beanFactory ) {
      this.beanFactory = beanFactory; 
   }
}

你们可以看到,我是否说了? 请参阅以下表格: Foo<T>, where Tlif Bar. 然而,不可能得出一个类别物体<代码>。 Foo<T>,因此我假定我不想做什么是不可能的?

任何其他人都看着这种方式,或者执行我想要做些什么的替代办法?

感谢

安道尔

最佳回答

既然你可以确定哪类人<代码>。 Foo<T> with specialized T in Spring context, i guess that You actual have subclasss of Foo<T>:

abstract public class Foo<T> { ... }

public class FooString extends Foo<String> { ... }
public class FooInteger extends Foo<String> { ... }

-

<bean id = "fooInteger" class = "FooInteger" />
<bean id = "fooString" class = "FooString" />

在这种情况下,你可以利用这样的事实,即类型参数没有从超级等级定义中删除:

public class FooFactory implements ApplicationContextAware {

    private Map<Class<?>, Foo<?>> beans = new HashMap<Class<?>, Foo<?>>();

    @SuppressWarnings("unchecked")
    public <T> Foo<T> createFoo(Class<T> c) {
        return (Foo<T>) beans.get(c);
    }

    @SuppressWarnings("unchecked")
    public void setApplicationContext(ApplicationContext ctx)
            throws BeansException {

        Collection<Foo> candidates = ctx.getBeansOfType(Foo.class).values();
        for (Foo candidate: candidates) {
            Type superclass = candidate.getClass().getGenericSuperclass();
            if (superclass instanceof ParameterizedType) {
                ParameterizedType t = (ParameterizedType) superclass;
                Class<?> p = (Class<?>) t.getActualTypeArguments()[0];
                beans.put(p, candidate);
            }
        }
    }
}
问题回答

是的,这是一种时代的情况。 由于您可以拿到<条码>>>,,你必须使用<条码>。 Foo并压制警告。

@SuppressWarnings("unchecked")
public <T extends Bar> Foo<T> createFoo( Class<T> clazz ) {
      return (Foo<T>) beanFactory.getBean("Name of Bean", Foo.class);
}

您可找到

当然,所有这些假设都假定,你的XML(或不管怎样)会形成一个可使用的<代码>。 Foo。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签