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. intellij idea NoClassDefFoundError javax.swing.UIManager

    今天启动idea报 NoClassDefFoundError javax.swing.UIManager 可是明明配置好了java 环境 ,后来仔细想了一下只配置了java的bin目录在PATH里 随 ...

  2. C 标准库 - string.h

    C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...

  3. Java入门系列-14-深入类和对象

    这篇文章用大量的代码帮你搞懂:值传递和引用传递.构造方法.方法重载.static关键字的使用 方法参数传递-值传递和引用传递 1.值传递 敲一敲: /** * 使用方法交换两个变量的值 * @auth ...

  4. wcf 基本配置

    <system.serviceModel> <services> <service name="ServiceUpdater.ServiceUpdate&quo ...

  5. c#中的程序集

     程序集:一些相关类的包,比如三层中Model层都是一些数据库表的实体类.我们所用到的类都是位于各个程序集中,若需要调用某个类,就必须引用其所在的程序集. 访问级别:程序集中的类有四种访问级别,int ...

  6. T-SQL 备份和还原数据库

    --完整备份  Backup   Database   db_database  To disk='D:\Backup\db_database_Full.bak'   --差异备份  Backup   ...

  7. JavaScript中 运算符

    运算符对一个或多个变量或值(操作数)进行运算,并返回一个新值 根据所执行的运算,运算符可分为以下类别: (1) 算术运算符 (2) 比较运算符 运算符 说 明 示 例 ==  等于. 如果两个操作数相 ...

  8. The Internet Communications Engine (Ice) 跨平台异构通讯方案 第一弹-ICE简介

    .net中的通讯方案很多,从.net Remoting,MSMQ,Webservice,WSE,WCF等等,他们都有一个特点,易用,但是都不能跨语种异构,如果你服务端要用java开发,客户端用C#开发 ...

  9. Node.js学习笔记(四) --- fs模块的使用

    目录 . fs.stat 检测是文件还是目录 . fs.mkdir 创建目录 . fs.writeFile 创建写入文件 . fs.appendFile 追加文件 . fs.readFile 读取文件 ...

  10. MySql 双实例安装

    1.官网下载压缩包 https://dev.mysql.com/downloads/mysql/ 2.解压到D盘 3.修改my.ini 文件 [mysql] # 设置mysql客户端默认字符集 def ...