• spring boot 将配置文件信息 赋值到类注解


    如何将application.properties中的值赋值给一个类注解呢

    先看两个类
    application.properties

    server.port=8080
    flow.name=myFlow
    flow.age=20
    
    • 1
    • 2
    • 3
    @Component
    @Documented
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface UserInfo {
    
        String name() default "";
    
        String age() default "18";
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    我们现在将application.properties中的flow.name和flow.age赋值到UserInfo注解的name和age中,应该怎么做呢?具体见下面代码,
    先定义一个User类获取properties里面的值

    @Configuration
    public class User {
    
        private String name;
    
        private String age;
    
    
        public String getName() {
            return name;
        }
    
    
        @Value("${flow.name}")
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAge() {
            return age;
        }
    
        @Value("${flow.age}")
        public void setAge(String age) {
            this.age = age;
        }
    
        public User() {
        }
    
        public User(String name, String age) {
            this.name = name;
            this.age = age;
        }
    }
    
    • 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
    • 32
    • 33
    • 34
    • 35

    然后我们再需要用这个类的地方这样写

    @UserInfo(name = "@user.name", age = "@user.age")
    @Service
    public class UserServiceImpl implements UserService {
    }
    
    • 1
    • 2
    • 3
    • 4

    最后我们定义一个配置文件实现BeanPostProcessor 接口

    @Service
    public class UserConfig implements BeanPostProcessor {
    
        @Autowired
        private BeanFactory beanFactory;
    
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) {
            UserInfo userInfo = bean.getClass().getAnnotation(UserInfo.class);
            if (userInfo != null) {
                String name = userInfo.name();
                String age = userInfo.age();
                System.out.println("name:" + name + ",age:" + age);
    
                SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
                StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
                standardEvaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
                String value1 = spelExpressionParser.parseExpression(name).getValue(standardEvaluationContext, String.class);
                String value2 = spelExpressionParser.parseExpression(age).getValue(standardEvaluationContext, String.class);
                System.out.println("value1:" + value1 + ",value2:" + value2);
            }
            // 在初始化之前执行的逻辑
            return bean;
        }
    }
    
    • 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

    实现BeanPostProcessor 接口并重写postProcessBeforeInitialization()方法然后在里面解析这两个属性
    使用spel解析器就可以把值解析出来。这样我们就可以将解析后的值随便赋值了,我们启动看看效果。

    name:@user.name,age:@user.age
    value1:myFlow,value2:20
    
    • 1
    • 2

    可以看到值成功取到了

  • 相关阅读:
    2022杭电多校第八场
    excel提取单元格中的数字
    免费开箱即用微鳄OA办公系统
    WEB前端网页设计 HTML网页代码 基础参数(二)
    Linux安装Oracle19c(极简版)
    Pytorch 基于VGG-16的服饰识别(使用Fashion-MNIST数据集)
    赋能工业数字化转型|辽宁七彩赛通受邀出席辽宁省工业互联网+安全可控先进制造业数字服务产业峰会
    【从零开始学习 UVM】1.1、UVM 概述 —— Preface
    阿里巴巴架构实战:SpringBoot+SpringCloud+Docker+Nginx+分布式
    【2023全网最全教程】web自动化测试入门
  • 原文地址:https://blog.csdn.net/loli_kong/article/details/138197962