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

可以看到值成功取到了

相关推荐
_.Switch29 分钟前
Python机器学习模型的部署与维护:版本管理、监控与更新策略
开发语言·人工智能·python·算法·机器学习
Hoper.J1 小时前
PyTorch 模型保存与加载的三种常用方式
人工智能·pytorch·python
千里码aicood1 小时前
【2025】springboot教学评价管理系统(源码+文档+调试+答疑)
java·spring boot·后端·教学管理系统
程序员-珍1 小时前
使用openapi生成前端请求文件报错 ‘Token “Integer“ does not exist.‘
java·前端·spring boot·后端·restful·个人开发
弱冠少年1 小时前
websockets库使用(基于Python)
开发语言·python·numpy
liuxin334455662 小时前
教育技术革新:SpringBoot在线教育系统开发
数据库·spring boot·后端
技术无疆2 小时前
【Python】Streamlit:为数据科学与机器学习打造的简易应用框架
开发语言·人工智能·python·深度学习·神经网络·机器学习·数据挖掘
架构师吕师傅2 小时前
性能优化实战(三):缓存为王-面向缓存的设计
后端·微服务·架构
羊小猪~~2 小时前
机器学习/数据分析--用通俗语言讲解时间序列自回归(AR)模型,并用其预测天气,拟合度98%+
人工智能·python·机器学习·数据挖掘·数据分析·回归·时序数据库
qq_273900232 小时前
解析TMalign文本文件中的转换矩阵
python·生物信息学