• SpringBoot在静态方法或工具类中注入Bean及配置参数


    场景一:静态方法或工具类中注入Bean

    示例:

    /**
     * @author 
     * @version 1.0
     * @description: 通用工具类型
     * @date 2022/5/22 19:36
     */
    public class CommonUtil {
        @Autowired
        private static MongoTemplate mongoTemplate;
        /**
         * 处理mongo数据
         */
        public static void handleMongoData(){
            Object obj = new Object();
            mongoTemplate.insert(obj);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    使用此方式静态对象注入不成功,mongoTemplate 为 null。

    解决方式:

    1、工具类增加@Component注解;

    2、声明工具类的静态对象,即CommonUtil;

    3、使用@PostConstruct注解;

    PostConstruct 注释用于在依赖关系注入完成之后需要执行的方法上,以执行任何初始化。此方法必须在将类放入服务之前调用。支持依赖关系注入的所有类都必须支持此注释。即使类没有请求注入任何资源,用 PostConstruct 注释的方法也必须被调用。只有一个方法可以用此注释进行注释。

    使用要求:

    1、只有一个非静态方法能使用此注解;

    2、被注解的方法不得有任何参数;

    3、被注解的方法返回值必须为void;

    4、被注解方法不得抛出已检查异常;

    5、此方法只会被执行一次;

    调整后示例:

    /**
     * @author 
     * @version 1.0
     * @description: 通用工具类型
     * @date 2022/5/22 19:36
     */
    @Component
    public class CommonUtil {
        @Autowired
        private  MongoTemplate mongoTemplate;
        private  static CommonUtil commonUtil;
        @PostConstruct
        public void init(){
            commonUtil = this;
            commonUtil.mongoTemplate = this.mongoTemplate;
        }
        /**
         * 处理mongo数据
         */
        public static void handleMongoData(){
            Object obj = new Object();
            commonUtil.mongoTemplate.insert(obj);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    场景二:静态方法或工具类中注入配置参数

    前置:

    1、配置文件yaml配置如下:

    server:

    netty-port: 88480000

    2、工具类中获取server.netty-port数据值;

    示例:

    /**
     * @author 
     * @version 1.0
     * @description: 通用工具类型
     * @date 2022/5/22 19:36
     */
    @Slf4j
    @Component
    public class CommonUtil {
        @Value("${server.netty-port}")
        public static Integer nettyPort;
        @Autowired
        private  MongoTemplate mongoTemplate;
        private  static CommonUtil commonUtil;
        @PostConstruct
        public void init(){
            commonUtil = this;
            commonUtil.mongoTemplate = this.mongoTemplate;
        }
        /**
         * 处理mongo数据
         */
        public static void handleMongoData(){
            Object obj = new Object();
            commonUtil.mongoTemplate.insert(obj);
            log.info("Netty-Port: {} 。", nettyPort);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    示例代码获取不到nettyPort。

    解决方式:

    1、使用场景一中的构造器注入;

    2、使用Setter方法注入;

    调整后示例:

    /**
     * @author 
     * @version 1.0
     * @description: 通用工具类型
     * @date 2022/5/22 19:36
     */
    @Slf4j
    @Component
    public class CommonUtil {
        public static Integer nettyPort;
        @Autowired
        private  MongoTemplate mongoTemplate;
        private  static CommonUtil commonUtil;
        @PostConstruct
        public void init(){
            commonUtil = this;
            commonUtil.mongoTemplate = this.mongoTemplate;
        }
        /**
         * 处理mongo数据
         */
        public static void handleMongoData(){
            Object obj = new Object();
            commonUtil.mongoTemplate.insert(obj);
            log.info("Netty-Port: {} 。", nettyPort);
        }
        @Value("${server.netty-port}")
        public void setNettyPort(Integer nettyPort){
            CommonUtil.nettyPort = nettyPort;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    测试工具类:

    /**
     * @author 
     * @version 1.0
     * @description: 测试工具类
     * @date 2022/5/22 19:03
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = AdminApplication.class)
    public class StaticBeanAutowiredTest {
        @Test
        public void test(){
            CommonUtil.handleMongoData();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    【UVM基础】7、寄存器模型
    数据工程中的单元测试完全指南
    如何利用AirDroid远程访问安卓设备屏幕?
    【IMX6ULL学习笔记之Linux系统移植01】——环境搭建
    NJ 时钟自动调整功能(SNTP)
    单商户商城系统功能拆解32—营销中心—消费奖励
    [linux] SFTP文件传输基本命令 --- xshell 直接上传文件
    cookie、session、token
    一篇文章带你入门HBase
    华为云云耀云服务器L实例评测|centos7.9配置java环境变量安装tomcat 部署war和jar
  • 原文地址:https://blog.csdn.net/m0_67401545/article/details/126114539