spring容器啟動實現(xiàn)初始化某個方法(init)
spring容器啟動 初始化某方法(init)
1、前言
很多時候,我們需要在項目啟動的時候,就要完成某些方法的執(zhí)行。今天整理了一個簡單的方法,使用spring容器中bean的屬性:init-method
2、代碼
/* 初始化的類。這里不需要添加任何注解 */ public class InitData { @Autowired private UserService userService; /* 初始化方法 */ public void inits(){ System.out.println("初始化方法執(zhí)行....."); List<User> userList = userService.queryAllUser(); System.out.println(userList.toString()); } }
3、配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" default-lazy-init="true"><!-- 默認懶加載(延遲加載):調用的時候才實例化 --> <!-- 啟動注解掃描包,獲取bean --> <context:component-scan base-package="ws.spring.mybatis.service" /> <!-- 引入數據源 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 注入數據源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 容器啟動后執(zhí)行:需要執(zhí)行初始化方法,所以必須直接實例化,取消懶加載--> <bean id="initData" class="ws.spring.mybatis.init.InitData" init-method="inits" lazy-init="false" /> </beans>
5、結果
6、注意事項
- 當初始化方法中有依賴注入的時候,需要將加載注入的bean放在初始化bean之前。最好直接放在子容器中。因為父容器先于子容器初始化。否則依賴注入報錯。
- 取消初始化bean的懶加載,否則初始化方法無法執(zhí)行。
- lazy-init 設置只對scop屬性為singleton的bean起作用
spring容器啟動初始化的幾種方法
方法一
實現(xiàn)InitializingBean接口
InitializingBean接口為bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是繼承該接口的類,在初始化bean的時候都會執(zhí)行該方法。
xml中添加bean
不在xml中添加可以在Initializing類頭部添加注解@Service
<bean id="initializingBean" class="cn.base.core.init.Initializing" init-method="init"></bean>
Initializing.java
public class Initializing implements InitializingBean{ private static final Logger logger = LoggerFactory.getLogger(Initializing.class); // 方法一 @Override public void afterPropertiesSet() throws Exception { logger.info(">>>>>>>> init start..."); } public void init() { logger.info(">>>>>>>> 初始化..."); } }
啟動項目日志如下:
Invoking afterPropertiesSet() on bean with name 'initializingBean'
>>>>>>>> init start...
Invoking init method 'init' on bean with name 'initializingBean'
>>>>>>>> 初始化...
方法二
使用@PostConstruct注解
@PostConstruct public void initConstruct() { System.out.println("注解初始化..."); }
注意:此方法所在的類需要被掃描到,多種注解可以用,推薦使用@Service
執(zhí)行順序:方法二比方法一優(yōu)先執(zhí)行。
方法三
web.xml配置
<filter> <filter-name>filterServlet</filter-name> <filter-class>cn.base.web.interceptor.FilterServlet</filter-class> </filter> <filter-mapping> <filter-name>filterServlet</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping>
FilterServlet.java
public class FilterServlet implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("FilterServlet 初始化"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { chain.doFilter(request, response); } @Override public void destroy() { } }
執(zhí)行順序:優(yōu)先級比上面兩個低。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringSecurity添加圖形驗證碼認證實現(xiàn)
本文主要介紹了SpringSecurity添加圖形驗證碼認證實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08Java集合排序規(guī)則接口Comparator用法解析
這篇文章主要介紹了Java集合排序規(guī)則接口Comparator用法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09Mybatis之association和collection用法
這篇文章主要介紹了Mybatis之association和collection用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02SSH框架網上商城項目第28戰(zhàn)之使用Ajax技術局部更新商品數量和總價
這篇文章主要為大家詳細介紹了SSH框架網上商城項目第28戰(zhàn)之使用Ajax技術局部更新商品數量和總價,感興趣的小伙伴們可以參考一下2016-06-06