viernes, noviembre 03, 2006

After Spring, the Summer Comes

Hello, in blog "After Spring, the Summer Comes", I will summarize interfaces of Spring Core Framework for extending its funcionalities. I'm an active member in Spring Forum, so I have seen some questions about extending funcionalities in Spring or how to modify some behavior of Framework, for this reason I have decided to write down some extension points of Core.

  • First interface is (application/Bean)FactoryAware if your class implements previous interface, and also have an attribute of (application/BeanFactory) and a setter method, Spring will inject the current loaded class. (See 3.3.8 for more information). Moreover it is explained how to avoid the intrusion of Spring into our code.
  • Next interface MethodReplacer is used for changing implementation of a method. Spring will inject new implementation to existing class method. (See for an example).
  • Another interesting interface is org.springframework.beans.factory.config.Scope. Spring 2.0, implements five different scopes, prototype, singleton, request, session, global session. Of course, Spring offers the possibility of creating our scope by implementing Scope interface. In chapter 3.4.4, an example can be found.
  • Two interfaces are provided to control lifecycle of bean. These two interfaces are org.springframework.beans.factory.InitializingBean that are executed after properties of bean are setted, and org.springframework.beans.factory.DisposableBean that is called when container containing the bean is being destroyed. Of course it is better to use next approach, that is using init-method and destroy-method from Spring file, so no Spring dependencies are inserted into our code. This last option is preferred but these two exceptions also exists. (See Chapter 3.5.1).
  • Another interface is org.springframework.beans.factory.BeanNameAware. The BeanFactory will call the bean through this interface to inform the bean of the id it was deployed under. (See for a little description).
  • Next interface BeanPostProcessor is used if you want to do some custom logic after the Spring container has finished instantiating, configuring and otherwise initializing a bean, you can plug in one or more BeanPostProcessor implementations. (See Chapter 3.7.1).
  • The next extension point that we will look at is the org.springframework.beans.factory.config.BeanFactoryPostProcessor. BeanFactoryPostProcessors reads the configuration metadata and potentially change it before the container has actually instantied any other beans. Typical example is PropertyPlaceholderConfigurer, where values injected into a bean are changed for .properties file. (See Chapter 3.7.2 for full information).
  • Next very important interface is org.springframework.beans.factory.FactoryBean. This interface must be implemented by objects that are themselves factories, and you want to use Spring IoC. If you have some complex initialization code that is better expressed in Java as opposed to a (potentially) verbose amount of XML, you can create your own FactoryBean, write the complex initialization inside that class, and then plug your custom FactoryBean into the container. (See Chapter 3.7.3).
  • Spring has events, two interfaces must be implemented ApplicationContext and ApplicationEvent. Every time an ApplicationEvent gets published to the ApplicationContext, that bean will be notified.
  • Spring's Resource interface is meant to be a more capable interface for abstracting access to low-level resources. It is used in Spring and by Spring.
  • Spring's features a Validator interface that you can use to validate objects. The Validator interface works using an Errors object so that while validating, validators can report validation failures to the Errors object.
  • PropertyEditorSupport is a class not interface, but also it is used for extending Spring. PropertyEditors are used for converting String format to the complex type of a property. For example Spring provides LocaleEditor that converts an String value to a Locale class. (In chapter an example is provided).
  • The last class I explain for extending Spring is NamespaceHandler and interface BeanDefinitionParser. Both classes are used for extending basic XML Spring file. You can write your own custom XML bean definition parsers and integrating such parsers into the Spring IoC container. This approach is used by Spring 2.0 for transactions, utils, ... You can create your own definition as is explainde in Appendix B.

Now Summer has become. You have some interfaces to extend Spring Framework so it can adapt to your necessities.

Donate If You Can and Find Post Useful