@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注解的更多相关文章

  1. Spring@PostConstruct注解和构造方法的调用顺序

    先看下@PostConstruct的注解 * The PostConstruct annotation is used on a method that needs to be executed * ...

  2. 【spring源码分析】spring和@PostConstruct注解

    @PostConstruct注解好多人以为是Spring提供的.其实是Java自己的注解. Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法.被@Pos ...

  3. Spring@PostConstruct和@PreDestroy注解详解

    @PostConstruct注解使用 @PostConstructApi使用说明 The PostConstruct annotation is used on a method that needs ...

  4. Spring系列之Spring常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  5. Spring JSR-250注解

    Java EE5中引入了“Java平台的公共注解(Common Annotations for the Java Platform)”,而且该公共注解从Java SE 6一开始就被包含其中. 2006 ...

  6. Spring中注解的使用详解

    一:@Rsource注解的使用规则 1.1.案例演示 Spring的主配置文件:applicationContext.xml(因为我这里将会讲到很多模块,所以我用一个主配置文件去加载各个模块的配置文件 ...

  7. Spring常用注解介绍【经典总结】

    Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spring XML配置方式. Spring注解方式减少了配置文件内容 ...

  8. Spring常用注解简析

    1. Autowired 自动装配,其作用是为了消除代码Java代码里面的getter/setter与bean属性中的property.当然,getter看个人需求,如果私有属性需要对外提供的话,应当 ...

  9. Spring常用注解总结

    转载自:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点 ...

  10. Spring _day02_IoC注解开发入门

    1.Spring IoC注解开发入门 1.1 注解开发案例: 创建项目所需要的jar,四个基本的包(beans core context expression ),以及两个日志记录的包,还要AOP的包 ...

随机推荐

  1. 从微信小程序到鸿蒙js开发【11】——页面路由

    目录: 1.router.push()&wx.navigateTo() 2.router.replace()&wx.redirectTo() 3.router.back()&w ...

  2. oracle创建和删除序列

    创建序列语法: CREATE SEQUENCE 序列名 [START WITH n] [INCREMENT BY n] [{MINVALUE n}] [{MAXVALUE n|NOMAXVALUE}] ...

  3. django 内置“信号”机制和自定义方法

    一.引子 在操作数据的时候,假设我们需要记录一些日志,这个时候,我们需要用什么来显示这个需求呢?装饰器?装饰器只能先实现整体的操作.在django 里面有这么一个东西--信号 下面我们就来了解了解它. ...

  4. Web性能优化之瘦身秘笈

    Web 传输的内容当然是越少越好,最近一段时间的工作一直致力于 Web 性能优化,这是我近期使用过的一些缩减 Web 体积的手段 这些手段主要是为了减少 Web 传输的内容大小,只有干货 CSS 删除 ...

  5. 如何读写拥有命名空间xmlns 属性的Xml文件(C#实现)

    我们在进行C#项目Xml读写开发时经常遇到一些读写问题,今天我要介绍的是遇到多个命名空间xmlns属性时如何读写此类文件. 比如下面这个Xml文件: <?xml version="1. ...

  6. 剑指 Offer 62. 圆圈中最后剩下的数字 + 约瑟夫环问题

    剑指 Offer 62. 圆圈中最后剩下的数字 Offer_62 题目描述 方法一:使用链表模拟 这种方法是暴力方法,时间复杂度为O(nm),在本题中数据量过大会超时. 方法二:递归方法 packag ...

  7. 剑指 Offer 53 - II. 0~n-1中缺失的数字 + 二分法

    剑指 Offer 53 - II. 0-n-1中缺失的数字 Offer_53 题目详情 java代码 package com.walegarrett.offer; /** * @Author Wale ...

  8. crontab任务重复执行?不执行?不按照配置执行?大概率是配置出错了!!!

    在使用crontab配置定时任务是,容易大意出错的配置记录,有温度的文章分享,有态度的日常记录- 一.情景1 设置每天凌晨执行某一任务,结果发现凌晨0点没分钟都执行了一次,我的天!!! 1.分析原因可 ...

  9. Node.js 模块化你所需要知道的事

    一.前言 我们知道,Node.js是基于CommonJS规范进行模块化管理的,模块化是面对复杂的业务场景不可或缺的工具,或许你经常使用它,但却从没有系统的了解过,所以今天我们来聊一聊Node.js模块 ...

  10. 一款免费的在线 Markdown 笔记,类似 typora 编辑体验

    为什么要开发一款新的编辑器 自从我开始使用 Markdown,就爱上了这种标记语法,轻量.纯文本兼容是最大的优点,哪里都可以编辑,一开始是在 IDE 上直接编辑,后来笔记越来越多,需要上传图片,有云同 ...