import org.springframework.context.annotation.AnnotationConfigApplicationContext;

使用AnnotationConfigApplicationContext可以实现基于Java的配置类加载Spring的应用上下文.避免使用application.xml进行配置。在使用spring框架进行服务端开发时,个人感觉注解配置在便捷性,和操作上都优于是使用XML进行配置;

使用JSR250注解需要在maven的pom.xml里面配置

<!-- 增加JSR250支持 -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>

  

prepostconfig文件:

package ch2.prepost;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; //声明这是一个配置类
@Configuration
//自动扫描ch2.prepost包下的@Service,@Component,@Repository,@Contrller注册为Bean
@ComponentScan()
public class PrePostConfig { //initMethod,destoryMethod制定BeanWayService类的init和destory在构造方法之后、Bean销毁之前执行
@Bean(initMethod="init",destroyMethod="destory")
BeanWayService beanWayService()
{
return new BeanWayService();
} //这是一个bean
@Bean
JSR250WayService jsr250WayService(){
return new JSR250WayService();
} }

  

JSR250WayService文件

package ch2.prepost;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; public class JSR250WayService { //PostConstruct在构造函数执行完之后执行
@PostConstruct
public void init()
{
System.out.println("jsr250-init-method");
} public JSR250WayService()
{
System.out.println("初始化构造函数JSR250WayService");
} //在Bean销毁之前执行
@PreDestroy
public void destory()
{
System.out.println("jsr250-destory-method");
} }

  

BeanWayService文件

package ch2.prepost;

//使用@bean的形式bean
public class BeanWayService { public void init()
{
System.out.println("@Bean-init-method");
} public BeanWayService()
{
super();
System.out.println("初始化构造函数-method");
} public void destory()
{
System.out.println("@Bean-destory-method");
} }

  

运行Main文件:

package ch2.prepost;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args)
{
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(PrePostConfig.class);
BeanWayService beanWayConfig = context.getBean(BeanWayService.class);
JSR250WayService jsr250WayService = context.getBean(JSR250WayService.class);
context.close(); } }

  

运行结果:

初始化构造函数JSR250WayService
jsr250-init-method

jsr250-destory-method

初始化构造函数-method

@Bean-init-method

@Bean-destory-method

spring boot: Bean的初始化和销毁 (一般注入说明(三) AnnotationConfigApplicationContext容器 JSR250注解)的更多相关文章

  1. Spring中bean的初始化和销毁几种实现方式

    Bean的生命周期 : 创建bean对象 – 属性赋值 – 初始化方法调用前的操作 – 初始化方法 – 初始化方法调用后的操作 – --- 销毁前操作 – 销毁方法的调用. [1]init-metho ...

  2. 四、Srping之Bean的初始化和销毁

    Srping之Bean的初始化和销毁方法 通常,bean的初始化和销毁方法我们有三个地方可以入手,分别是: 自定义初始化,销毁方法 实现spring提供的InitializingBean(初始化逻辑) ...

  3. Spring Boot实战笔记(三)-- Spring常用配置(Bean的初始化和销毁、Profile)

    一.Bean的初始化和销毁 在我们的实际开发的时候,经常会遇到Bean在使用之前或之后做些必要的操作,Spring对Bean的生命周期操作提供了支持.在使用Java配置和注解配置下提供如下两种方式: ...

  4. spring boot之 Bean的初始化和销毁(4)

    原文:https://blog.csdn.net/z3133464733/article/details/79189699 -------------------------------------- ...

  5. Spring bean 实现初始化、销毁方法的方式及顺序

    Spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作,常用方法有三种: 使用注解,在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是 ...

  6. Spring3实战第二章第一小节 Spring bean的初始化和销毁三种方式及优先级

    Spring bean的初始化和销毁有三种方式 通过实现 InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法: 优先级第二通过 <bean& ...

  7. Spring bean的初始化及销毁

    Spring bean的几个属性:scope.init-method.destroy-method.depends-on等. Scope 在Spring容器中是指其创建的Bean对象相对于其他Bean ...

  8. 【Spring Framework】Spring注解设置Bean的初始化、销毁方法的方式

    bean的生命周期:创建---初始化---销毁. Spring中声明的Bean的初始化和销毁方法有3种方式: @Bean的注解的initMethod.DestroyMethod属性 bean实现Ini ...

  9. Spring boot变量的初始化顺序

    起因是Spring建议”总是在您的bean中使用构造函数建立依赖注入.总是使用断言强制依赖”,而且之前用@Autowired时idea总是给警告,于是全部改成了构造器注入,运行时发生了循环注入,于是找 ...

随机推荐

  1. C++11并发学习之三:线程同步(转载)

    C++11并发学习之三:线程同步 1.<mutex> 头文件介绍 Mutex又称互斥量,C++ 11中与 Mutex 相关的类(包括锁类型)和函数都声明在 <mutex> 头文 ...

  2. centos 7 安装 mail

    yum install -y sendmail 将下面内容粘贴到/etc/mail.rc中 set ssl-verify=ignore set nss-config-dir=/root/.certs ...

  3. shiro设置session超时

    通过api:Shiro的Session接口有一个setTimeout()方法 //登录后,可以用如下方式取得session SecurityUtils.getSubject().getSession( ...

  4. linux 改动时间和日期date

    查看日期和时间 date 改动日期 date -s 月/日/年 date -s 08/15/2015 改动时间 date -s 09:29:33 写入CMOS sudo clock -w 利用ssh同 ...

  5. Openstack(Kilo)安装系列之环境准备(一)

    本文采用VMware虚拟环境,使用CentOS 7.1作为openstack的基础环境. 一.基础平台 1.一台装有VMware的windows系统(可联网) 2.CentOS 7.1 64bit镜像 ...

  6. ASP.NET动态网站制作(14)-- CSS3

    前言:这节课主要讲解CSS之前没有讲到过的知识点以及CSS3的一些内容. 内容: 1.内容参考博文:http://www.cnblogs.com/ruanmou/p/4832214.html. 后记: ...

  7. vptr

    #include <stdio.h> class Point3d { public: virtual ~Point3d(){} public: static Point3d origin; ...

  8. 大组合取模之:1<=n<=m<=1e6,1<=p<=1e9

    /****************************** 大组合取模之:1<=n<=m<=1e6,1<=p<=1e9 使用:程序最开始调用getprime(),需要 ...

  9. Java 学习 day08

    01-面向对象(多态-概念) package myFirstCode; /* 多态:可以理解为事务存在的多种体现形态. 人:男人,女人 动物:猫,狗 猫 x = new 猫(); 动物 x = new ...

  10. 初步jmeter安装与使用

    前言,最近公司做了面向全国用户的教育平台,由于测试人员以功能测试为主,于是接口代码压测就被开发揽了,这就开始倒腾jmeter了,其实我想对于java,我更愿意用Python的工具,毕竟我爬虫时用的Py ...