プロパティ設定ファイルは次のとおりです。
human.name=Mr.Yu
human.age=21
human.gender=male
プロパティの構成をJavaBeanにバインドする方法、以前のアプローチは次のとおりです。
public class PropertiesUtil {
public static void getProperties(Person person) throws IOException {
Properties properties = new Properties();
properties.load(new FileInputStream("src/main/resources/demo.properties"));
//得到配置文件key的名字
Enumeration enumeration = properties.propertyNames();
while (enumeration.hasMoreElements()){
String key =(String)enumeration.nextElement();
String value = properties.getProperty(key);
System.out.println("key="+key+"——————value="+value);
//封装到JavaBean
//以下内容省略
}
}
}
出力結果:
key=human.name——————value=Mr.Yu
key=human.age——————value=21
key=human.gender——————value=male
Process finished with exit code 0
このプロセスは非常に複雑であることがわかりますが、SpringBootではこのプロセスは非常に単純になります。
SpringBootには3つのメソッドがあります。
1. @ConfigurationPropertiesアノテーション+ @ Component(または@Controllerまたは@Serviceまたは@Repository)アノテーション
- コンテナ内のコンポーネントのみがSpringBootによって提供される強力な機能を持ちます。つまり、@ ConfigurationPropertiesアノテーションを使用する必要がある場合は、最初にJavaBeanオブジェクトのペアがIoCコンテナ内にあることを確認する必要があるため、コンポーネントアノテーションを使用してコンポーネントを追加する必要があります。コンテナへ。
- @ConfigurationPropertiesアノテーションでは、プレフィックスと値は相互のエイリアスです
- つまり、同じ
@ConfigurationProperties(value = "human")
@ConfigurationProperties(prefix = "human")
- プレフィックスと値はプレフィックスの意味を参照します
コードテスト:
プロパティ構成ファイル:
human.name=Mr.Yu
human.age=21
human.gender=male
個人クラス:
@Component 只有在容器中的组件,才会拥有SpringBoot提供的强大功能
@ConfigurationProperties(value = "human")
public class Person {
public String name;
public int age;
public String gender;
public Person() {
}
public Person(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
}
テストクラスは次のとおりです。
//@Controller
@ResponseBody以字符串的方式写给浏览器,而不是跳转到某个页面
//@ResponseBody
//@RestController = @Controller + @ResponseBody
@RestController
public class HelloController {
//自动注入属性
@Autowired
Person person;
@RequestMapping("/person")
public Person getPerson(){
return person;
}
}
試験結果:
2. @ ConfigurationPropertiesアノテーション+ @ EnableConfigurationPropertiesアノテーション
このように、@ EnableConfigurationPropertiesアノテーションを構成クラスに書き込む必要があります。@ EnableConfigurationPropertiesは、上記のPersonクラスがバインディングを構成する必要があるため、プロパティ構成関数がオンになっているプロパティ構成関数をオンにすることを意味します。後で追加します。パラメータPerson.class:@EnableConfigurationProperties(Person.class)
@EnableConfigurationProperties(Person.class)の機能は、Personコンポーネントをコンテナに登録することです。オブジェクトの名前は次のとおりです。human-com.ysw.boot.properties.Personここで、humanは@ConfigurationProperties(value = "human")のプレフィックス値です。の値
Personクラスにはまだ@ConfigurationPropertiesアノテーションがあります。このメソッドは、Personクラスの@Componentアノテーションを構成クラスの@EnableConfigurationProperties(Person.class)アノテーションに置き換えます。
このメソッドは主に、最初のパッケージなどのサードパーティパッケージを参照するときに使用されます。サードパーティパッケージにPersonクラスがあります。@ Componentはこのクラスでは使用されません。サードパーティパッケージのクラスに@Componentを追加することはできません。現時点では、構成クラスに@EnableConfigurationPropertiesアノテーションを追加する方法を使用できます。
3. @ConfigurationPropertiesアノテーション+ @ Importアノテーション
- @Importを使用して、このタイプのコンポーネントをコンテナ内に自動的に作成します。デフォルトのコンポーネントの名前は、完全なクラス名です。
- @Importアノテーションは、2の@ConfigurationPropertiesアノテーションと同じ効果があります。
- コンテナに異なる名前が登録されているだけです。
- コンテナ内の@ConfigurationPropertiesアノテーションで登録されたオブジェクトの名前は次のとおりです。human-com.ysw.boot.properties.Personここで、humanは@ConfigurationProperties(value = "human")のプレフィックス値の値です。
- @Importアノテーションが付いたコンテナに登録されているオブジェクトの名前は次のとおりです:com.ysw.boot.properties.Person
構成クラス:
Personクラス:
テストクラス:
application.propertiesファイル:
server.port=8888
human.name=Mr.Yu
human.age=21
human.gender=male222
試験結果: