tags:

views:

38

answers:

3

Hi,

I am making heavy use of Springs XML Configuration for Java Beans. Is there a best practice to mark a property of a bean as optional or required? I am currently initializing the optional beans in the default constructor. If one of the required properties it not set in most cases a NullPointerException will be thrown, but that doesn't really seem to be a good solution.

Sincereley, Heinrich

+1  A: 

You can set bean lazy which are not frequently required , they will be initilized only when they actually needed.

Doc

org.life.java
+2  A: 

There is the @Required annotation that you can use on mandatory fields.

Bozho
+1  A: 

If you are not using AOP, then one way to deal with mandatory properties is to declare your bean class as implementing InitializingBean, and test that all mandatory properties have been set in the afterPropertiesSet() method.

Stephen C