Use of Spring Framework
Principles of use of the Spring framework
The Spring framework is used for its "lightweight container" features that instantiate declared beans 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. http://www.springframework.org
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. It is possible to overload the beans in the directory / WEB-INF / conf / override
|Plugin||/ WEB-INF / conf / plugins / < plugin > _context.xml||/ WEB-INF / conf / override / plugins / < plugin > _context.xml|
<? xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE PUBLIC Beans "- // SPRING // DTD BEAN // EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <Beans> <bean id = "myplugin.pollDAO" class = "en.paris.lutece.plugins.myplugin.business.PollDAO" /> <bean id = "myplugin.pollQuestionDAO" class = "en.paris.lutece.plugins.myplugin.business.PollQuestionDAO" /> <bean id = "myplugin.pollChoiceDAO" class = "en.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 implementation of the " pollDAO " bean declared in the XML file can be replaced by another without calling the code into question. This gives Lutece a great flexibility of evolution and customization.