内蒙古生产建设兵团四师三十四团知青网站,微信管理系统在哪,做网站带吗,如何做自动交易网站在 Spring 中#xff0c;如果使用 Value 注解注入值#xff0c;不能将其应用于 static 字段。Spring 只能为实例变量注入值#xff0c;不能直接对静态变量进行注入。
使用 PostConstruct 初始化#xff1a;
如果确实需要在静态上下文中使用该值#xff0c;可以使用 Post…在 Spring 中如果使用 Value 注解注入值不能将其应用于 static 字段。Spring 只能为实例变量注入值不能直接对静态变量进行注入。
使用 PostConstruct 初始化
如果确实需要在静态上下文中使用该值可以使用 PostConstruct 方法来设置静态变量
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;Component
public class UserConfig {Value(${upload.user})private String user;private static String staticUser;PostConstructpublic void init() {staticUser user;}public static String getStaticUser() {return staticUser;}
} 现在可以通过 UserConfig.getStaticUser() 来访问 upload.user 的值。