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

spring初始化完成后执行初始化数据方法的更多相关文章

  1. 【Spring实战】Spring容器初始化完成后执行初始化数据方法

    一.背景知识及需求 在做WEB项目时,经常在项目第一次启动时利用WEB容器的监听.Servlet加载初始化等切入点为数据库准备数据,这些初始化数据是系统开始运行前必须的数据,例如权限组.系统选项.默认 ...

  2. spring boot 启动后执行初始化方法

    http://blog.csdn.net/catoop/article/details/50501710 1.创建实现接口 CommandLineRunner 的类 package org.sprin ...

  3. 当spring 容器初始化完成后执行某个方法

    在做web项目开发中,尤其是企业级应用开发的时候,往往会在工程启动的时候做许多的前置检查. 比如检查是否使用了我们组禁止使用的Mysql的group_concat函数,如果使用了项目就不能启动,并指出 ...

  4. 当spring 容器初始化完成后执行某个方法 防止onApplicationEvent方法被执行两次

    在做web项目开发中,尤其是企业级应用开发的时候,往往会在工程启动的时候做许多的前置检查. 比如检查是否使用了我们组禁止使用的Mysql的group_concat函数,如果使用了项目就不能启动,并指出 ...

  5. spring bean容器加载后执行初始化处理@PostConstruct

    先说业务场景,我在系统启动后想要维护一个List常驻内存,因为我可能经常需要查询它,但它很少更新,而且数据量不大,明显符合缓存的特质,但我又不像引入第三方缓存.现在的问题是,该List的内容是从数据库 ...

  6. 当springMVC 容器初始化完成后执行某个方法

    分类: spring java2013-06-19 16:40 8289人阅读 评论(4) 收藏 举报 在某些应用中,我们希望,当spring 容器将所有的bean都初始化完成后,做一个操作(例如:将 ...

  7. 【hibernate spring data jpa】执行了save()方法 sql语句也执行了,但是数据并未插入数据库中

    执行了save()方法  sql语句也执行了,但是数据并未插入数据库中 解决方法: 是因为执行了save()方法,也执行了sql语句,但是因为使用的是 @Transactional 注解,不是手动去提 ...

  8. angular指令监听ng-repeat渲染完成后执行自定义事件方法

    今天工作中遇到需要用到ng-repeat遍历渲染完后执行某个操作,angular本身并没有提供监听ng-repeat渲染完成的指令,所以需要自己创建自定义指令. 在ng-repeat模板实例内部会暴露 ...

  9. Spring 为Bean对象执行初始化和销毁方法

    1)初始化: ①可以利用<bean>元素的init-method="方法名"属性指定初始化方法. ②指定的初始化方法是在构造方法调用后自动执行.若非单例模式,则每创建一 ...

随机推荐

  1. 如何在cmd窗口里快速且正确打开任意位置路径(各版本windows系统都适合)(图文详解)(博主推荐)

    问题的由来 有时候,我们很苦恼,总是先系统键 + R,然后再去手动敲.尤其对win7系统比较麻烦 解决办法 方法一:复制路径(这点对win10系统做得好,直接可以复制) ,win7系统的话可能还需要设 ...

  2. WPF的布局--StackPanel

    1. StackPanel是以堆叠的方式来显示控件(从左到右,或者从上到下) 默认是从上到下显示的,并且宽度为StackPanel的宽度,高度自动适应控件中内容的高度(未对控件进行设置时) 如图: 代 ...

  3. 九度oj题目1555:重复子串

    题目1555:重复子串 时间限制:3 秒 内存限制:256 兆 特殊判题:否 提交:738 解决:125 题目描述: 给定一个由小写字母组成的字符串,求它的所有连续子串中,出现过至少两次,且至少有一对 ...

  4. CentOS6.5安装Mysql数据库

    一.卸载原有mysql    # rpm -e --nodeps mysql 二.安装mysql    # yum install mysql-server mysql mysql-devel 三.查 ...

  5. touch-slide-image

    用htmls5+css3实现的在android和ios,以及wekit新版浏览器上实现手指滑动切换图片. https://github.com/navyxie/touch-slide-image

  6. PostgreSQL PARTITION 分区表

    PostgreSQL 分区表,操作性相当便捷. 但只能在创建时决定是否为分区表,并决定分区条件字段,普通表创建后,不能在修改为分区表. Note:通过其他方法也可转化为分区表. 和其他数据库一样,分区 ...

  7. WP手机短信导出方法和MSG格式文件阅读器的实现

    最近想起来自己一直扔在抽屉里的Nokia920T里还存着珍贵的短信,觉得把它导出来存到电脑上比较稳妥也方便阅读.经过搜索找到一下方法:到应用市场里搜索contacts+message backup,安 ...

  8. C#项目””是针对”.NETFramework,Version=v4.5.2”但此计算机没有,需要修改为v4.5.1.

    每次下载别人的代码都会出现这样的问题,以为是没有安装.NETFramework,就下载安装了,但是每次安装都会出现已安装高版本的4.6(Win10自带),无需下次安装,但是每次VS中都会显示有问题,而 ...

  9. linux环境的基本搭建

    1.准备Linux环境(我的是centos系统) 如果你是hadoop用户在使用sudo之前需要配置一下:获取sudo权限 切换到root vi /etc/sudoersroot ALL=(ALL) ...

  10. [shell]管理 Sphinx 启动|停止|重新生成索引的脚本

    对于启动sphinx的服务,可以直接输入如下命令 /usr/bin/searchd -c /etc/sphinx/sphinx.conf <!-- /usr/local/bin/searchd  ...