Saturday, October 11, 2008

Spring by Example

Spring by Example was started at the beginning of 2008 and now has over 25 examples. I've been writing about it in my blog Spring, Java, and other Fun Stuff. I decided to start a separate blog to link to from examples to have any user discussions take place here, and use my blog to talk more about anything I've been doing. Below is the original announcement on my blog from March 7th, 2008 with the links updated to point to the new static format instead of to the TWiki.

The Spring by Example site is finally open! I've been working this for a couple of months, but wanted it to be fairly nice when it went live. It has a few good examples and I have more to post. A few people I know have said they will contribute examples as they have time also. These are some of my favorite examples posted so far.


Tim Voet also posted an example using a BeanPostProcessor to dynamically insert a logger based on the bean's class if it has a setLogger method. This is a nice way to avoid typos when creating loggers.

4 comments:

Unknown said...

Hi, I'm trying to figure out how to use the AnnotationLoggerBeanPostProcessor in my application and I'm not doing something right.

I have added this to my app context:

&#lt;bean class="org.springbyexample.util.log.AnnotationLoggerBeanPostProcessor"/&#gt;

Then, in my Java class I imported the annotation:

import org.springbyexample.util.log.AutowiredLogger;
import org.apache.log4j.Logger;

public class MyClass {

@AutowiredLogger
final Logger logger = null;

...
}

I'm getting a NullPointerException when the logger is dereferenced in one of my methods. What am I missing in my configuration to make this work?

I read thru the doc on your springbyexample site, and it's not clear to me what I am missing. Your help is most appreciated. Thanks.

Oh, I'm using version 1.2.1 of spring-by-example-util

David Winterfeldt said...

I just want to double check, but your class is Spring managed? I don't see an annotation on it, so I assume it's defined in your XML config along with the AnnotationLoggerBeanPostProcessor. I'm not sure what the problem would be. I just double checked that the unit test was working and it seems fine ('mvn -Dtest=AnnotationLoggerBeanPostProcessorTest test'). You could try checking out the project ('svn co http://svn.springbyexample.org/modules/spring-by-example-util/tags/1.2.1/ sbe-util') and run the unit test yourself just to confirm it isn't your environment. You don't see anything in your logging indicating a problem, you just get a NullPointerException?

David Winterfeldt said...

I did a more advanced test and I'm seeing a problem too. I'll look into this more.

David Winterfeldt said...

The issue I was having is that @PostConstruct for initialization is running before the BeanPostProcessors. If I switched to use InitializingBean I could see the post processor running. This is with Spring 2.5.6. Are you doing any initialization? Otherwise it seems to be working fine for me. This issue seems like a bug. The initialization event is supposed to run after the BeanPostProcessor's postProcessBeforeInitialization method. Maybe it's fixed in Spring 3.0. I'll check when I have time.