Spring getBean with type validation

后端 未结 2 427
南笙
南笙 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条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-04 13:15

    Not really but there is a runtime workaround that at least removes the need for an @SuppressWarnings. You can make your class abstract and let Spring instrument your code:

    public abstract class Test {
      Set getMyBean();
    }
    

    and then inject a lookup method in your XML config:

    
      
    
    

    It's not really statically checked but it fails-fast at runtime and you keep the ugly casting out of your code.

提交回复
热议问题