Spring -> 函数式注册对象,注入到ioc容器中,不使用注解xml方式,仅仅是用方法注入

1.要注入的类

package test10month.test1018;

/**
 * 功能描述:
 * @ClassName Addd
 * @Author: 罗德
 * @Create: 2020-10-18 16:57
 * @Version 1.0
 */
public class Addd {
  private int i;

  @Override
  public String toString() {
    return "Addd{" + "i=" + i + '}';
  }

  public int getI() {
    return i;
  }

  public void setI(int i) {
    this.i = i;
  }
}

2. 函数式注册对象

package test10month.test1018;

import org.springframework.context.support.GenericApplicationContext;

/**
 * 功能描述: 创建对象交给Spring管理
 * @ClassName Null
 * @Author: 罗德
 * @Create: 2020-10-18 16:17
 * @Version 1.0
 */
public class Null {
  public static void main(String[] args) {
    //用来创建对象的类
    GenericApplicationContext context = new GenericApplicationContext();
    //刷新
    context.refresh();
    /**
     * 从给定的bean类注册一个bean,
     * 使用给定的供应商获取一个新实例(通常声明为lambda表达式或方法引用),
     * 可以选择自定义其bean定义元数据(通常也声明为lambda表达式)。
     * 放在sprong ioc容器中
     */
    context.registerBean(Addd.class, Addd::new);
    //从ioc容器中获取一个类的bean
    Addd bean = context.getBean(Addd.class);
    System.out.println(bean);
    //Addd{i=0}
  }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值