• spring @value 注入static 注入静态变量方法


    对于spring static变量

    下面给大家介绍spring不能注入static变量的原因,具体详情如下所示:

    Spring 依赖注入 是依赖 set方法

    set方法是 是普通的对象方法

    static变量是类的属性

    只能在setAppId方法上加注解,另外class需要加 @Component等注解,这样spring才能扫描到

    对于

    import org.springframework.beans.factory.annotation.Value;
        import org.springframework.stereotype.Component;
    
        @Componentpublic
        class GlobalValue {
            @Value("${mysqk.db}")
            public static String DATABASE;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    DATABASE的值是null

    但是静态的XXX如何注入呢?

    上网查了很多的说法,其实很简单:

    import org.springframework.beans.factory.annotation.Value;
        import org.springframework.stereotype.Component;
        import lombok.Getter;
    
        @Componentpublic
        class GlobalValue {
            @Getter
            public static String DATABASE;
    
            @Value("${mysql.db:test}")
            public void setDatabase(String db) {
                DATABASE = db;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    DATABASE可以获取到值

    这里要特别注意,自动生成的getter和setter方法,会带有static的限定符,需要去掉,才可以。

    ===========================================

    方式二 通过InitializingBean

    import lombok.Getter;
        import lombok.Setter;
        import org.springframework.beans.factory.InitializingBean;
        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.context.annotation.Configuration;
    
        /**
         * 通用配置 *
         */
        @Configurationpublic
        class TestConfig implements InitializingBean {
            /**
             * 数据拼接时,数据分隔符,默认:;
             */
            @Value("${data.separator.flag:&}")
            private String separatorCon;
            /**
             * 数据拼接时,数据分隔符,默认:;
             */
            @Getter
            @Setter
            private static String separator;
    
            @Override
            public void afterPropertiesSet() throws Exception {
                separator = separatorCon;
            }
        }
    
    • 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

    ===========================================

    方式三 定义后通过注入bean

     @Autowiredprivate TestConfig config;
    
    • 1

    然后通过bean获取

    ===========================================

    方式四

    @PostConstruct方式实现

    import org.mongodb.morphia.AdvancedDatastore;
        import org.springframework.beans.factory.annotation.Autowired;
    
        @Componentpublic
        class MongoFileOperationUtil {
            @Autowired
            private static AdvancedDatastore dsForRW;
            private static MongoFileOperationUtil mongoFileOperationUtil;
    
            @PostConstruct
            public void init() {
                mongoFileOperationUtil = this;
                mongoFileOperationUtil.dsForRW = this.dsForRW;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    @PostConstruct 注解的方法在加载类的构造函数之后执行,也就是在加载了构造函数之后,执行init方法;(@PreDestroy 注解定义容器销毁之前的所做的操作)
    这种方式和在xml中配置 init-method和 destory-method方法差不多,定义spring 容器在初始化bean 和容器销毁之前的所做的操作

    ====================================================================

    方式五 set方法上添加@Autowired注解,类定义上添加@Component注解;

    import org.mongodb.morphia.AdvancedDatastore;
         import org.springframework.beans.factory.annotation.Autowired;
         import org.springframework.stereotype.Component;
    
        @Componentpublic
        class MongoFileOperationUtil {
            private static AdvancedDatastore dsForRW;
    
            @Autowired
            public void setDatastore(AdvancedDatastore dsForRW) {
                MongoFileOperationUtil.dsForRW = dsForRW;
            }
    
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    首先Spring要能扫描到AdvancedDatastore的bean,然后通过setter方法注入;

    然后注意:成员变量上不需要再添加@Autowired注解;

    转载至:https://blog.csdn.net/ZYC88888/article/details/87863038

  • 相关阅读:
    前端新手Vue3+Vite+Ts+Pinia+Sass项目指北系列文章 —— 第五章 组件库安装和使用(Element-Plus基础配置)
    【知识地图】开发测试全栈 (未完待续)
    ICCV2021 Exploring Cross-Image Pixel Contrast for Semantic Segmentation (Oral)
    【控制台】报错:Uncaught ReferenceError: process is not defined
    java 使用bc库封装ASN1结构案例
    C. Nice Garland
    【rosrun diagnostic_analysis】报错No module named rospkg | ubuntu 20.04
    Day-02 从 0 开始搭建一套规范的 Vue3.x 项目工程环境
    不用Swagger,那我用啥?
    发个阿里云广告,对园子脱困很重要
  • 原文地址:https://blog.csdn.net/asd54090/article/details/132628026