其執行順序爲:java
4===>5===>1===>2===>3web
即指定init-method的Bean開始執行app
接着實現Spring的Bean後置處理器開始執行ide
而後是Servlet的監聽器執行post
再接下來是Servlet的過濾器執行url
最後纔是Servlet執行spa
一、實現Servlet監聽器接口ServletContextListener.net
public class InitListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent context) { } @Override public void contextInitialized(ServletContextEvent context) { // 上下文初始化執行 System.out.println("================>[ServletContextListener]自動加載啓動開始."); SpringUtil.getInstance().setContext( <span style="white-space:pre;"> </span>WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext()) <span style="white-space:pre;"> </span>); } }
而後在web.xml文件配置該監聽器
<listener> <listener-class>com.test.init.InitListener</listener-class> </listener>
二、實現Servlet的過濾器Filterxml
public class InitFilter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { } @Override public void init(FilterConfig config) throws ServletException { System.out.println("================>[Filter]自動加載啓動開始."); // 讀取Spring容器中的Bean[此時Bean已加載,能夠使用] //寫啓動須要執行的代碼 System.out.println("================>[Filter]自動加載啓動結束."); } }
而後在web.xml文件配置過濾器便可
<filter> <filter-name>InitFilter</filter-name> <filter-class>com.test.init.InitFilter</filter-class> </filter> <filter-mapping> <filter-name>InitFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping>
三、編寫一個Servlet,在web.xml裏面配置容器啓動後執行便可
public class InitServlet extends HttpServlet { /** */ private static final long serialVersionUID = 1L; @Override public void init(ServletConfig config) { try { super.init(); } catch (ServletException e) { e.printStackTrace(); } System.out.println("================>[Servlet]自動加載啓動開始."); // 讀取Spring容器中的Bean[此時Bean已加載,能夠使用] //執行想要的代碼 System.out.println("================>[Servlet]自動加載啓動結束."); } }
而後在web.xml文件配置該Servlet的啓動方式爲:容器啓動後執行
<servlet> <servlet-name>InitServlet</servlet-name> <servlet-class>com.test.init.InitServlet</servlet-class> <init-param> <param-name>username</param-name> <param-value>test</param-value> </init-param> <!-- 此處指定加載順序爲2,代表還有優先級更高的Servlet要先執行 --> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>InitServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
關於啓動後執行,由load-on-startup指定:
(1)當值爲0或者大於0時,表示容器在應用啓動時就加載這個servlet。值越小,啓動優先級越高;
(2)當是一個負數時或者沒有指定時,表示該servlet被調用時才加載。
<!-- service --> <bean id="shopService" class="com.test.teach.service.ShopService" <span style="color:#33ffff;">init-method="start"</span>> <property name="shopDao" ref="shopDao" /> </bean>
五、若是你使用Spring IOC做爲Bean管理容器,還能夠實現Spring的Bean後置處理器接口:BeanFactoryPostProcessor表示:該Bean加載完成以後,Spring能夠讓開發者自定義一些事件
public class KeyWordInit implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException { System.out.println("================>[BeanFactoryPostProcessor]自動加載啓動開始."); ShopService shopService = factory.getBean("shopService", ShopService.class); List<Map<String, Object>> shopList = shopService.findAllShop(); System.out.println("================>" + shopList); System.out.println("================>[BeanFactoryPostProcessor]自動加載啓動結束."); } }