Saturday, August 13, 2011

Spring Good Practices

These are some of the good practices while using spring framework.

Spring IOC
  • Select the factory carefully. If you are using basic IOC feature go for BeanFactory in place of ApplicationContext.
  • Do not overuse the dependency injection. Spring is good for wiring configurations and services.
  • Use type in constructor injection, as it removes ambiguities.
  • Avoid wiring your domain model object. Another good parameter is if you are wiring too many objects with prototype scope rethink what you are using spring for. Spring is not a good place to keep your preferences and default value for objects to be populated.
  • Divide your configuration details in different XML's This helps in maintainability. In web application we have two context already available, follow that paradigm. Put your spring security configuration in other XML's.
  • Use good names for naming beans. Preferable use alias and wire the beans with interface name. e.g you have ExampleDao having implementation ExampleJDBCDao and ExampleHibernateDao. Register it like this:
- if you are using hibernate dao

<alias name="exampleHibernateDao" alias="exampleDao">
<bean id="exampleHibernateDao" class="ExampleHibernateDao.">
<bean id="exampleJDBCDao" class="ExampleJDBCDao">

This helps in building indirection in XML. Program against interface bean names.
  • Use autowiring, they reduce the configuration.
  • Use dependency checking wherever it makes sense.
  • Adopt annotations and Spring 2.5 (of course Javaa 5+ ) as soon as possible :-)

Spring AOP

Use AOP pointcut sensibly. Try to narrow down as far as possible. AOP has runtime performance implication as they introduce a level of indirection.

Spring Services

Use caching of lookups through jndi if your jndi lookup is going to return the object which will not change in the lifetime of your program.

No comments:

Post a Comment