• 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

    可以看到值成功取到了

  • 相关阅读:
    uview的安装和功能
    Bootstrap-- 栅格系统
    【ISP】噪声--sensor(2)
    【公众号开发】访问第三方接口应用于开发 · 回复图文消息
    css 星星闪烁加载框
    动态规划--(回文子串,最长回文子序列)
    gradle安装配置
    1个月5次发版:测试人的模块测试策略分类归纳
    离子液体(1-乙基-3-甲基咪唑六氟磷酸[EMIM][PF6])负载量不同的多孔纳米氧化硅(SiOx)研究结果
    运维 | 使用 Docker 安装 Jenkins | Jenkins
  • 原文地址:https://blog.csdn.net/loli_kong/article/details/138197962