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.
- Custom Thread Scope
- Spring custom scope implementation for thread scoped beans.
- Dynamic Tiles Spring MVC Module
- Have a default Tiles definition that takes the url of an incoming request and assumes it should be the request body and dynamically inserts the url into the default definition.
- Spring by Example JDBC
- Some nice utitlies for helping initialize scripts and to make it easier using HSQL DB. They are especially useful for unit tests.
- Spring by Example JCR Module
- Utilities for helping to recurse through JCR Nodes.
- Spring by Example Util's HttpClientTemplate
- Callback template around Commons HttpClient.
- Spring by Example Util's HttpClientOxmTemplate
- Callback template around Commons HttpClient that uses Spring OXM to marshall and unmarshall XML for HTTP requests.
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:
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
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?
I did a more advanced test and I'm seeing a problem too. I'll look into this more.
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.
Post a Comment