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. Python数据类型(列表)

    文章内容参考了教程:http://www.runoob.com/python/python-basic-syntax.html#commentform Python 列表(List) 序列是Pytho ...

  2. ArrayList、Vector和LinkedList等的差别与用法(基础回顾)

    ArrayList 和Vector是采取数组体式格式存储数据,此数组元素数大于实际存储的数据以便增长和插入元素,都容许直接序号索引元素,然则插入数据要设计到数组元素移动等内存操纵,所以索引数据快插入数 ...

  3. C++析构函数(转)

    创建对象时系统会自动调用构造函数进行初始化工作,同样,销毁对象时系统也会自动调用一个函数来进行清理工作(例如回收创建对象时消耗的各种资源),这个函数被称为析构函数. 析构函数(Destructor)也 ...

  4. 【linux】虚拟机内装Linux系统的ssh访问

    一般在虚拟机内安装一个Linux系统,虚拟机网络设置为桥接后,Linux系统会在安装的过程中自动设置其为dhcp配置,会给其随机分配一个ip,这个ip可以用命令 "ifconfig" ...

  5. Wamp设置虚拟目录

    1. 默认安装 wamp后,工作目录为"..../wamp/www" 也就是PHP文件只有放在此目录下才能打得开,打开Apache的配置文件httpd.conf可以看到: 这么两行 ...

  6. Python基础(2) - 动态数据类型

    Python是一门强类型语言,单定义变量时不需要制定类型. C#这样定义变量: ; VB这样定义变量: Python不需要制定类型,给变量赋什么类型的值,它就是什么类型.(穿神马就是神马?) > ...

  7. Java入门系列-22-IO流

    File类的使用 Java程序如何访问文件?通过 java.io.File 类 使用File类需要先创建文件对象 File file=new File(String pathname);,创建时在构造 ...

  8. webservice log4net日志写入失败

    原因1:如果webservice和调用者都部署在一台机器上,日志有可能写到了项目所在目录中,虽然你添加的服务引用是部署在iis下的,但不会写到这.暂时解决办法,把webservice部署到内网服务器上 ...

  9. 关于responseHeader的一些基础设置

    1.关于响应头的一些基础设置 //设置相应头 response.addHeader("name","zhangsan"); response.addIntHea ...

  10. 中南oj 1216: 异或最大值 数据结构

    1216: 异或最大值 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 98  Solved: 29 [Submit][Status][Web Boar ...