Spring getBean with type validation

后端 未结 2 419
南笙
南笙 2021-01-04 12:49

I\'m using the method ApplicationContext.getBean(String name, Class requiredType). The bean is of type util:set. My code looks like:

Set mySe         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-04 13:18

    maybe this can be usefull to you:

    Set setBean= null;
    Map beans = applicationContext.getBeansOfType(Set.class);
    for (Map.Entry bean: beans.entrySet()) {
        ParameterizedType thisType = (ParameterizedType) bean.getClass().getGenericSuperclass();
        Class parametrizedClass= thisType.getActualTypeArguments()[0];
        if (parametrizedClass.isAssignableFrom(String)) {
            setBean= (Set) bean;
        }
    }
    

    http://javahelp.redsaltillo.net

提交回复
热议问题