Spring IoC

I like spring framework – it is very flexible and provides a lot of useful mechanisms, but sometimes configuration of spring-base project is very annoying.

On one our project we mix xml-based configuration with annotation-based configuration. The rule is very simple for this: use annotations and if you need additional flexibility then use xml. Also we use xml for configuration standard mechanisms, e.g. transactions, security, acl, etc.

Unfortunately this simple approach doesn’t work correctly for all cases. 🙁 I found the following problematic cases:
* if use @Component for creation singleton – it doesn’t work as singleton. I tried a lot of variants of annotation-based configuration (with/without @Scope, with/without name, etc.) and finally I configured it via xml. And now it works fine.
* if use @Component for beans, where @Secured is used, then it is possible that security will not be checked (it works on some methods and doesn’t work on another). There I also started use xml-based configuration.
* if use @PostConstruct in @Repository bean then this method will be executed without transaction (at least for environment based on spring, hibernate and glassfish). Thus I recommend to create @PostConstruct method in @Service bean, which will call method of @Repository bean, which is marked as @Transactional.

It seams that is all – I’ll expand this list if I’ll find another configuration problems. I hope it will help somebody, because I spend a lot of time on analyzing and fixing these stupid problems.