spring容器启动 初始化某方法(init)
1、前言
很多时候,我们需要在项目启动的时候,就要完成某些方法的执行。今天整理了一个简单的方法,使用spring容器中bean的属性:init-method
2、代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/*
初始化的类。这里不需要添加任何注解
*/
public class InitData {
@Autowired
private UserService userService;
/*
初始化方法
*/
public void inits(){
System.out.println( "初始化方法执行....." );
List<User> userList = userService.queryAllUser();
System.out.println(userList.toString());
}
}
|
3、配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<? 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 >
<!-- 容器启动后执行:需要执行初始化方法,所以必须直接实例化,取消懒加载-->
< bean id = "initData" class = "ws.spring.mybatis.init.InitData" init-method = "inits" lazy-init = "false" />
</ beans >
|
5、结果
6、注意事项
- 当初始化方法中有依赖注入的时候,需要将加载注入的bean放在初始化bean之前。最好直接放在子容器中。因为父容器先于子容器初始化。否则依赖注入报错。
- 取消初始化bean的懒加载,否则初始化方法无法执行。
- lazy-init 设置只对scop属性为singleton的bean起作用
spring容器启动初始化的几种方法
方法一
实现InitializingBean接口
InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法。
xml中添加bean
不在xml中添加可以在Initializing类头部添加注解@Service
1
|
< bean id = "initializingBean" class = "cn.base.core.init.Initializing" init-method = "init" ></ bean >
|
Initializing.java
1
2
3
4
5
6
7
8
9
10
11
12
|
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注解
1
2
3
4
|
@PostConstruct
public void initConstruct() {
System.out.println( "注解初始化..." );
}
|
注意:此方法所在的类需要被扫描到,多种注解可以用,推荐使用@Service
执行顺序:方法二比方法一优先执行。
方法三
web.xml配置
1
2
3
4
5
6
7
8
9
|
< 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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() {
}
}
|
执行顺序:优先级比上面两个低。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/static_coder/article/details/78226787