Use listener to start spring

problem:

  Object data to initialize the listener to inject spring container, you must first start spring container can use the Listener initialization data.

solve:

  Use listener to start spring frame

Problem: the Spring framework which parameters need to start?

1. The need to specify the location of the configuration file or configuration class

2. If you are using annotation configuration tired, need to modify the type of container

 

Question: listener does not support initialization parameters. How to pass parameters to the listener inside?

A: By re-setting the global context parameters inside, ContextLoaderListener listener is to solve this problem by obtaining a contextual parameter

< Context-param > 
      < param-name > the contextConfigLocation </ param-name > // Note that the front end of the corresponding spring controller <param-value> not write 
      < param-value > org.chu.config </ param-value > 
    </ context-param > 
    < context-param > 
       < param-name > the contextClass </ param-name > 
       < param-value > org.springframework.web.context.support.AnnotationConfigWebApplicationContext </ param-value > 
    </context-param>
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

Custom data initialization listeners

public class DataInitializeListener implements ServletContextListener  {
    
    // problem: Spring's dependency injection only objects in the Spring container use. The listener is not the object container Spring.
    // Solution: Object of non-container objects get inside the container, the container object .getBean ();

    /**
     * Context initialization method when called
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
    
     System.out.println ( "Start it == ==" );
     ServletContext context = sce.getServletContext();
     //ApplicationContext 
     WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
     RoleService roleService = applicationContext.getBean(RoleService.class);
     DictionaryService dictionaryService = applicationContext.getBean(DictionaryService.class);
     ModularService modularService=applicationContext.getBean(ModularService.class);
     PermissionService permissionService=applicationContext.getBean(PermissionService.class);
     UserService userService = applicationContext.getBean(UserService.class);
     AreaService areaService = applicationContext.getBean(AreaService.class);
     CustomerSerivce customerSerivce = applicationContext.getBean(CustomerSerivce.class);
     
    }

    /**
     * The method of destruction when the context calls
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println ( "closed == ==" );
        ServletContext context = sce.getServletContext();
        context.removeAttribute("global_roles");
        context.removeAttribute("global_dictionarys");
        
    }

}

 

Guess you like

Origin www.cnblogs.com/vieta/p/11142219.html