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

可以看到值成功取到了

相关推荐
风象南几秒前
SpringBoot的3种六边形架构应用方式
java·spring boot·后端
程序员爱钓鱼1 小时前
Go 网络编程:HTTP服务与客户端开发
后端·google·go
jakeswang5 小时前
一致性框架:供应链分布式事务问题解决方案
分布式·后端·架构
2501_915373885 小时前
Conda 常用命令大全:从入门到高效使用
python
微信公众号:AI创造财富5 小时前
conda create -n modelscope python=3.8 conda: command not found
开发语言·python·conda
没枕头我咋睡觉6 小时前
[python]conda用法笔记
笔记·python·conda
IT艺术家-rookie6 小时前
golang--channel的关键特性和行为
开发语言·后端·golang
心软且酷丶8 小时前
leetcode:263. 丑数(python3解法,数学相关算法题)
python·算法·leetcode
点云SLAM9 小时前
Pytorch中gather()函数详解和实战示例
人工智能·pytorch·python·深度学习·机器学习·计算视觉·gather函数