开发者

Marking a 3rd party class property as required (Java/Spring)

开发者 https://www.devze.com 2022-12-08 05:55 出处:网络
Recently, I hit a very perplexing error while trying to clean up my spring configs. The exception was being thrown from deep within a third party library and was basically an obfuscated NPE.

Recently, I hit a very perplexing error while trying to clean up my spring configs. The exception was being thrown from deep within a third party library and was basically an obfuscated NPE.

What I would like to be able to do is configure spring to require a specific field on this class as being required, but I do not want to build a custom version of the library.

开发者_运维知识库

An obvious solution would be to subclass and mark the setter there, but out of curiosity I was wondering if there were an easy way to do this in spring without having to do the class juggling.

Java 6u10, Spring 2.5.


Write a FactoryBean for your object. Subclassing is not necessary.

Update (based on comments / question clarification)

I finally understand what you're trying to do now :-) There isn't anything in Spring that would do what you want but it's rather trivial to write. You're basically looking at extending org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor except you're specifying the bean class / method names statically instead of relying on them being annotated. You'll need to overwrite isRequiredProperty() method.


I would write a class that extends your curent class and implements the InitalizingBean interface in Spring whichs checks the fields you specify.

0

精彩评论

暂无评论...
验证码 换一张
取 消