Spring入门系列 小白跟着一起学 (10)

文章介绍了Spring中Bean的管理,包括单例和原型scope的区别,以及@PostConstruct和@PreDestroy注解在生命周期中的作用。接着讨论了依赖注入的概念,通过实例展示了自动装配的工作原理,并提到了@Repository、@Service的使用。此外,文章还提到了简单类型的注入,如使用@Value注解注入配置文件中的属性值。
摘要由CSDN通过智能技术生成

学前要求:已学完前9个系列

bean的管理

在前一系列配置类的加持下,新建一个Java文件

@Component("testDemo19")
@Scope("singleton") // 单例 默认设置
public class TestDemo19 {
    // nothing~
}

再通过实例看看效果

public class App16 {
    public static void main(String[] args) {
        // 加载配置类初始化容器
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);

        TestDemo19 testDemo19 = (TestDemo19) ctx.getBean("testDemo19");
        System.out.println(testDemo19);
        TestDemo19 testDemo191 = (TestDemo19) ctx.getBean("testDemo19");
        System.out.println(testDemo191);
    }
}

输出结果
在这里插入图片描述
确实正确,是单例
稍作修改,将@Scope(“singleton”)改为@Scope(“prototype”)
运行结果:
在这里插入图片描述

生命周期

重新调换成单例,外加上init和destory函数和注解

@Component("testDemo19")
@Scope("singleton")  // @Scope("singleton") 单例(默认)  @Scope("prototype") 非单例
public class TestDemo19 {
    // nothing~
    @PostConstruct
    public void init() {
        System.out.println("init...");
    }
    @PreDestroy
    public void destory() {
        System.out.println("destory...");
    }
}

运行类也有稍微变化,毕竟得看到ctx.close函数

public class App16 {
    public static void main(String[] args) {
        // 加载配置类初始化容器
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);

        TestDemo19 testDemo19 = (TestDemo19) ctx.getBean("testDemo19");
        System.out.println(testDemo19);

        ctx.close();
    }
}

运行结果:
在这里插入图片描述

可以发现,当我们从XML配置文件过渡到注解开发的过程就是不断的采用不同的方式将以前学过的内容重现,接下来便是依赖注入

依赖注入

自动装配
新建文件

@Repository
public class TestDemo20 {
    public void say() {
        System.out.println("TestDemo20 ...");
    }
}

再新建

@Service
public class TestDemo21 {
    private TestDemo20 testDemo20;

    public void setTestDemo20(TestDemo20 testDemo20) {
        this.testDemo20 = testDemo20;
    }

    public void speak() {
        System.out.println("TestDemo21 speak!");
        testDemo20.say();
    }
}

试用类

public class App17 {
    public static void main(String[] args) {
        // 加载配置类初始化容器
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);

        TestDemo21 testDemo21 = ctx.getBean(TestDemo21.class);
        testDemo21.speak();

        ctx.close();
    }
}

运行结果
在这里插入图片描述
可以发现TestDemo20成空对象,还差什么呢?
修改如下:
在这里插入图片描述
再看看运行结果
在这里插入图片描述
现在就没有问题了
再看
在这里插入图片描述
注释掉这set方法,运行无异~

还有就是即使这匹配多个@Repository(“testDemo20”),加上名称,再来个类并配置@Repository(“testDemo201”)也会根据本该有的set函数来匹配testDemo20那个类

在这里插入图片描述
里面来了个@Qualifier(“testDemo20”)便可以人工指定使用哪个名称的bean

注意:自动装配基于反射设计创建对象并暴力反射对应属性为私有属性初始化数据,因此无需提供setter方法

简单类型的注入

配置类中加上一行代码
在这里插入图片描述
哦,对了,配置文件jdbc.properties(就先简单加上简简单单一行代码)

name = wangdashuai

再看如下

@Repository
public class TestDemo20 {
//    @Value("名称") // 这也就配置了名称name的值
    @Value("${name}")
    private String name;
    public void say() {
        System.out.println("TestDemo20 ..."+name);
    }
}

运行便可使用

在配置类的路径仅支持单一文件配置,多文件请使用数组格式配置,不允许使用通配符*

总结

那么学到这我们就已经掌握了bean的生命周期,依赖注入等

我是哈利巴多先生,如果觉得不错,还望多多鼓励(文章不定时更新)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值