Spring-@PostConstruct注解
@PostConstruct注解
@PostConstruct注解好多人以为是Spring提供的。其实是Java自己的注解。
Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法。被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。
通常我们会是在Spring框架中使用到@PostConstruct注解 该注解的方法在整个Bean初始化中的执行顺序:
Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)
Why use @PostConstruct?
because when the constructor is called, the bean is not yet initialized - i.e. no dependencies are injected. In the @PostConstruct
method the bean is fully initialized and you can use the dependencies.
because this is the contract that guarantees that this method will be invoked only once in the bean lifecycle. It may happen (though unlikely) that a bean is instantiated multiple times by the container in its internal working, but it guarantees that @PostConstruct
will be invoked only once.
@Configuration
public class BeanConfiguration { @Bean
public ToyFactory toyFactory() {
return new DefaultToyFactory();
} }
接口
public interface ToyFactory {
Toy createToy();
}
接口实现类
package com.sixinshuier.Initialization.servive.impl; import com.sixinshuier.Initialization.entity.Toy;
import com.sixinshuier.Initialization.servive.ToyFactory; import javax.annotation.PostConstruct; public class DefaultToyFactory implements ToyFactory { public DefaultToyFactory() {
System.out.println("构造器...");
} // 1. 基于 @PostConstruct 注解
@PostConstruct
public void init() {
System.out.println("@PostConstruct : DefaultToyFactory 初始化中...");
} @Override
public Toy createToy() {
Toy toy = new Toy();
toy.setName("Football");
toy.setSize("big");
return toy;
}
}
main
package com.sixinshuier.Initialization.main; import com.sixinshuier.Initialization.config.BeanConfiguration;
import com.sixinshuier.Initialization.entity.Toy;
import com.sixinshuier.Initialization.servive.ToyFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class PostConstructTest { public static void main(String[] args) {
// 创建BeanFactory
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
// 注册Configuration Class(配置类)
applicationContext.register(BeanConfiguration.class);
// 启动spring应用上下文
applicationContext.refresh();
// 依赖查找
ToyFactory toyFactory = applicationContext.getBean(ToyFactory.class);
Toy toy = toyFactory.createToy();
System.out.println("ToyName:" + toy.getName() + " ToySize:" + toy.getSize());
//关闭Spring应用上下文
applicationContext.close();
}
}
输出结果
构造器...
@PostConstruct : DefaultToyFactory 初始化中...
ToyName:Football ToySize:big
参考:https://blog.csdn.net/qq360694660/article/details/82877222
Spring-@PostConstruct注解的更多相关文章
- Spring@PostConstruct注解和构造方法的调用顺序
先看下@PostConstruct的注解 * The PostConstruct annotation is used on a method that needs to be executed * ...
- 【spring源码分析】spring和@PostConstruct注解
@PostConstruct注解好多人以为是Spring提供的.其实是Java自己的注解. Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法.被@Pos ...
- Spring@PostConstruct和@PreDestroy注解详解
@PostConstruct注解使用 @PostConstructApi使用说明 The PostConstruct annotation is used on a method that needs ...
- Spring系列之Spring常用注解总结
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...
- Spring JSR-250注解
Java EE5中引入了“Java平台的公共注解(Common Annotations for the Java Platform)”,而且该公共注解从Java SE 6一开始就被包含其中. 2006 ...
- Spring中注解的使用详解
一:@Rsource注解的使用规则 1.1.案例演示 Spring的主配置文件:applicationContext.xml(因为我这里将会讲到很多模块,所以我用一个主配置文件去加载各个模块的配置文件 ...
- Spring常用注解介绍【经典总结】
Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spring XML配置方式. Spring注解方式减少了配置文件内容 ...
- Spring常用注解简析
1. Autowired 自动装配,其作用是为了消除代码Java代码里面的getter/setter与bean属性中的property.当然,getter看个人需求,如果私有属性需要对外提供的话,应当 ...
- Spring常用注解总结
转载自:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点 ...
- Spring _day02_IoC注解开发入门
1.Spring IoC注解开发入门 1.1 注解开发案例: 创建项目所需要的jar,四个基本的包(beans core context expression ),以及两个日志记录的包,还要AOP的包 ...
随机推荐
- JDK源码阅读-ByteBuffer
本文转载自JDK源码阅读-ByteBuffer 导语 Buffer是Java NIO中对于缓冲区的封装.在Java BIO中,所有的读写API,都是直接使用byte数组作为缓冲区的,简单直接.但是在J ...
- 纯CSS实现内容放大缩小效果
先搭架子 再实现第一个内容 填充更多内容 拆掉border,查看最终效果 html代码 <!-- 服务 --> <div class="service"> ...
- ElasticSearcher的安装以及安装过程中出现的问题
先给出参考链接,带安装成功后再进行总结整个过程. 参考链接:https://blog.csdn.net/fjyab/article/details/81101284 java操作ElasticSear ...
- redis数据结构和对象一
1. SDS:简单动态字符串(simple dynamic string) Redis没有直接使用C语言的字符串,而是自己构建了一种名为简单动态字符串类型,并将SDS用作Redis的默认字符串. SD ...
- POJ-3268(来回最短路+dijkstra算法)
Silver Cow Party POJ-3268 这题也是最短路的模板题,只不过需要进行两次求解最短路,因为涉及到来回的最短路之和. 该题的求解关键是:求解B-A的最短路时,可以看做A是起点,这就和 ...
- dom_bom学习
1. bom是什么? browser object model(浏览器对象模型) 在浏览器中 window指的就是bom对象 //网页重定向 // window.location.href=" ...
- 【测试技术分享】在Linux下安装Python3
导语:Python在linux环境下没有安装包,同时很多系统没有Python环境,即使有Python环境也是Python2.x,顺应时代,现在开始进行安装Python3的教程. 一.安装依赖 sudo ...
- Java方法:命令行传参,重载,可变参数,递归
Java方法:System.out.println()//系统类.out对象.输出方法Java方法是语句的集合,他们在一起执行一个功能方法是解决一类问题的步骤的有序组合方法包含于类或对象中方法在程序中 ...
- innerHTML,innerText
文本替换 <p id="p1">Hello World!</p><div>神经</div><h3 class="hh ...
- PTE 准备之 Personal introduction
Task strategies Be prepared! This is your opportunity to give the admissions officers a first impres ...