Spring注記駆動開発第11節デフォルト付与注記


Spring注記駆動開発第11節デフォルト付与注記


beanがspringコンテナに注入されたときに属性がデフォルト値に入力される方法
@Component
public class Dog {
    private String name;
    private String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

まずDogクラスを作成し、set getメソッドとtoStringメソッドがあります.
@Configuration
//@Import(MyImportSelector.class)
@ComponentScan("com.meng")
public class MainConfig {

}

次に構成クラスでパケットをスキャンし、Dogをspringコンテナに注入します.
	@Test
    public void test001(){
        ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        String[] names = context.getBeanDefinitionNames();
        for(String name : names){
            System.out.println(name);
        }
        Dog dog = (Dog) context.getBean("dog");
        System.out.println(dog);
    }

最後にspringコンテナを起動しdogオブジェクトを取得し、印刷結果を表示します.
  23, 2019 9:32:55   org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
 : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4141d797: startup date [Tue Apr 23 21:32:55 CST 2019]; root of context hierarchy
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
dog
Dog{name='null', age='null'}

Process finished with exit code 0

Dogクラスのnameとage属性が付与されていないことがわかりますが、どのように付与しますか?
@Component
public class Dog {
    @Value(" ")
    private String name;
    @Value("#{20-17}")
    private String age;


Dogクラスの属性に@Value注記を加えると属性に値を付けることができ、SpEl操作も可能で、プロファイルの内容も読み取ることができます.これは後でデモを行います.ここでは、1つ目は直接値を割り当て、2つ目はSpElが整数に加算減算乗算を行います.印刷結果の表示:
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
dog
Dog{name=' ', age='3'}

Process finished with exit code 0


Dogクラスの属性はnullではないことがわかります.
次に、プロファイルの値を${}で取得する方法を示します.
@Configuration
//@Import(MyImportSelector.class)
@ComponentScan("com.meng")
@PropertySource(value={"classpath:/dog.properties"})
public class MainConfig {

}

まず、コンフィギュレーションクラスに@PropertySource注記を追加し、コンフィギュレーションファイルのクラスパスを追加し、このコンフィギュレーションファイルをロードします.
@Value(" ")
    private String name;
    @Value("#{20-17}")
    private String age;
    @Value("${dog.sax}")
    private String sax;
    public String getName() {
        return name;
    }
dog.sax= 

次にsax属性に注記${}記号を加えると、プロファイルから取得したkeyがdogであることを示す.saxの値を指定し、saxプロパティに割り当てます.印刷結果の表示:
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
dog
Dog{name=' ', age='3', sax=' '}

saxプロパティの値がプロファイルから読み込まれ、割り当てに成功したことがわかります.