一、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)的更多相关文章

  1. Spring Boot 项目学习 (三) Spring Boot + Redis 搭建

    0 引言 本文主要介绍 Spring Boot 中 Redis 的配置和基本使用. 1 配置 Redis 1. 修改pom.xml,添加Redis依赖 <!-- Spring Boot Redi ...

  2. spring boot实战(第十三篇)自动配置原理分析

    前言 spring Boot中引入了自动配置,让开发者利用起来更加的简便.快捷,本篇讲利用RabbitMQ的自动配置为例讲分析下Spring Boot中的自动配置原理. 在上一篇末尾讲述了Spring ...

  3. Spring Boot 系列(三)属性配置&自定义属性配置

    在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配 ...

  4. Spring Boot实战系列(7)集成Consul配置中心

    本篇主要介绍了 Spring Boot 如何与 Consul 进行集成,Consul 只是服务注册的一种实现,还有其它的例如 Zookeeper.Etcd 等,服务注册发现在微服务架构中扮演这一个重要 ...

  5. Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)

    一.Bean的Scope Scope描述的是Spring容器如何新建Bean实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只 ...

  6. Spring Boot实战笔记(一)-- Spring简介

    一.Spring 概述 Spring框架是一个轻量级的企业级开发的一站式解决方案.所谓的解决方案就是可以基于Spring解决所有的Java EE开发的所有问题. Spring框架主要提供了Ioc(In ...

  7. JavaEE开发的颠覆者 Spring Boot实战--笔记

    1.Spring boot的三种启动模式 Spring 的问题 Spring boot的特点,没有特别的地方 1.Spring 基础 PS:关于spring配置 PS: 现在都已经使用 java配置, ...

  8. spring boot 实战笔记(一)

    spring 概述: Bean :每一个被 Spring 管理的 JAVA对象,都称之为 Bean.Spring提供一个IoC容器来初始化对象,负责创建Bean, 解决对象之间的依赖管理和对象的使用. ...

  9. Spring Boot 菜鸟教程 application.properties 常用配置

    SPRING CONFIG (ConfigFileApplicationListener) spring.config.name 配置文件名称,默认为application spring.config ...

随机推荐

  1. 【Android 应用开发】Android 数据存储 之 SQLite数据库详解

    . 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19028665 . SQLiteDataBase示例程序下 ...

  2. 升级CentOS5.6_X64 python2.4.3到2.7

    本文转自:http://hxl2009.blog.51cto.com/779549/1031310 升级CentOS 5.6 64位版python到2.7.31. 背景CentOS 5.6自带的Pyt ...

  3. LeetCode之“链表”:在O(1)时间删除链表节点

    下边讨论暂不包括尾节点. 一般来说,我们要删除链表中的一个节点是需要知道其上一节点的.但我们真的需要吗? 其实我们可以将待删节点的下一节点的值和指向的下一节点赋予待删节点,然后删除待删节点的下一节点. ...

  4. 百度地图android studio导入开发插件

    百度地图SDK v3.5.0开发包下载地址:http://lbsyun.baidu.com/sdk/download?selected=location 开发工具 Android开发工具很多,在这我们 ...

  5. TCP的核心系列 — SACK和DSACK的实现(二)

    和18版本相比,37版本的SACK和DSACK的实现做了很多改进,最明显的就是需要遍历的次数少了, 减少了CPU的消耗.37版的性能提升了,代码有大幅度的改动,逻辑也更加复杂了. 本文主要内容:37版 ...

  6. LeetCode之“动态规划”:Interleaving String

    题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...

  7. Media Player Classic - HC 源代码分析 2:核心类 (CMainFrame)(1)

    ===================================================== Media Player Classic - HC 源代码分析系列文章列表: Media P ...

  8. CImage 对话框初始化时候显示透明 PNG

    使用CImage的时候,发现显示出来的并不是透明背景的图片,而是白色背景的图片. 后发现原因如下: PNG图片的透明背景总是一片白色,后来才发现这其实是微软GDI+的设计问题,PNG图片是ARGB,使 ...

  9. Linux进程管理 - PRI,nice,free,uname,netstat

    优先运行序 (priority, PRI) 这个 PRI 值越低代表越优先的意思.不过这个 PRI 值是由核心动态调整的, 使用者无法直接调整 PRI 值的. 由於 PRI 是核心动态调整的,我们使用 ...

  10. UIScrollView的无限左滑轮播一点也不难

    UIScrollView的轮播在如今的app中用得十分广泛,最初实现的时候方式比较拙劣,滚动到最后一个视图时再返回到第一个看起来非常的不连贯. 今天查询UIScrollView轮播资料,总结两种比较喜 ...