Use of Spring Framework
Principles of use of the Spring framework
The Spring framework is used for its "lightweight container" features that instantiate beans declared in an XML file. This device also called BeanFactory offers many advantages :
- The code only knows the name of the bean and its interface. It is therefore possible to very easily modify an implementation by another without modifying the calling code.
- The objects are instantiated by Spring and run in a container which allows to add, declaratively and without modification of code, behaviors managed by the container (ex: transactional support, "interceptors"...).
- The mode of instantiation of the beans implements the design pattern Inversion of Control, also designated by the symbol IoC. This gives an object the responsibility to load and check its dependencies. This mechanism allows to do in particular the injection of dependencies.
For more information on these topics, see Spring's documention.
The implementation of Spring in Lutece is based on the SpringContextService service which aims to provide objects defined in a given context (in the Spring sense of the term).
Lutece provides a common context for the core and plugins. The context XML file describing the beans must respect the rules location and name to be automatically loaded by the SpringContextService service.
Info It is possible to overload the beans in the directory /WEB-INF/conf/override.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="myplugin.pollDAO" class="fr.paris.lutece.plugins.myplugin.business.PollDAO" /> <bean id="myplugin.pollQuestionDAO" class="fr.paris.lutece.plugins.myplugin.business.PollQuestionDAO" /> <bean id="myplugin.pollChoiceDAO" class="fr.paris.lutece.plugins.myplugin.business.PollChoiceDAO" /> </beans>
The instantiation of the beans in the code is done using the method getBean().
// Instantiation via Spring of a DAO object declared in the myplugin_context.xml file private static IPollDAO _dao = (IPollDAO) SpringContextService.getBean( "myplugin.pollDAO" );
The myplugin.pollDAO bean can be declared in the XML file with another implementation of IPollDAO without compromising the code. This then allows the Lutece code to have a very great flexibility of evolution and personalization.