Java Spring getBeanNamesForType工作不正常

Java Spring getBeanNamesForType工作不正常,java,spring,Java,Spring,我有简单的上下文配置 package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.con

我有简单的上下文配置

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.ResolvableType;

import java.util.Arrays;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);

        String[] namesForType = ctx.getBeanNamesForType(ResolvableType.forClassWithGenerics(Transformer.class, Integer.class, String.class));

        System.out.println("namesForType = " + Arrays.toString(namesForType));
    }

    @Bean
    public Transformer<Integer, String> stringTransformer() {
        return new Transformer<Integer, String>();
    }
}
package.com.example;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入org.springframework.context.ConfigurableApplicationContext;
导入org.springframework.context.annotation.Bean;
导入org.springframework.core.ResolvableType;
导入java.util.array;
@SpringBoot应用程序
公共类演示应用程序{
公共静态void main(字符串[]args){
ConfigurableApplicationContext ctx=SpringApplication.run(DemoApplication.class,args);
String[]namesForType=ctx.getBeanNamesForType(ResolvableType.forClassWithGenerics(Transformer.class,Integer.class,String.class));
System.out.println(“namesForType=“+Arrays.toString(namesForType));
}
@豆子
公共变压器stringTransformer(){
返回新变压器();
}
}
简单类

package com.example;

public class Transformer<F, T> {

    T transform(F from) {
        return null;
    }
}
package.com.example;
公共级变压器{
T变换(F自){
返回null;
}
}
启动应用程序时,输出为namefortype=[]

如果bean的创建被更改为单独的类,如

    @Bean
    public Transformer<Integer, String> stringTransformer() {
        return new Transformer<Integer, String>(){};
    }
@Bean
公共变压器stringTransformer(){
返回新的Transformer(){};
}

输出为namesForType=[stringTransformer],这是正确的行为。在使用@Bean注释之前,它不会在spring上下文中出现。

问题可能出在
org.springframework.core.ResolvableType#isInstance
obj.getClass()


可以注入正确的bean。

问题已在Spring JIRA中报告。请对这个问题投赞成票。

你说的单独上课是什么意思?我在你的代码中没有看到单独的类。很抱歉,这是匿名类
newtransformer(){}
这不是真的。名为“stringTransformer”的Bean位于上下文中,但spring无法使用可解析类型获取它。在上下文关闭之前尝试以下代码:
ctx.getBean(“stringTransformer”)
public boolean isInstance(Object obj) {
    return (obj != null && isAssignableFrom(obj.getClass()));
}
public boolean isAssignableFrom(Class<?> other) {
    return isAssignableFrom(forClass(other), null);
}
@Autowire
private Transformer<String, Integer> transformer;