`

spring启动自动加载数据

阅读更多

有时我们需要在项目启动时就要加载一些数据到缓存中(spring缓存或第三方缓存),spring在启动时加载数据有两种方式*(我所知道的,如果还有别的方式欢迎告知,嘿嘿):实现InitializingBean和实现BeanPostProcessor

第一种:实现InitializingBean

这种方式比较简单,写一个service实现InitializingBean并在spring配置文件中配置一下,启动时就会加载数据

public class InternationalServiceImpl implements InitializingBean{

	@Override
	public void afterPropertiesSet() throws Exception {
		
		System.err.println("重启开始加载数据。。。");
		
	}

}

 

配置文件:

<bean id = "internationalService" class="com.taotao.manage.service.InternationalServiceImpl"></bean>

 测试例子:

public class LoadTest {
	
	private PBSTrackManagerPostProcessor pbsTrackManagerPostProcessor;
	
	@Before
    public void setUp() throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/applicationContext.xml"});
        this.pbsTrackManagerPostProcessor = context.getBean(PBSTrackManagerPostProcessor.class);
    }
	
	@Test
    public void testQueryOrderByOrderNumber() {
        System.out.println("开始加载");
    }
}

 

结果:

重启开始加载数据。。。

 

第二种:实现BeanPostProcessor

数据层(这里写的比较简单):

public class Country implements Serializable{

	private static final long serialVersionUID = 4014709713611499131L;
	private String Province;
	private String city;
	public String getProvince() {
		return Province;
	}
	public void setProvince(String province) {
		Province = province;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	@Override
	public String toString() {
		return "Country [Province=" + Province + ", city=" + city + "]";
	}
	
	
}

 service层:

@Service
public class CountryServiceImpl implements CountryService{

	@Override
	public List<Country> getAllCountry() {
		List<Country> countries = new ArrayList<Country>();
		for(int i = 0;i <10;i++){
			Country country = new Country();
			country.setCity("北京"+i);
			country.setProvince("上海"+i);
			countries.add(country);
		}
		return countries;
	}

}

 再写一个service来调用这个service

public class StartOnLoadService {

	public static Map<String, List<Country>> map = new HashMap<String, List<Country>>();
	
	@Autowired
	private CountryService countryService;
	
	public void loadAllCountries(){
		
		List<Country> list = countryService.getAllCountry();
		
		map.put("countryMap", list);
		
		System.out.println(map);
	}
}

 然后写一个processor来加载这个service

public class PBSTrackManagerPostProcessor implements BeanPostProcessor{

	@Override
	public Object postProcessAfterInitialization(Object obj, String arg1)
			throws BeansException {
		
		if(obj instanceof StartOnLoadService){
			((StartOnLoadService)obj).loadAllCountries();
		}
		
		return obj;
	}

	@Override
	public Object postProcessBeforeInitialization(Object arg0, String arg1)
			throws BeansException {
		// TODO Auto-generated method stub
		return arg0;
	}

}

 最后写配置文件:

<bean id="startOnLoadService" class="com.taotao.manage.controller.StartOnLoadService"></bean>
	
<bean id="pbsTrackManagerPostProcessor" class="com.taotao.manage.controller.PBSTrackManagerPostProcessor">
</bean>

 测试例子:

public class LoadTest {
	
	private PBSTrackManagerPostProcessor pbsTrackManagerPostProcessor;
	
	@Before
    public void setUp() throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/applicationContext.xml"});
        this.pbsTrackManagerPostProcessor = context.getBean(PBSTrackManagerPostProcessor.class);
    }
	
	@Test
    public void testQueryOrderByOrderNumber() {
        System.out.println("开始加载");
    }
}

 结果:

{countryMap=[Country [Province=上海0, city=北京0], Country [Province=上海1, city=北京1], Country [Province=上海2, city=北京2],。。。}

我觉得那个countryService可以不用写,当时就那么写了。大家可以尝试下

新手,不足指出请指教!

 

 

分享到:
评论

相关推荐

    从零开始学Spring Boot

    1.26 Spring Boot启动加载数据CommandLineRunner 1.27 Spring Boot环境变量读取和属性对象的绑定 1.28 Spring Boot使用自定义的properties 1.29 改变自动扫描的包 1.30 Spring Boot Junit单元测试 1.31 SpringBoot...

    spring boot实战.pdf高清无水印

    3.1 覆盖Spring Boot自动配置 42 3.1.1 保护应用程序 43 3.1.2 创建自定义的安全配置 44 3.1.3 掀开自动配置的神 秘面纱 48 3.2 通过属性文件外置配置 49 3.2.1 自动配置微调 50 3.2.2 应用程序Bean的...

    Spring.3.x企业应用开发实战(完整版).part2

    4.11.2 使用基于Java类的配置信息启动Spring容器 4.12 不同配置方式比较 4.13 小结 第5章 Spring容器高级主题 5.1 Spring容器技术内幕 5.1.1 内部工作机制 5.1.2 BeanDefinition 5.1.3 InstantiationStrategy 5.1.4 ...

    Spring Boot实战 ,丁雪丰 (译者) 中文版

    3.1 覆盖Spring Boot自动配置 42 3.1.1 保护应用程序 43 3.1.2 创建自定义的安全配置 44 3.1.3 掀开自动配置的神秘面纱 48 3.2 通过属性文件外置配置 49 3.2.1 自动配置微调 50 3.2.2 应用程序...

    Spring攻略(第二版 中文高清版).part1

    10.3 为应用添加Spring BlazeDS支持 406 10.3.1 问题 406 10.3.2 解决方案 406 10.3.3 工作原理 406 10.4 通过BlazeDS/Spring暴露服务 411 10.4.1 问题 411 10.4.2 解决方案 411 10.4.3 工作原理 ...

    SpringBoot启动过程-mind版.md

    3. **执行自动配置:** Spring Boot通过自动配置机制根据项目的依赖和配置信息,自动配置应用中需要的各种组件,如数据源、Web服务器等。这减少了开发者的手动配置工作。 4. **扫描组件:** Spring Boot会扫描项目...

    Spring Boot中文文档.rar

    28.2.1.Spring WebFlux自动配置 28.2.2.带有HttpMessageReaders和HttpMessageWriters的HTTP编解码器 28.2.3.静态内容 28.2.4.模板引擎 28.2.5.错误处理 自定义错误页面 28.2.6.网络过滤器 28.3...

    Web服务启动时自动加载Servlet,并读取数据库内容

    我操作数据库用的是spring的HibernateTemplate和JdbcTemplate,访问数据库是以bean注入的方式,在action里注入service的实现,读取数据库数据,其实现在的关键就是得到这个service的实例化对象,

    Spring3.x企业应用开发实战(完整版) part1

    4.11.2 使用基于Java类的配置信息启动Spring容器 4.12 不同配置方式比较 4.13 小结 第5章 Spring容器高级主题 5.1 Spring容器技术内幕 5.1.1 内部工作机制 5.1.2 BeanDefinition 5.1.3 InstantiationStrategy 5.1.4 ...

    Spring攻略(第二版 中文高清版).part2

    10.3 为应用添加Spring BlazeDS支持 406 10.3.1 问题 406 10.3.2 解决方案 406 10.3.3 工作原理 406 10.4 通过BlazeDS/Spring暴露服务 411 10.4.1 问题 411 10.4.2 解决方案 411 10.4.3 工作原理 ...

    spring boot+mybatis整合

    4、在application.yml中添加数据源、Mybatis的实体和配置文件位置。 5、自动生成代码配置文件。 6、建立数据库和表 7、生产Dao层和entity类 8、建立controller层类 9、建立service层类 10、启动之后结果展示 -------...

    springboot知识点整理

    7 Spring Boot启动配置原理 128 7.1 启动流程(Springboot 1.50版本) 128 7.1.1 创建SpringApplication对象 129 7.1.2 运行run方法 130 7.1.3 编写事件监听机制 132 8 Spring Boot自定义starters 136 8.1 概述 136 ...

    基于 SpringBoot 多数据源 动态数据源 主从分离 快速启动器 支持分布式事务.zip

    3.自动配置:SpringBoot的自动配置特性利用了Spring对条件化配置的支持,合理地推测应用所需的bean并自动化配置他们。 4.使部署变得简单,SpringBoot内置了三种Servlet容器,Tomcat,Jetty,undertow.我们只需要一个...

    spring_MVC源码

    本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,现在这一篇补上。下面开始贴代码。 文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下。 先说...

    Myeclipse开发struts+hibernate+spring新手入门--环境配置---项目开发示例

    同加载struts方式类似,选择Myeclipse后在弹出的二级菜单中选择Add spring in action in action Capabilities…后提示如下图所示: 选择所需要的spring in action in action包,然后默认下一步,直到完成。 到此...

    尚硅谷Spring boot核心技术篇(上)

    入门-springboot-helloworld 06、尚硅谷_SpringBoot_入门-HelloWorld细节-场景启动器(starter) 07、尚硅谷_SpringBoot_入门-HelloWorld细节-自动配置 08、尚硅谷_SpringBoot_入门-使用向导快速创建Spring Boot应用...

    Bauta:Bauta是基于Spring Batch和Spring Boot的批处理自动化框架。 它添加了一个用户界面,您可以在其中启动停止重新启动作业,监视步进作业状态。 还提供了一些作业和步骤的附加组件,例如报告日志生成,作业参数处理等

    它添加了一个用户界面,您可以在其中启动/停止/重新启动作业,监视步骤/作业状态。 还提供了一些作业和步骤的附件,例如报告/日志生成,作业参数处理等。 您可以在个人计算机或服务器上本地运行作业以支持团队协作...

    Spring面试题

    FactoryBean 接口为使用 Spring 框架构建的应用程序添加了一个间接的级别。 IOC 示例 理解控制反转最简单的方式就是看它的实际应用。在对由三部分组成的 Spring 系列 的第 1 部分进行总结时,我使用了一个示例,...

    springboot参考指南

    1. 介紹 2. I.... 在启动时执行Spring Batch作业 ix. 70. 执行器(Actuator) i. 70.1. 改变HTTP端口或执行器端点的地址 ii. 70.2. 自定义'白标'(whitelabel,可以了解下相关理念)错误页面 x. 71...

    ssh(structs,spring,hibernate)框架中的上传下载

    这是Hibernate3引入的新特性,对于包含重量级大数据的表字段,这种抽取方式提高了对大字段操作的灵活性,否则加载Tfile对象的结果集时如果总是返回fileContent,这种批量的数据抽取将可以引起数据库的"洪泛效应"。...

Global site tag (gtag.js) - Google Analytics