对属性赋值的四种方式

一:带有属性的类,属性类,注解

①:带有属性的类

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 {
}

二:对属性赋值的四种测试类

①:构造器
②:反射调用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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值