属性に値を割り当てる4つの方法


一:属性付きクラス、属性クラス、注釈
①:属性付きクラス
package com.it.test;

import com.it.Autowired;

public class HelloController {

    @Autowired
    private HelloService helloService;

    public HelloController() {
    }

    public HelloController(HelloService helloService) {
        System.err.println("            "+helloService);
        this.helloService = helloService;
    }

    public HelloService getHelloService() {
        return helloService;
    }

    public void setHelloService(HelloService helloService) {
        System.err.println("    set       "+helloService);
        this.helloService = helloService;
    }


}


②:属性類
package com.it.test;

public class HelloService {
}


③:注記
package com.it;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Autowired {
}


二:属性に値を割り当てる4種類のテストクラス
①:コンストラクタ②:反射呼び出しsetメソッド(反射呼び出しsetメソッド、最終的にはsetメソッドを呼び出す)③:反射は注釈設定④:直接呼び出しsetメソッド
package com.it.test;

import com.it.Autowired;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.stream.Stream;

@Slf4j
public class TestReflect {
    public static void main(String[] args){

        System.err.println("--------------------     --------------------------");
        HelloService helloService = new HelloService();
        HelloController helloController = new HelloController(helloService);


        System.err.println("--------------      set  ----------------");
        HelloController helloControllerNoCons = new HelloController();
        Class extends HelloController> aClass = helloControllerNoCons.getClass();
        HelloService helloService1 = new HelloService();
        try {
            Field field = aClass.getDeclaredField("helloService");
            String fieldName = field.getName();
            fieldName = fieldName.substring(0,1).toUpperCase()+ fieldName.substring(1,fieldName.length());
            field.setAccessible(true);
            String methodName = "set"+ fieldName;
            Method method = aClass.getMethod(methodName, HelloService.class);
            method.invoke(helloControllerNoCons,helloService1);
            System.err.println("        set       :"+helloControllerNoCons.getHelloService());
        } catch (Exception e) {
            log.error("      set      :"+e.getMessage());
        }


        System.err.println("--------------          ----------------");
        HelloController helloControllerNoConsVar = new HelloController();
        Class extends HelloController> aClass1 = helloControllerNoCons.getClass();
        try {
            Stream.of(aClass1.getDeclaredFields()).forEach(field -> {
                Autowired annotation = field.getAnnotation(Autowired.class);
                if(null != annotation){
                    Class> type = field.getType();
                    try {
                        Object instance = type.newInstance();
                        field.setAccessible(true);
                        field.set(helloControllerNoConsVar,instance);
                        System.err.println("                  :"+helloControllerNoConsVar.getHelloService());
                    } catch (Exception e) {
                        log.error("               :"+e.getMessage());
                    }
                }
            });



        } catch (Exception e) {
            log.error("      set      :"+e.getMessage());
        }


        System.err.println("-----------------------    set  --------------------------------");
        HelloController helloControllerNoConsVar1 = new HelloController();
        HelloService helloService3 = new HelloService();
        helloControllerNoConsVar1.setHelloService(helloService3);



    }
}


三:テスト結果
--------------------     --------------------------
            com.it.test.HelloService@1e0aca6
--------------      set  ----------------
    set       com.it.test.HelloService@1906a77
        set       :com.it.test.HelloService@1906a77
--------------          ----------------
                  :com.it.test.HelloService@163006a
-----------------------    set  --------------------------------
    set       com.it.test.HelloService@1be847c