我在 Spring bean 配置方面遇到两个主要问题: 1. 当我从应用程序上下文中检索 bean 时,我通过属性文件读取的属性不会保留。 2. 我无法弄清楚如何将一个 bean 的值设置为另一个 bean - 在下面的示例中,我试图将 Plan bean 的 Metric 属性设置为某个 bean。
我有两个小类、一个指标和一个较大的计划:
@Component
public class Metric {
@Value("${nameOfMetric}")
String name;
@Value("${empID}")
String empID;
@Value("${value:0.0}")
Double value;
}
还有一个更大的 Plan 对象,它具有三个不同的 Metrics 作为属性:
public class Plan {
public Metric hoursWorked;
public Metric monthlyDeals;
public Metric monthlyGoal;
...}
我还有一个正在读取的属性文件:
nameOfMetric = Untitled Metric
empID = Unknown
hoursWorked = 120.0
monthlyDeals = 40.0
monthlyGoal = 100.0
有没有办法让我为每个指标(hoursWorked、monthlyDeals、monthlyGoal)创建 bean,然后将每个 bean 注入(inject) Plan bean 中?到目前为止,我尝试使用带有 @Configuration 注释的 PropertyConfig 类:
@Inject
private Environment environment;
private Metric injectMetric(String propertyName){
Metric metric = new Metric();
metric.setEmpID(environment.getProperty("empID"));
metric.setName(propertyName);
metric.setValue(Double.parseDouble(environment.getProperty(propertyName)));
System.out.println("Metric injected: " + metric.toString());
return metric;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean(name = {"hoursWorkedBean"})
@Scope("prototype")
public Metric hoursWorked() {
Metric metric = injectMetric("hoursWorked");
return metric;
}
但是当我尝试访问此 bean 时,我在主应用程序中得到默认值或 null 值,这表明这些属性尚未保留:
Metric hoursWorked = (Metric) ctx.getBean("hoursWorkedBean");
System.out.println(hoursWorked.getName()); // prints out "Untitled Metric", but should print out "hoursWorked"
最佳答案
您可以在 Spring Boot 应用程序中使用 @ConfigurationProperties。
您可以在 Spring Boot 应用程序中进行以下配置:
资源/application.yml
metrics:
hoursWorked:
name: one
empID: 1
value: 1.23
monthlyDeals:
name: two
empID: 2
value: 2.34
monthlyGoal:
name: three
empID: 3
value: 3.45
Metric.java(不要忘记 getters/setters)
public class Metric {
private String name;
private String empID;
private Double value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmpID() {
return empID;
}
public void setEmpID(String empID) {
this.empID = empID;
}
public Double getValue() {
return value;
}
public void setValue(Double value) {
this.value = value;
}
@Override
public String toString() {
return "Metric{" +
"name='" + name + '\'' +
", empID='" + empID + '\'' +
", value=" + value +
'}';
}
}
Metrics.java(定义指标名称/限定符/配置)
public class Metrics {
public static final String HOURS_WORKED = "hoursWorked";
public static final String MONTHLY_DEALS = "monthlyDeals";
public static final String MONTHLY_GOAL = "monthlyGoal";
}
MetricsConfiguration.java(创建 bean 并从配置中初始化它们)
@Configuration
public class MetricsConfiguration {
@Bean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Qualifier(Metrics.HOURS_WORKED)
@ConfigurationProperties(prefix = "metrics." + Metrics.HOURS_WORKED)
public Metric hoursWorked() {
return new Metric();
}
@Bean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Qualifier(Metrics.MONTHLY_DEALS)
@ConfigurationProperties("metrics." + Metrics.MONTHLY_DEALS)
public Metric monthlyDeals() {
return new Metric();
}
@Bean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Qualifier(Metrics.MONTHLY_GOAL)
@ConfigurationProperties("metrics." + Metrics.MONTHLY_GOAL)
public Metric monthlyGoal() {
return new Metric();
}
}
DemoRunner.java(用于测试代码的命令行运行程序)
@Component
public class DemoRunner implements CommandLineRunner
{
@Autowired
private List<Metric> allMetrics;
@Autowired
@Qualifier(Metrics.HOURS_WORKED)
private Metric hoursWorkedMetric;
@Autowired
@Qualifier(Metrics.MONTHLY_DEALS)
private Metric monthlyDealsMetric;
@Autowired
@Qualifier(Metrics.MONTHLY_GOAL)
private Metric monthlyGoalMetric;
@Override
public void run(String... args) throws Exception {
System.out.println();
System.out.println("All metrics:");
allMetrics.stream().forEach(System.out::println);
System.out.println();
System.out.println("HOURS_WORKED metric: " + hoursWorkedMetric);
System.out.println("MONTHLY_DEALS metric: " + monthlyDealsMetric);
System.out.println("MONTHLY_GOAL metric: " + monthlyGoalMetric);
System.out.println();
}
}
DemoApplication.java(SpringBoot应用程序初始化)
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
关于java - 在 Java Spring 中将一个 bean 的值设置为另一个 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43363645/