spring容器啟動(dòng)實(shí)現(xiàn)初始化某個(gè)方法(init)
spring容器啟動(dòng) 初始化某方法(init)
1、前言
很多時(shí)候,我們需要在項(xiàng)目啟動(dòng)的時(shí)候,就要完成某些方法的執(zhí)行。今天整理了一個(gè)簡(jiǎn)單的方法,使用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"><!-- 默認(rèn)懶加載(延遲加載):調(diào)用的時(shí)候才實(shí)例化 -->
<!-- 啟動(dòng)注解掃描包,獲取bean -->
<context:component-scan base-package="ws.spring.mybatis.service" />
<!-- 引入數(shù)據(jù)源 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 注入數(shù)據(jù)源 -->
<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>
<!-- 容器啟動(dòng)后執(zhí)行:需要執(zhí)行初始化方法,所以必須直接實(shí)例化,取消懶加載-->
<bean id="initData" class="ws.spring.mybatis.init.InitData" init-method="inits" lazy-init="false" />
</beans>
5、結(jié)果

6、注意事項(xiàng)
- 當(dāng)初始化方法中有依賴注入的時(shí)候,需要將加載注入的bean放在初始化bean之前。最好直接放在子容器中。因?yàn)楦溉萜飨扔谧尤萜鞒跏蓟?。否則依賴注入報(bào)錯(cuò)。
- 取消初始化bean的懶加載,否則初始化方法無(wú)法執(zhí)行。
- lazy-init 設(shè)置只對(duì)scop屬性為singleton的bean起作用
spring容器啟動(dòng)初始化的幾種方法
方法一
實(shí)現(xiàn)InitializingBean接口
InitializingBean接口為bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是繼承該接口的類,在初始化bean的時(shí)候都會(huì)執(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(">>>>>>>> 初始化...");
}
}
啟動(dòng)項(xiàng)目日志如下:
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)先級(jí)比上面兩個(gè)低。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot?將配置文件掛到?jar?包外面的操作方法
在 SpringBoot 中,可以將配置文件放在 jar 包外面,這樣可以方便地修改配置而不需要重新打包和部署,這篇文章主要介紹了SpringBoot?如何將配置文件掛到?jar?包外面,需要的朋友可以參考下2023-03-03
spring mvc4中相關(guān)注解的詳細(xì)講解教程
這篇文章主要給大家介紹了關(guān)于spring mvc4中相關(guān)注解的相關(guān)資料,其中詳細(xì)介紹了關(guān)于@Controller、@RequestMapping、@RathVariable、@RequestParam及@RequestBody等等注解的相關(guān)內(nèi)容,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-06-06
java創(chuàng)建jar包并被項(xiàng)目引用步驟詳解
這篇文章主要介紹了java創(chuàng)建jar包并被項(xiàng)目引用步驟詳解,jar包實(shí)現(xiàn)了特定功能的,java字節(jié)碼文件的壓縮包,更多相關(guān)內(nèi)容需要的朋友可以參考一下2022-07-07
深入理解Java虛擬機(jī)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
虛擬機(jī)是一種抽象化的計(jì)算機(jī),通過(guò)在實(shí)際的計(jì)算機(jī)上模擬各種計(jì)算機(jī)功能來(lái)實(shí)現(xiàn)的,下面通過(guò)本文給大家分享Java虛擬機(jī)相關(guān)知識(shí),感興趣的朋友一起看看吧2017-06-06
java 實(shí)現(xiàn)增量同步和自定義同步的操作
這篇文章主要介紹了java 實(shí)現(xiàn)增量同步和自定義同步的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
淺談ThreadLocal為什么會(huì)內(nèi)存泄漏
這篇文章主要介紹了淺談ThreadLocal為什么會(huì)內(nèi)存泄漏,每個(gè)Thread內(nèi)部維護(hù)著一個(gè)ThreadLocalMap,它是一個(gè)Map,這個(gè)映射表的Key是一個(gè)弱引用,其實(shí)就是ThreadLocal本身,Value是真正存的線程變量Object,需要的朋友可以參考下2023-12-12
基于SpringBoot應(yīng)用監(jiān)控Actuator安全隱患及解決方式
這篇文章主要介紹了SpringBoot應(yīng)用監(jiān)控Actuator安全隱患及解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07

