有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

基于唯一构造函数的JavaSpringbean解析

如果您能在以下方面帮助我,我将不胜感激。 假设我有以下类和接口:

public interface BaseType {
     public void method();
}

@Component
@Scope(SCOPE_PROTOTYPE)
public class BaseTypeImpl implements BaseType {
    private int num;

    public BaseTypeImpl(int num) {
        this.num = num;
    }

    @Override
    public void method() {
        System.out.println(num);
    }
}

@Component
@Scope(SCOPE_PROTOTYPE)
public class ChildBaseTypeImpl extends BaseTypeImpl {

    String mes;

    public ChildBaseTypeImpl(int num, String mes) {
        super(num);
        this.mes = mes;
    }

    @Override
    public void method() {
        super.method();
        System.out.println(mes);
    }
}

@Component
@Scope(SCOPE_PROTOTYPE)
public class SecondaryTypeImpl implements BaseType {

    private String str;

    public SecondaryTypeImpl(String str) {
        this.str = str;
    }

    @Override
    public void method() {
        System.out.println(str);
    }
}

结果,我有3个类实现了1个接口。 所有类都具有具有不同参数的非默认构造函数。 有没有一种方法可以根据构造函数参数通过BaseType接口对正确的bean进行Spring查找? 我想这样做:

ApplicationContext ctx = new FileSystemXmlApplicationContext("beans.xml");
ctx.getBean(BaseType.class, 11); //Should return instance of BaseTypeImpl class object, since it has constructor taking int

或者像这样:

ctx.getBean(BaseType.class, 11, "hello!"); //Should return instance of ChildBaseTypeImpl

尝试执行此操作将导致异常:

Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'springtest.BaseType' available: expected single matching bean but found 3: baseTypeImpl,childBaseTypeImpl,secondaryTypeImpl

似乎没有直接的方法可以做到这一点

但是,是否有可能获取所有可从BaseType赋值的类,在它们之间找到合适的构造函数,然后使用*Impl类作为第一个参数调用getBean方法

更新

谢谢大家的回答! 正如pvpkiran提到的,有一种方法可以通过bean名称获取bean类。 这为反射打开了大门,解决了问题,下面是示例代码:

public class App {

    static ApplicationContext ctx = new FileSystemXmlApplicationContext("beans.xml");

    public static void main(String[] args) {
        BaseType beanByInterface = getBeanByInterface(BaseType.class, "Hello!");
        System.out.println(beanByInterface.getClass());
    }

    public static <T> T getBeanByInterface(Class<T> interf, Object... params) {

        BeanDefinitionRegistry bdr = new SimpleBeanDefinitionRegistry();
        ClassPathBeanDefinitionScanner s = new ClassPathBeanDefinitionScanner(bdr);

        TypeFilter tf = new AssignableTypeFilter(interf);
        s.addIncludeFilter(tf);
        s.scan("springtest"); //Sample project package name
        String[] beans = bdr.getBeanDefinitionNames();

        for(String b : beans) {
            Class<?> type = ctx.getType(b);
            MAIN: for(Constructor cons : type.getConstructors()) {
                if (cons.getParameterCount() == params.length) {
                    for (int i = 0; i < cons.getParameterCount(); i++) {
                        if (!params[i].getClass().equals(cons.getParameterTypes()[i])) { //Will fail comparing primitive and boxed types, just leaving like this for simplicity
                            continue MAIN;
                        }
                    }
                    return (T) ctx.getBean(type, params);
                }
            }
        }

        return null;
    }

}

代码有缺陷,但这只是为了展示概念。 有了bean类,我可以得到正确的bean,它具有所需的构造函数

此示例输出“类springtest.SecondaryTypeImpl”

但我相信这应该是一些UTIL中涉及的常见问题。 我不想发明自行车,但仍然找不到解决办法

更新2

在现有的LIB中似乎没有这样的解决方案,因为这不是最佳实践。 无论如何,这是更新的方法,也许有人会发现它很有用

public static <T> T getBeanByInterface(Class<T> interf, Object... params) {

    String[] beans = ctx.getBeanNamesForType(interf);

    for(String beanName : ctx.getBeanNamesForType(interf)) {
        Class<?> type = ctx.getType(beanName);
        Class<?>[] paramTypes = new Class[params.length];
        //Getting params types
        for (int i = 0; i < params.length; i++) {
            paramTypes[i] = params[i].getClass();
        }

        if (ConstructorUtils.getMatchingAccessibleConstructor(type, paramTypes) != null) {
            return (T) ctx.getBean(type, params);
        }

    }
    return null;
}

共 (2) 个答案

  1. # 1 楼答案

    BeanFactory具有以下getBean()声明

    Object getBean(String name, Object... args) throws BeansException;
    <T> T getBean(Class<T> requiredType) throws BeansException;
    <T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
    Object getBean(String name) throws BeansException;
    <T> T getBean(String name, Class<T> requiredType) throws BeansException
    

    正如您在这里看到的,没有类类型、bean名称和参数的声明(这就是您要寻找的)

    但是您可以使用bean名称和参数来获取bean

     ApplicationContext ctx = new FileSystemXmlApplicationContext("beans.xml");
    
     ChildBaseTypeImpl childBaseTypeImpl = (ChildBaseTypeImpl) ctx.getBean("childBaseTypeImpl", 11, "hello");
     BaseTypeImpl basetypeImpl = (BaseTypeImpl)  ctx.getBean("baseTypeImpl", 11, );  
    
  2. # 2 楼答案

    正如@alexlys所指出的,您不能使用构造函数参数来区分bean。但是你可以用同样的结果做一些不同的事情。这两种方法都假设您可以在编写代码时定义您不需要的bean

    • 您可以创建具有唯一名称的bean,然后通过 名称,而不是界面

    • 您可以添加一些标记接口,如 interface SecondaryType extends BaseType {//empty}。每个bean将只实现一个特定接口,因此您可以使用这样的iterface实例化bean