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 ...
随机推荐
- 取消选中单选框radio的三种方式
作者: 铁锚 日期: 2013年12月21日 本文提供了三种取消选中radio的方式,代码示例如下: 本文依赖于jQuery,其中第一种,第二种方式是使用jQuery实现的,第三种方式是基于JS和DO ...
- Material Design之RecyclerView的使用(一)
Android 5.0开始就推荐使用Material Design这个设计语言,本文开始就逐一讲解Material Design中控件的使用.本章主要讲解RecyclerView,期中会带有ToolB ...
- (三十三)Xcode项目的重要工程文件
1.Supporting files内有一个Xxx-Info.plist文件(旧版本Xcode的配置文件叫Info.plist).因此自定义的plist不要带Info关键词. 这个plist是系统的全 ...
- Mahout系列之----kmeans 聚类
Kmeans是最经典的聚类算法之一,它的优美简单.快速高效被广泛使用. Kmeans算法描述 输入:簇的数目k:包含n个对象的数据集D. 输出:k个簇的集合. 方法: 从D中任意选择k个对象作为初始簇 ...
- 关于iOS常用的26中公共方法,可copy的代码
1. 获取磁盘总空间大小 //磁盘总空间 + (CGFloat)diskOfAllSizeMBytes{ CGFloat size = 0.0; NSError *error; NSDictionar ...
- 《java入门第一季》之面向对象(private关键字与封装概念的初探)
/* 定义一个学生类: 成员变量:name,age 成员方法:show()方法 在使用这个案例的过程中,发现了一个问题: 通过对象去给成员变量赋值,可以赋值一些非法的数据.例如:name你赋值了一个3 ...
- JDBC详解(汇总)
from:http://www.cnblogs.com/lee/archive/2007/08/25/869656.html:http://blog.csdn.net/lovesomnus/artic ...
- Zeroc Ice开发环境搭建
搭建Ice环境 1. Linux(推荐,更接近真实生产环境) 2. Windows(方便学习开发) 下载安装包:https://zeroc.com/downloads (百度网盘链接:http ...
- Spring 学习笔记 ----依赖注入
依赖注入 有三种方式,本文只学习下属性注入. 属性注入 属性注入即通过 setXxx方法()注入Bean的属性值或依赖对象,由于属性注入方式具有可选择性和灵活性高的优点,因此属性注入方式是 ...
- ubuntu14.04下安装rubinius测试原生线程
因为CRuby(MRI)本身不支持原生多线程,所以想试一下其他ruby解释器实现对原生多线程的支持.于是安装rubinius折腾一下:) 在rubinius官网下载2.4.1源代码,然后驾轻就熟首先b ...