Initalization of Class properties by Spring

I need to initialize a property of a class , which contains a path to a folder.

I used this property within the default constructor of the class.

it seems that Spring does not initialize this property before it executes the constructor, thus i get a crash with NullPointerException.

 

Alternatively i need to get a relative path to that Sprin bean, which runs under Tomcat. Had this been a Servlet, there would be no problem to get relative path. But how do i achieve same result with Spring ?

 

Comments

1. You can't expect Spring to inject properties to a class that wasn't constructed yet... You can use constructor parameters injection.

2. Please elaborate...

1. i have a list of files in some directory (main/java/resources/document - maven style)
2. during the server startup i need to read those files into memory (initialization process)
3. path of directory, naturally, needs to be relative.

Initialization process at the moment executed at the constructor, which makes Spring useless, so far.

All i found on the web point to definition of system property on java arguments.

Digging long enough on google i learned a proper Spring way to handle the situation:
InitializingBean interface with afterPropertiesSet method... does the trick

@PostConstruct is so much nicer, though. Standard, allows meaningful method(s) names and is Spring interfaces free.

@PostConstruct IS nicer.

However, I strongly advice using XML. The reasons are:

1. XML helps you organize you're DI setup in a better way (DI is managed in one or few known files. Much like Guice modules). When spreading annotations across the source code it is very easy to lose control. Especially when no naming conventions are used. You'll quickly find yourself running "find in path..." every time you want to list your beans/services/components/etc. This is, BTW, told to be one of the reasons JBoss Seam didn't lift (even though Seam support XML configuration as well).
2. Spring literature almost always uses XML configuration.


1. agree, sometimes xml is better over annotations. However, in the case of PostConsruct, the equivalent is init-method="method_name" in the xml, and that, in my opinion, breaks the object's encapsulation. Whatever initialisation method the object needs, it is nobody's business but his own (and the person who wrote it). Out of the 3 options (xml, interface, annotations), I think xml is the least appropriate in this case.

2. Spring Reference uses xml, true. why? who knows. maybe the Spring folks haven't had the time or motivation to move all their xml samples to annotations. maybe they think it's more appropriate for a guide, being more methodological (same approach implemented in Tikal's Spring 2.5 course). maybe they think annotations are the mark of the beast, who knows. but that's beside the point.

So you use XML and annotations together?

yes, most def. I use XML whenever I need Spring's service abstraction (session factory, Quartz scheduler integration, ...) or special mechanisms such as property placeholder configurer, and for bootstrapping annotations (aop, tx, components).
For all other purposes, I use @Component, @Repository, @Autowired.

How many real-world, production-proven, annotation-based projects have you written or took part of?

How many real-world, production-proven, xml-based projects have you written or took part of?

Look,

1)
if you need to initialize spring beans from the files(like configuration files), Spring has some classes to make it:

a)
http://static.springframework.org/spring/docs/2.5.x/api/org/springframew...

b)
http://static.springframework.org/spring/docs/2.5.x/api/org/springframew...

2)
But if you just want to get and read your files from the classpath, use that:
Properties p = new Properties(); p.load(getClass().getClassLoader().getResourceAsStream("com/app/bla/my.properties"))

On Tomcat, and almost any other container you can use ServletContext.getRealPath or Class.getResource, like this:

URL url = com.imp.soap.sales.SalesService.class.getResource("/sales.wsdl");

To get ServletContext in a spring bean documentation suggests using

@Resource
ServletContext sc;

But I never tried it.