spring静态字段注入

对于用static修饰的静态字段,直接用@Value("${xxx}")无法注入,需要以下几步:

1.在类上添加@Component注解
2.字段需要有set方法
3.在set方法上使用@Value("${xxx}")

例子:

@Component
public class Sender {


    public static String filePath;

    @Value("${filePath}")
    public void setFilePath(String file_path) {
        filePath = file_path;
    }
}

猜你喜欢

转载自blog.csdn.net/hqqqqqqq555/article/details/106680939