Java 如何在配置中向静态bean注入系统属性?

Java 如何在配置中向静态bean注入系统属性?,java,spring,dependency-injection,static-initialization,Java,Spring,Dependency Injection,Static Initialization,在我的Spring@Configuration类中,我希望将系统属性${brand}注入名为brandString的静态字符串bean中。通过使用@PostConstruct和通过@value注入的实例字段值为静态字段分配解决方案,我成功地做到了这一点: @Configuration public class AppConfig { @Value("${brand}") private String brand; private static String brandString;

在我的Spring
@Configuration
类中,我希望将系统属性
${brand}
注入名为
brandString
的静态字符串bean中。通过使用
@PostConstruct
和通过
@value
注入的实例字段值为静态字段分配解决方案,我成功地做到了这一点:

@Configuration
public class AppConfig {

  @Value("${brand}")
  private String brand;
  private static String brandString;

  @PostConstruct
  public void init() {
    brandString = brand;
  }

  @Bean
  public static String brandString() {
    return brandString;
  }

  // public static PropertyPlaceHolderConfigurer propertyPlaceHolderConfigurer() {...}
是否有另一种方法可以静态地将
${brand}
的值注入
brandString
字段,而无需使用另一个“copy”
brand
@PostConstruct
方法来解决此问题?

尝试以下方法:

@Configuration
public class AppConfig {

    private static String brand;

    @Value("${brand}")
    public void setBrand(String brand) {
        AppConfig.brand = brand;
    }

    @Bean
    public static String brandString() {
        return brand;
    }
...
}

伟大的在setter方法上设置
@Value
注释,这到底是如何工作的?您是如何为@Bean方法选择方法名称的??