スプリング注釈駆動型プロパティの割り当て(B)

スプリング注釈駆動型プロパティの割り当て(B)


春の注釈駆動型でプロパティの割り当て方法を探ります。紙のテストはSpringBoot上で実施しました。

@valueコメント

@Componentで、Personクラスを書くには、コンポーネントとしてマーク。

package com.example.demo.annotation;

import lombok.Data;

@Data
@Component
public class Person {
    public  String name;

    public  Integer age;

}

ユニットテスト印刷この人を書きます。

package com.example.demo;

import com.example.demo.annotation.Person;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
class annotationTest {

    @Autowired
    Person person;

    @Test
    void contextLoads() {

        System.out.println(person);
    }
}

:結果は以下の通りである。
ここに画像を挿入説明
彼らはNULLであるように、名前と年齢が2つのプロパティに割り当てられていないので。

プロパティの割り当てに直接、次の@valueのコメントを使用してください。

@Value("张三")
public  String name;

@Value("#{20}")
public  Integer age;

結果は以下の通りであります:
ここに画像を挿入説明

そして、それを行う方法を、構成ファイル内の値を読むには?
グローバルコンフィギュレーション・ファイルの最初のapplication.yml割り当て、それらをクラスパスをファイル:

person:
  name: 李四
  age: 30

注釈@valueグローバルコンフィギュレーション・ファイル内の値をとります。
次のようにサンプルコードは次のとおりです。

package com.example.demo.annotation;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Data
@Component
public class Person {

    @Value("${person.name}")
    public  String name;

    @Value("${person.age}")
    public  Integer age;

}

結果は以下の通りであります:
ここに画像を挿入説明

@ConfigurationPropertiesコメント

時には属性が多すぎる、あまりにも面倒つの割り当てずつ。@ConfigurationProperties注釈は、マップ・コンポーネントにコンフィギュレーションファイル内の各属性の値であってもよいです。

package com.example.demo.annotation;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    public  String name;

    public  Integer age;

}

結果を確認するために、再度実行します
ここに画像を挿入説明
ので、以上の全体マップの値に設定ファイルを配置しました。

@PropertySourceコメント

あなたはデフォルトのグローバル・コンフィギュレーション・ファイル、それに加えて、他のファイルのプロパティ値を読みたい場合はどうしますか?@PropertySourceアノテーションは、カスタム設定ファイルを指定するために使用することができます。
クラスperson.propertiesパスに新しい設定ファイルをここに。なぜ.propertiesファイルは、それを提出しますか?2020年1月の終わりのように、PropertySourceノート@まだ.ymlファイルをサポートしていませんので。

person.propertiesプロフィール:

person.name=王五
person.age=50

Personクラス:

package com.example.demo.annotation;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Data
@Component
@PropertySource(value = {"classpath:person.properties"})
@ConfigurationProperties(prefix = "person")
public class Person {

    public  String name;

    public  Integer age;

}

テスト結果:
ここに画像を挿入説明
それはことは注目に値する、私たちが最初に新しい.propertiesファイルの設定ファイルのエンコーディングの前に設定されている、あるいは中国が文字化けします。
次の設定への参照:
ここに画像を挿入説明

公開された17元の記事 ウォンの賞賛1 ビュー303

おすすめ

転載: blog.csdn.net/weixin_43424932/article/details/104079431
おすすめ