正如评论中所述,可以通过在配置文件中存储位置详细信息,然后在Spring Bean初始化时注入它们来实现此操作。
假设您的application.properties
文件如下:
my.sample.config.A=somelocationA
my.sample.config.B=somelocationB
my.sample.config.C=somelocationC
my.sample.config.D.one=somelocationD1
my.sample.config.D.two=somelocationD2
以下是4种实现方法的演示:
1. 通过在Bean方法创建时直接注入属性:
@Bean
public A myBeanA(@Value("${my.sample.config.A}") String myprop) {
System.out.println("from bean A with " + myprop);
return new A(myprop);
}
2.通过在全局变量上注入属性,并在Bean方法创建中使用它:
@Value("${my.sample.config.B}")
private String mylocationB;
@Bean
public B myBeanB() {
System.out.println("from bean B with " + mylocationB);
return new B(mylocationB);
}
3.通过将整个环境注入配置中,然后手动选择所需的属性:
@Autowired
private Environment env;
@Bean
public C myBeanC() {
String locationC = env.getProperty("my.sample.config.C");
System.out.println("from bean C with " + locationC);
return new C(locationC);
}
4.这是Spring Boot的专属方法。您可以使用类型安全的配置属性,通过在您的bean上注释@ConfigurationProperties
来定义前缀命名空间,从该点开始的所有参数都将自动映射到该bean中定义的属性!
@ConfigurationProperties(prefix = "my.sample.config.D")
@Component
class D {
private String one;
private String two;
public String getOne() { return one; }
public void setOne(String one) {
System.out.println("from bean D with " + one);
this.one = one;
}
public String getTwo() { return two; }
public void setTwo(String two) {
System.out.println("from bean D with " + two);
this.two = two;
}
}
以下是整个代码的单文件版本:
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@SpringBootApplication
public class DemoApplication {
@Autowired
private Environment env;
@Value("${my.sample.config.B}")
private String mylocationB;
@Bean
public A myBeanA(@Value("${my.sample.config.A}") String myprop) {
System.out.println("from bean A with " + myprop);
return new A(myprop);
}
@Bean
public B myBeanB() {
System.out.println("from bean B with " + mylocationB);
return new B(mylocationB);
}
@Bean
public C myBeanC() {
String locationC = env.getProperty("my.sample.config.C");
System.out.println("from bean C with " + locationC);
return new C(locationC);
}
@ConfigurationProperties(prefix = "my.sample.config.D")
@Component
class D {
private String one;
private String two;
public String getOne() { return one; }
public void setOne(String one) {
System.out.println("from bean D with " + one);
this.one = one;
}
public String getTwo() { return two; }
public void setTwo(String two) {
System.out.println("from bean D with " + two);
this.two = two;
}
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
class A {
private final String location;
public A(String location) { this.location = location; }
}
class B {
private final String location;
public B(String location) { this.location = location; }
}
class C {
private final String location;
public C(String location) { this.location = location; }
}
}