Modrisco

Lingxu Meng's personal blog

Information Technology student graduated from BUPT and now learning in UNSW.


Spring Annotation - @PostConstruct

Executing specific methon in initialization

Annotation explanation

Spring annotated @PostConstruct annotation on a method, means the method is executed immediately after this Java Bean is instantiated by Spring before instantiating other Beans. @PostConstruct could also be used for annotating multiple methods in a single Bean.

Example code:

package com.example.demo.service;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service("BeanA")
public class BeanA {
    private static final Logger LOGGER = LoggerFactory.getLogger(BeanA.class);

    public BeanA() {
        LOGGER.info("Bean A constructed in -> {} ", System.currentTimeMillis());
    }

    @PostConstruct
    public void start() {
        LOGGER.info("Bean A started in -> {}, calling Bean B", System.currentTimeMillis());
        BeanB.uniqueInstance().print();
    }

    @PostConstruct
    public void print() {
        LOGGER.info("Bean A running a private function -> {} ", System.currentTimeMillis());
    }
}

package com.example.demo.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service("BeanB")
public class BeanB {
    private static final Logger LOGGER = LoggerFactory.getLogger(BeanB.class);

    public BeanB() {
        LOGGER.info("Bean B constructed in -> {} ", System.currentTimeMillis());
    }

    private static BeanB instance = uniqueInstance();

    public static BeanB uniqueInstance() {
        if (instance == null ) {
            instance = new BeanB();
        }
        return instance;
    }

    @PostConstruct
    public void start() {
        LOGGER.info("Bean B started in -> {} ", System.currentTimeMillis());
    }

    @PostConstruct
    public void print() {
        LOGGER.info("Bean B running a private function -> {} ", System.currentTimeMillis());
    }
}

package com.example.demo.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


@Service("BeanC")
public class BeanC {
    private static final Logger LOGGER = LoggerFactory.getLogger(BeanC.class);

    public BeanC() {
        LOGGER.info("Bean C constructed in -> {} ", System.currentTimeMillis());
    }
}

Execucation result:

2020-08-23 10:53:00.761  INFO 49785 --- [           main] com.example.demo.service.BeanA           : Bean A constructed in -> 1598143980761 
2020-08-23 10:53:00.762  INFO 49785 --- [           main] com.example.demo.service.BeanA           : Bean A started in -> 1598143980762, calling Bean B
2020-08-23 10:53:00.762  INFO 49785 --- [           main] com.example.demo.service.BeanB           : Bean B constructed in -> 1598143980762 
2020-08-23 10:53:00.762  INFO 49785 --- [           main] com.example.demo.service.BeanB           : Bean B running a private function -> 1598143980762 
2020-08-23 10:53:00.762  INFO 49785 --- [           main] com.example.demo.service.BeanA           : Bean A running a private function -> 1598143980762 
2020-08-23 10:53:00.762  INFO 49785 --- [           main] com.example.demo.service.BeanB           : Bean B constructed in -> 1598143980762 
2020-08-23 10:53:00.763  INFO 49785 --- [           main] com.example.demo.service.BeanB           : Bean B started in -> 1598143980763 
2020-08-23 10:53:00.763  INFO 49785 --- [           main] com.example.demo.service.BeanB           : Bean B running a private function -> 1598143980763 
2020-08-23 10:53:00.763  INFO 49785 --- [           main] com.example.demo.service.BeanC           : Bean C constructed in -> 1598143980763 

Add up:

Constructor >> @Autowired >> @PostConstruct

更早的文章

Vim 入门:基础

为工作或者新项目学习足够的 Vim 知识。我还清晰地记得我第一次接触 Vim 的时候。那时我还是一名大学生,计算机学院的机房里都装着 Ubuntu 系统。尽管我在上大学前也曾接触过不同的 Linux 发行版(比如 RHEL —— Red Hat 在百思买出售它的 CD),但这却是我第一次要在日常中频繁使用 Linux 系统,因为我的课程要求我这样做。当我开始使用 Linux 时,正如我的前辈和将来的后继者们一样,我感觉自己像是一名“真正的程序员”了。真正的程序员,来自 xkcd学生们可...…

继续阅读