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());
}
} 个人建议用第一种方法
 

最新文章

  1. 【数据结构】简单谈一谈二分法和二叉排序树BST查找的比较
  2. android 查看解压后的.xml文件代码(axmlprinter2)
  3. BZOJ 3224: Tyvj 1728 普通平衡树 treap
  4. UIKit的手风琴菜单,单条展开和多条同时展开
  5. 16. Linux 文件目录权限
  6. 使用DBUtils小框架
  7. Python_异常处理结构与调试
  8. 好程序员web前端分享HTML基本结构和基本语法
  9. delphi开源JWT
  10. [UE4]Tree View
  11. jmeter之最佳实践
  12. springboot 项目pom.xml文件基本配置
  13. centos6.x下安装maven
  14. gulp入门实践
  15. Spring MVC基本配置和实现(三)
  16. React事件初探
  17. log4cpp简单示例
  18. rpm包管理和源码包管理
  19. WEB APP 开发标签
  20. iOS静态库的制作与引用

热门文章

  1. Oracle 数据库管理员及管理员的作用
  2. TOJ 1836 Play on Words
  3. 设计模式之第7章-外观模式(Java实现)
  4. 从C语言的整数取值范围说开去
  5. a[i]==i[a]==*(i+a)==*(a+i)
  6. MySQL操作汇总
  7. 利用git工具命令简单的从github上拷贝和上传代码
  8. MySQL wait_timeout参数设置与网上常见错误小纠
  9. BAT的关于程序员的那些事
  10. .net EF框架 MySql实现实例