Spring Boot实战笔记(三)-- Spring常用配置(Bean的初始化和销毁、Profile)
一、Bean的初始化和销毁
在我们的实际开发的时候,经常会遇到Bean在使用之前或之后做些必要的操作,Spring对Bean的生命周期操作提供了支持。在使用Java配置和注解配置下提供如下两种方式:
(1)Java配置的方式:使用 @Bean 的 initMethod 和 destroyMethod(相当于xml配置中的 init-method 和 destroy-method)。
(2)注解方式:利用JSR-250的 @PostContruct 和 @PreDestroy。
演示:
1.增加 JSR-250 支持。
<!-- JSR-250 支持 -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
2.使用 @Bean 形式的Bean。
package com.ecworking.bean; public class BeanWayService { public void init(){
System.out.println("@Bean-init-method");
} public BeanWayService() {
super();
System.out.println("初始化构造函数-BeanWayService"); } private void destroy(){
System.out.println("@Bean-destroy-method");
}
}
3.使用JSR250形式的Bean。
package com.ecworking.bean; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; public class JSR250WayService { @PostConstruct // 在构造函数执行完之后执行
public void init(){
System.out.println("@JSR250-init-method");
} public JSR250WayService() {
super();
System.out.println("初始化构造函数-JSR250WayService");
} @PreDestroy // 在Bean销毁之前执行
private void destroy(){
System.out.println("@JSR250-destroy-method");
}
}
4.配置类。
package com.ecworking.bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan("com.ecworking.bean")
public class PrePostConfig { // initMethod 和 destroyMethod 指定BeanWayService类的 init 和 destroy 方法在构造之后、Bean销毁之前执行
@Bean(initMethod = "init", destroyMethod = "destroy")
BeanWayService beanWayService(){
return new BeanWayService();
} @Bean
JSR250WayService jsr250WayService(){
return new JSR250WayService();
} }
5.运行。
package com.ecworking.bean; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(PrePostConfig.class); BeanWayService beanWayService = context.getBean(BeanWayService.class); JSR250WayService jsr250WayService = context.getBean(JSR250WayService.class); context.close();
}
}
运行结果:
二、Profile
Prifile为不同环境下提供不同不同配置提供了支持(开发环境和生产环境下的配置肯定是不同的,例如,数据库的配置)。
1.通过设定Environment的 ActiveProfile来设定当前context需要使用的配置环境。在开发环境中使用@Profile注解类或方法,达到在不同环境下选择实例化不同的Bean。
2.通过设定jvm的spring.profile.active参数来设置配置环境。
3.Web项目设置在Servlet的context parameter中。
Servlet2.5及以下:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>production</param-value>
</init-param>
</servlet>
Servlet3.0及以上
public class WebInit implement WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException{
container.setInitParameter("spring.profiles.default", "dev");
}
}
演示:
1.示例Bean。
package com.ecworking.profile; public class DemoBean {
private String content; public DemoBean(String content) {
this.content = content;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
}
}
2.profile配置。
package com.ecworking.profile; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile; @Configuration
public class ProfileConfig { @Bean
@Profile("dev") // profile为dev时实例化devDemoBean
public DemoBean devDemoBean(){
return new DemoBean("from dev profile");
} @Bean
@Profile("prod") // profile为prod时实例化prodDemoBean
public DemoBean prodDemoBean(){
return new DemoBean("from prod profile");
}
}
3.运行。
package com.ecworking.profile; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("prod"); //先将活动的Profile设置为prod。
context.register(ProfileConfig.class); //后注册Bean配置类,不然会报Bean未定义的错误。
context.refresh(); //刷新容器 DemoBean demoBean = context.getBean(DemoBean.class); System.out.println(demoBean.getContent()); context.close();
}
}
运行结果:
将 context.getEnvironment().setActiveProfiles("prod") 改为 context.getEnvironment().setActiveProfiles("dev") 效果如下:
Spring Boot实战笔记(三)-- Spring常用配置(Bean的初始化和销毁、Profile)的更多相关文章
- Spring Boot 项目学习 (三) Spring Boot + Redis 搭建
0 引言 本文主要介绍 Spring Boot 中 Redis 的配置和基本使用. 1 配置 Redis 1. 修改pom.xml,添加Redis依赖 <!-- Spring Boot Redi ...
- spring boot实战(第十三篇)自动配置原理分析
前言 spring Boot中引入了自动配置,让开发者利用起来更加的简便.快捷,本篇讲利用RabbitMQ的自动配置为例讲分析下Spring Boot中的自动配置原理. 在上一篇末尾讲述了Spring ...
- Spring Boot 系列(三)属性配置&自定义属性配置
在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配 ...
- Spring Boot实战系列(7)集成Consul配置中心
本篇主要介绍了 Spring Boot 如何与 Consul 进行集成,Consul 只是服务注册的一种实现,还有其它的例如 Zookeeper.Etcd 等,服务注册发现在微服务架构中扮演这一个重要 ...
- Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)
一.Bean的Scope Scope描述的是Spring容器如何新建Bean实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只 ...
- Spring Boot实战笔记(一)-- Spring简介
一.Spring 概述 Spring框架是一个轻量级的企业级开发的一站式解决方案.所谓的解决方案就是可以基于Spring解决所有的Java EE开发的所有问题. Spring框架主要提供了Ioc(In ...
- JavaEE开发的颠覆者 Spring Boot实战--笔记
1.Spring boot的三种启动模式 Spring 的问题 Spring boot的特点,没有特别的地方 1.Spring 基础 PS:关于spring配置 PS: 现在都已经使用 java配置, ...
- spring boot 实战笔记(一)
spring 概述: Bean :每一个被 Spring 管理的 JAVA对象,都称之为 Bean.Spring提供一个IoC容器来初始化对象,负责创建Bean, 解决对象之间的依赖管理和对象的使用. ...
- Spring Boot 菜鸟教程 application.properties 常用配置
SPRING CONFIG (ConfigFileApplicationListener) spring.config.name 配置文件名称,默认为application spring.config ...
随机推荐
- Android APP新的“优雅”退出方式--EventBus大显身手
最近在研究eventBus..很多小伙伴不知道他有什么用.. 前篇介绍了EventBus的基本使用 这里简单举一个例子,就是退出APP 转载请注明出处:http://blog.csdn.net/win ...
- 【Unity插件】LitJson杂谈
距离上一次更新博客已有一段时间了,一实习就懒了,嘿嘿.这次谈一下在实习里新碰到的一个Unity插件--LitJson(也可以去官网下载最新版). 开场白 LitJson是一个开源项目,比较小巧轻便,安 ...
- C 语言内存区域分配(进程的各个段)详解
C语言可执行代码结构 名称 内容 代码段 可执行代码.字符串常量 数据段 已初始化全局变量.已初始化全局静态变量.局部静态变量.常量数据 BSS段 未初始化全局变量,未初始化全局静态变量 栈 ...
- Workflow Notification Mailer Setup
Workflow notification mailer setup in R12 is similar to 11i ( In both release 11i (OWF.H and higher ...
- ServletRequest
/** * Defines an object to provide client request information to a servlet. The * servlet container ...
- ViewPager切换动画PageTransformer的使用
Android从3.0开始添加了属性动画后,诸多难以实现的动画都可以轻松解决了,v4包下的ViewPager控件当然也不例外,相对于非常平庸的默认切换动画,Google官方给我们展示了两个动画例子:D ...
- OpenCV GUI基本操作,回调函数,进度条,裁剪图像等
代码为转载,出处找不到了,不贴了 工具条进度条: // ConvertColor.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #incl ...
- 推荐一些用CRF做图像语义分割的资源
原文地址:http://blog.sina.com.cn/s/blog_5309cefc01014nri.html 首先是code,以前找了很多,但发现比较好用的有: 1. Matlab版的UGM:h ...
- 新版MATERIAL DESIGN 官方动效指南(二)
继上一篇,本文继续第二部分,从动效的速度.动态持续时间.通用持续时间和缓动曲线4个部分,教你创建平滑一致的Material Design 动效.再系统的干货都比不上官方的动效指南,西瓜就在这,赶紧来捡 ...
- 【Qt编程】基于Qt的词典开发系列<十二>调用讲述人
我们知道,win7系统自带有讲述人,即可以机器读出当前内容,具体可以将电脑锁定,然后点击左下角的按钮即可.之前在用Matlab写扫雷游戏的时候,也曾经调用过讲述人来进行游戏的语音提示.具体的Matla ...