views:

57

answers:

2

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.

+1  A: 

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.

ChssPly76
Maybe I'm missing something, but I dont quite see how that would help. Specifically, I am asking about adding a @Required to a specific method, not initialize fields to a specific value. I dont understand how writing a factory will let me define metadata on methods.
liam
@Required is not the only way to ensure that specific property is set; you can do so manually in initialization callback which you can get from either FactoryBean or by subclassing and implementing InitializingBean as Gandalf suggested. Or you can annotate the corresponding setter on FactoryBean with @Required.
ChssPly76
I understand I can manually test fields, I really dont see a benefit to writing a factory, or having a constructor do the work instead of the simple subclass. Ideally, I am looking for a way that I could set up a section in my spring configs where: -- I give a fully qualified class name, and a list of pattern of methods Spring applies the same logic and prints the same error as using @Required. If I find other properties on classes that my application requires, I could add those in to do startup checks.I am guessing there is not. Thanks for the suggestions, though.
liam
Not quite what I was looking for, but a workable suggestion, and a path I didn't even consider. Thanks!
liam
+1  A: 

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

Gandalf
Subclassing, and marking the methods as @Required is what I am planning, I am specifically wondering if there is an AOP-style way of marking methods/classes as required through configs. The way spring/hibernate allows you to define pointcuts for transactions.
liam
I'm going with my original trivial subclass. Generally, I was looking for a way to mark 3rd party setters as required without subclassing or compiling a custom version of the jar. Thanks for the suggestion, though.
liam