spring初始化完成后执行初始化数据方法
2024-08-25 14:50:27
Spring提供的解决方案三种:
1.InitializingBean
package com.foriseland.fsoa.fabricca; import com.foriseland.fsoa.fabricca.service.IVerifyNumberService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; /**
* @version V1.0
* @Description
* @Author ZhangYue
* @Date 2018/4/12 11:58
*/
@Component
public class InitData2 implements InitializingBean {
@Autowired
private IVerifyNumberService iVerifyNumberService;
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware).
* <p>This method allows the bean instance to perform initialization only
* possible when all bean properties have been set and to throw an
* exception in the event of misconfiguration.
*
* @throws Exception in the event of misconfiguration (such
* as failure to set an essential property) or if initialization fails.
*/
@Override
public void afterPropertiesSet() throws Exception {
Integer number = iVerifyNumberService.findAllNumber();
System.out.println(number);
}
}
若采用XML来配置Bean的话,可以指定属性init-method。
2.ApplicationListener
package com.foriseland.fsoa.fabricca; import com.foriseland.fsoa.fabricca.service.IVerifyNumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component; /**
* @version V1.0
* @Description
* @Author ZhangYue
* @Date 2018/4/12 10:35
*/
@Component
public class InitData implements ApplicationListener<ContextRefreshedEvent> { @Autowired
private IVerifyNumberService iVerifyNumberService;
/**
* Handle an application event.
*
* @param event the event to respond to
*/
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
//保证在web容器中只执行一次该事件
if (event.getApplicationContext().getParent() == null) {
Integer number = iVerifyNumberService.findAllNumber();
System.out.println(number);
}
}
}
注意是监听的ContextRefreshedEvent事件。
在web 项目中(spring mvc),系统会存在两个容器,一个是root application context ,另一个就是我们自己的 projectName-servlet context(作为root application context的子容器)。这种情况下,就会造成onApplicationEvent方法被执行两次。为了避免上面提到的问题,我们可以只在root application context初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理。
event.getApplicationContext().getParent() == null
3.@PostConstruct
package com.foriseland.fsoa.fabricca; import com.foriseland.fsoa.fabricca.service.IVerifyNumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; /**
* @version V1.0
* @Description
* @Author ZhangYue
* @Date 2018/4/12 12:01
*/
@Component
public class InitData3 {
@Autowired
private IVerifyNumberService iVerifyNumberService; @PostConstruct
public void testInit(){
System.out.println(iVerifyNumberService.findAllNumber());
}
} 个人建议用第一种方法
最新文章
- 【数据结构】简单谈一谈二分法和二叉排序树BST查找的比较
- android 查看解压后的.xml文件代码(axmlprinter2)
- BZOJ 3224: Tyvj 1728 普通平衡树 treap
- UIKit的手风琴菜单,单条展开和多条同时展开
- 16. Linux 文件目录权限
- 使用DBUtils小框架
- Python_异常处理结构与调试
- 好程序员web前端分享HTML基本结构和基本语法
- delphi开源JWT
- [UE4]Tree View
- jmeter之最佳实践
- springboot 项目pom.xml文件基本配置
- centos6.x下安装maven
- gulp入门实践
- Spring MVC基本配置和实现(三)
- React事件初探
- log4cpp简单示例
- rpm包管理和源码包管理
- WEB APP 开发标签
- iOS静态库的制作与引用
热门文章
- Oracle 数据库管理员及管理员的作用
- TOJ 1836 Play on Words
- 设计模式之第7章-外观模式(Java实现)
- 从C语言的整数取值范围说开去
- a[i]==i[a]==*(i+a)==*(a+i)
- MySQL操作汇总
- 利用git工具命令简单的从github上拷贝和上传代码
- MySQL wait_timeout参数设置与网上常见错误小纠
- BAT的关于程序员的那些事
- .net EF框架 MySql实现实例