spring boot: Bean的初始化和销毁 (一般注入说明(三) AnnotationConfigApplicationContext容器 JSR250注解)
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注解)的更多相关文章
- Spring中bean的初始化和销毁几种实现方式
Bean的生命周期 : 创建bean对象 – 属性赋值 – 初始化方法调用前的操作 – 初始化方法 – 初始化方法调用后的操作 – --- 销毁前操作 – 销毁方法的调用. [1]init-metho ...
- 四、Srping之Bean的初始化和销毁
Srping之Bean的初始化和销毁方法 通常,bean的初始化和销毁方法我们有三个地方可以入手,分别是: 自定义初始化,销毁方法 实现spring提供的InitializingBean(初始化逻辑) ...
- Spring Boot实战笔记(三)-- Spring常用配置(Bean的初始化和销毁、Profile)
一.Bean的初始化和销毁 在我们的实际开发的时候,经常会遇到Bean在使用之前或之后做些必要的操作,Spring对Bean的生命周期操作提供了支持.在使用Java配置和注解配置下提供如下两种方式: ...
- spring boot之 Bean的初始化和销毁(4)
原文:https://blog.csdn.net/z3133464733/article/details/79189699 -------------------------------------- ...
- Spring bean 实现初始化、销毁方法的方式及顺序
Spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作,常用方法有三种: 使用注解,在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是 ...
- Spring3实战第二章第一小节 Spring bean的初始化和销毁三种方式及优先级
Spring bean的初始化和销毁有三种方式 通过实现 InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法: 优先级第二通过 <bean& ...
- Spring bean的初始化及销毁
Spring bean的几个属性:scope.init-method.destroy-method.depends-on等. Scope 在Spring容器中是指其创建的Bean对象相对于其他Bean ...
- 【Spring Framework】Spring注解设置Bean的初始化、销毁方法的方式
bean的生命周期:创建---初始化---销毁. Spring中声明的Bean的初始化和销毁方法有3种方式: @Bean的注解的initMethod.DestroyMethod属性 bean实现Ini ...
- Spring boot变量的初始化顺序
起因是Spring建议”总是在您的bean中使用构造函数建立依赖注入.总是使用断言强制依赖”,而且之前用@Autowired时idea总是给警告,于是全部改成了构造器注入,运行时发生了循环注入,于是找 ...
随机推荐
- 从零開始学Swift之Hello World进化版
上节课,也就是昨晚啦,我们学习到从零開始学Swift之Hello World.那一节仅仅有一句代码,大家会认为不够过瘾. 那么这节课,就给大家来多点瘾货吧! 先上图! //var 代表变量的类型, s ...
- 自动化测试工具 - Unified Functional Testing
这几天跟自动化测试工具UFT耗上了... 网罗了下,居然有不少自动化测试工具,像Selenium,QTP(UFT前身),LoadRunner,真是只有想不到,没有人家办不到. 言归正传,记录下小白使用 ...
- Failed to read artifact descriptor for org.apache.httpcomponents:httpmime:jar
额,在Stackoverflow上找到了一个答案: I had this in eclipse and did this which fixed it(even though my command l ...
- 给定一个字符串s,你可以从中删除一些字符,使得剩下的串是一个回文串。如何删除才能使得回文串最长呢? 输出需要删除的字符个数。
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> ...
- Nginx 变量漫谈
转自:http://blog.sina.com.cn/openrestyNginx 的配置文件使用的就是一门微型的编程语言,许多真实世界里的 Nginx 配置文件其实就是一个一个的小程序.当然,是不是 ...
- php 模拟get和post提交方法[解决ajax跨域问题]
get: $url = "http://www.111cn.net /index.php?a=b&c=d&e=f&g=" . urlencode('王璐个人 ...
- DB2恢复一例 SQL0928N
环境是AIX 6.1.DB2版本号9.7.0.7 首先查看db2主进程是否存在 ps -ef|grep db2sys 若不存在使用db2start打开数据库 备份介质为冷备数据源, cd 到介质所在文 ...
- iptables启动脚本分析
#!/bin/sh # # iptables Start iptables firewall # # chkconfig: 2345 08 92 # description: Starts, stop ...
- vue表单输入的绑定
vue的核心:声明式的指令和数据的双向绑定. 那么声明式的指令,已经给大家介绍完了.接下来我们来研究一下什么是数据的双向绑定? 另外,大家一定要知道vue的设计模式:MVVM M是Model的简写,V ...
- Android开发之深入理解泛型extends和super的区别
摘要: 什么是泛型?什么是擦除边界?什么是上界限定或下界限定(子类型限定或超类型限定)?什么是类型安全?泛型extends关和super关键字结合通配符?使用的区别,两种泛型在实际Android开发中 ...