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

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

先看两个类
application.properties

xml 复制代码
server.port=8080
flow.name=myFlow
flow.age=20
java 复制代码
@Component
@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface UserInfo {

    String name() default "";

    String age() default "18";

}

我们现在将application.properties中的flow.name和flow.age赋值到UserInfo注解的name和age中,应该怎么做呢?具体见下面代码,

先定义一个User类获取properties里面的值

java 复制代码
@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;
    }
}

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

java 复制代码
@UserInfo(name = "@user.name", age = "@user.age")
@Service
public class UserServiceImpl implements UserService {
}

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

java 复制代码
@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;
    }
}

实现BeanPostProcessor 接口并重写postProcessBeforeInitialization()方法然后在里面解析这两个属性

使用spel解析器就可以把值解析出来。这样我们就可以将解析后的值随便赋值了,我们启动看看效果。

复制代码
name:@user.name,age:@user.age
value1:myFlow,value2:20

可以看到值成功取到了

相关推荐
花酒锄作田2 小时前
[python]argparse 包在聊天机器人中的应用
python
NiceCloud喜云4 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
为思念酝酿的痛5 小时前
POSIX信号量
linux·运维·服务器·后端
小羊在睡觉5 小时前
力扣84. 柱状图中最大的矩形
后端·算法·leetcode·golang·go
AI玫瑰助手5 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
weixin_468466855 小时前
全局与局部注意力机制新手实战指南
人工智能·python·深度学习·算法·自然语言处理·transformer·注意力机制
小糖学代码5 小时前
LLM系列:环境搭建:5.Python-dotenv 环境变量管理
人工智能·python·深度学习·神经网络
swipe5 小时前
Neo4j + Graph RAG 医疗知识图谱工程实践:患者教育问答真正需要的是“关系可追溯”
后端·langchain·llm
智慧物业老杨6 小时前
智慧物业合同周期管理系统:从风险预警到智能交接的全流程数智化落地方案
java·人工智能·python
橙橙笔记6 小时前
Python的学习第一部分
python·学习