spring属性注入方式样例

前言:本次测试目的使用反射方式去赋值一个类的属性,模仿spring注入原理,进一步加深spring,IOC的实现方式理解。

一、普通方式赋值类属性

  1. 定义一个userService类
public class UserService {


  private TestService testService;

    public TestService getTestService() {
        return testService;
    }

    public void setTestService(TestService testService) {
        this.testService = testService;
    }
}
  1. 定义一个注入的属性testservice
public class TestService {
}
  1. 写一个test测试类测试,如果getTestService成功能把对象地址打印出来证明对象注入成功,完成测试。
public class ExamTest {
    public static void main(String[] args) throws Exception {
        // 获取当前对象
        UserService userService = new UserService();
        Class usercla = userService.getClass();
        // 创建属性对象
        TestService testService = new TestService();
        //获取对象的属性
        Field field =usercla.getDeclaredField("testService");
        //设置可访问
        field.setAccessible(true);
        //拼接method名字
        String name =field.getName();
        name =name.substring(0,1).toUpperCase()+name.substring(1,name.length());
        String methodName="set"+name;
        //调用setTestService方法
        Method method =usercla.getMethod(methodName,TestService.class);
        //完成属性注入
        method.invoke(userService,testService);
        System.out.println(userService.getTestService());
    }
}
  1. 输出结果如图
    在这里插入图片描述

二、自定义注解方式赋值类属性

  1. 定义一个autowire注解
//编译期运行
@Retention(RetentionPolicy.RUNTIME)
//作用域是属性
@Target(ElementType.FIELD)
public @interface Autowire {
}
  1. 给userService加上注解
public class UserService {
    @Autowire
  private TestService testService;

    public TestService getTestService() {
        return testService;
    }

    public void setTestService(TestService testService) {
        this.testService = testService;
    }
}
  1. 新建一个测试类,并且判断注解完成注入。
public class ExamTest1 {

    public static void main(String[] args) throws Exception {
        // 获取当前对象
        UserService userService = new UserService();
        Class usercla = userService.getClass();
        // 创建属性对象
        TestService testService = new TestService();

        Field[] fields=usercla.getDeclaredFields();
        //循环获取对象的属性
        for (Field f:fields
             ) {
           Autowire an =f.getAnnotation(Autowire.class);
           //判断属性是否加上了注解
           if(an!=null){
               f.setAccessible(true);
               //实例化一个对象使用构造方法
              Class type= f.getType();
              Constructor c= type.getConstructor();
             Object o= c.newInstance();
             //对对象进行属性的注入。
             f.set(userService,o);
           }
        }
        //输出属性的地址值,证明已经给对象注入了属性
        System.out.println(userService.getTestService());
    }
}
  1. 最后输出结果截图
    在这里插入图片描述
    总结:使用了两种方式注入属性,模拟spring注入过程,第二种方式不用自己构造setmethod方法,可以直接自己生成属性注入,更方便灵敏。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值