views:

279

answers:

3

I have an ApplicationContext.xml file with the following node:

<context:property-placeholder 
location="classpath:hibernate.properties, classpath:pathConfiguration.properties" />

It specifies that both properties files will be used by my application.

Inside pathConfiguration.properties, some paths are defined, such as:

PATH_ERROR=/xxx/yyy/error
PATH_SUCCESS=/xxx/yyy/success

A PathConfiguration bean has setters for each path.

The problem is: when some of those mandatory paths are not defined, no error is thrown. How and where should I handle this problem?

A: 

I'm not sure if I fully understand your issue, but there are probably a variety of ways to approach this. One would be to make the paths mandatory by using constructor injection. In the constructor you could then validate the incoming values and if null for example, throw BeanInitializationException instances.

Derek Clarkson
I think you understood the problem :-) In fact I'm completely new to Spring and cannot figure out how to implement constructor injection. I'm going to read some about it and if this is the solution I'll be back here to give +1 you. (If you can update your answer with an example, it would be great)
Paulo Guedes
... think I found the solution here: http://static.springsource.org/spring/docs/2.0.3/reference/beans.html#beans-factory-properties-detailed (item 3.5.1.2.1). Is that what you meant?
Paulo Guedes
Yep, thats what I was thinking of. I don't have an example on me. But the thing to note is that the benefit of constructor inject is that you can ensure that the required parameters are passed in. But remember that doesn't guarantee they are correct. You may still want to validate the property values inside the constructor and throw a BeanInitializationException if they are not correct.
Derek Clarkson
+4  A: 

The standard behaviour of the PropertyPlaceholder that is configured via <context:property-placeholder ... /> throws an exception when a property cannot be resolved once it is required in some place as long as you do not configure it otherwise.

For your case if you have a Bean that requires some properties like this, it will fail when the value cannot be resolved. For example like this:

public class PropertiesAwareBean {

  @Value("${PATH_ERROR}")
  private String errorPath;

  String getErrorPath() {
    return errorPath;
  }

}

If you want to relax the PropertyPlaceholder and don't make it throw an Exception when a property cannot be resolved you can configure the PropertyPlaceholder to ignore unresolvable properties like this <context:property-placeholder ignore-unresolvable="true" ... />.

codescape
+1  A: 

One way to reinforce the verification of parameters is to switch to a classical PropertyPlaceholderConfigurer bean in your beans file.

The PropertyPlaceholderConfigurer has properties which you can use to tweak its behavior and specify either an exception is thrown or not if some key is missing (take a look at setIgnoreUnresolvablePlaceholders or setIgnoreResourceNotFound).

If I remember correctly, in Spring 2.5, only the location attribute is supported for <context:property-placeholder> (things might have changed though).

dpb
You are correct for Spring 2.5 there is just a `location` and a `properties-ref` attribute. Spring 3.0 offers more configuration possibilities on the `<context:property-placeholder />`-Tag as mentioned in my answer.
codescape