zoukankan      html  css  js  c++  java
  • @PostConstruct注解

    @PostConstruct注解好多人以为是Spring提供的。其实是Java自己的注解。

    Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法。被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。

    通常我们会是在Spring框架中使用到@PostConstruct注解 该注解的方法在整个Bean初始化中的执行顺序:

    Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)

    实战:在静态方法中调用依赖注入的Bean中的方法。

    package com.example.studySpringBoot.util;
     
    import com.example.studySpringBoot.service.MyMethorClassService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
     
    import javax.annotation.PostConstruct;
     
    @Component
    public class MyUtils {
     
        private static MyUtils          staticInstance = new MyUtils();
     
        @Autowired
        private MyMethorClassService    myService;
     
        @PostConstruct
        public void init(){
            staticInstance.myService = myService;
        }
     
        public static Integer invokeBean(){
            return staticInstance.myService.add(10,20);
        }
    }
    

    版权声明:本文为CSDN博主「那些很冒险的梦丶」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

    原文链接:https://blog.csdn.net/qq360694660/java/article/details/82877222

    缘于生活,而归于工作。本人所书,而意于分享。 如有转载,请注明出处! --活出自己范儿
  • 相关阅读:
    数据结构-栈
    virtualenvwrapper 的安装和使用
    MySQL命令行本地登陆,远程登陆MySQL 的快捷键
    关于mysql8授权的问题,mysql萌新小白采坑记录
    史上最直接小白式的Sourcetree的分支创建与合并
    验证两个集合是否相等的方法
    VS开发工具的常用插件
    C# 数据库并发的解决方案(通用版、EF版)
    Jquery实现div左右重复来回走动
    描述符的应用
  • 原文地址:https://www.cnblogs.com/Small-sunshine/p/13299323.html
Copyright © 2011-2022 走看看