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. hdu - 5033 - Building(单调栈)

    题意:N 幢楼排成一列(1<=N<=10^5),各楼有横坐标 xi(1<=xi<=10^7) 以及高度 hi(1<=hi<=10^7),在各楼之间的Q个位置(1&l ...

  2. linux 中vim学习与总结

    平常使用vim总是忘记快捷键,在这里做一个总结一下比较常用的快捷把,省的每次都要去查. h : 向左移动一个字符(←) j : 向上移动一个字符(↑) k : (↓) l : (→) ctrl+f : ...

  3. 【demo练习二】:WPF依赖属性的练习

    2016-10-11 依赖属性demo小样: 要求:在窗口中点击按钮,利用设置“依赖属性”把Label和TextBox控件里的属性值进行改变. ============================ ...

  4. tcp 状态转移图详解

    首先看一张图片: 虚线表示服务端的状态转移,实现表示客户端的状态转移. 初始的close状态并不是真是的状态,只是为了方便描述开始和终止状态而构造出来的. 从服务端的状态转移开始说: 服务端打开后处于 ...

  5. SimpleAdapter的用法

    学习listView的时候,按照例子设定item的布局为系统提供的simple_list_item_single_choice.xml@frameworks/base/core/res/res/lay ...

  6. 【转】使用 Python Mock 类进行单元测试

    出处:https://www.oschina.net/translate/unit-testing-with-the-python-mock-class?lang=chs&page=2#

  7. 关于函数的return

    function add(x, y) { var total = x + y; return total; } add(5,10); 关于函数的return 我一开始是认为没有什么用的  后来在项目中 ...

  8. android webview 加载本地html 实现 与 java 之间的相互响应

    1.布局 <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:androi ...

  9. win10中如何成功安装lxml

    lxml官网地址:http://lxml.de/index.html 问题: 在学习lxm的时候,发现在win10下总是安装失败,如下: 在网上搜索了半天也没找到具体的解决方案,就FQgoogle下, ...

  10. 【BZOJ4205】卡牌配对 最大流

    [BZOJ4205]卡牌配对 Description 现在有一种卡牌游戏,每张卡牌上有三个属性值:A,B,C.把卡牌分为X,Y两类,分别有n1,n2张. 两张卡牌能够配对,当且仅当,存在至多一项属性值 ...