【Spring】——1、使用@Configuration和@Bean给容器中注册组件
创始人
2024-01-26 05:14:01
0

在这里插入图片描述

📫作者简介:zhz小白
公众号:小白的Java进阶之路
专业技能:
1、Java基础,并精通多线程的开发,熟悉JVM原理
2、熟悉Java基础,并精通多线程的开发,熟悉JVM原理,具备⼀定的线上调优经验
3、熟悉MySQL数据库调优,索引原理等,⽇志原理等,并且有出过⼀篇专栏
4、了解计算机⽹络,对TCP协议,滑动窗⼝原理等有⼀定了解
5、熟悉Spring,Spring MVC,Mybatis,阅读过部分Spring源码
6、熟悉SpringCloud Alibaba体系,阅读过Nacos,Sentinel,Seata,Dubbo,Feign,Gateway核⼼源码与设计,⼆次开发能⼒
7、熟悉消息队列(Kafka,RocketMQ)的原理与设计
8、熟悉分库分表ShardingSphere,具有真实⽣产的数据迁移经验
9、熟悉分布式缓存中间件Redis,对其的核⼼数据结构,部署架构,⾼并发问题解决⽅案有⼀定的积累
10、熟悉常⽤设计模式,并运⽤于实践⼯作中
11、了解ElasticSearch,对其核⼼的原理有⼀定的了解
12、了解K8s,Jekins,GitLab
13、了解VUE,GO
14、⽬前有正在利⽤闲暇时间做互游游戏,开发、运维、运营、推销等

本人著作git项目:https://gitee.com/zhouzhz/star-jersey-platform,有兴趣的可以私聊博主一起编写,或者给颗star
领域:对支付(FMS,FUND,PAY),订单(OMS),出行行业等有相关的开发领域
🔥如果此文还不错的话,还请👍关注、点赞、收藏三连支持👍一下博主~

文章目录

  • Spring IOC和DI
  • 通过XML配置文件注入JavaBean
  • 通过注解注入JavaBean
  • 总结

Spring IOC和DI

在Spring容器的底层,最重要的功能就是IOC和DI,也就是控制反转和依赖注入。
在这里插入图片描述
DI和IOC它俩之间的关系是DI不能单独存在,DI需要在IOC的基础上来完成。
在Spring内部,所有的组件都会放到IOC容器中,组件之间的关系通过IOC容器来自动装配,也就是我们所说的依赖注入。接下来,我们就使用注解的方式来完成容器中组件的注册、管理及依赖、注入等功能。

在介绍使用注解完成容器中组件的注册、管理及依赖、注入等功能之前,我们先来看看使用XML配置文件是如何注入bean的。

通过XML配置文件注入JavaBean

首先,新建一个maven项目,例如spring-annotation-learn,注意其打包方式是jar。
在maven依赖中添加:

org.springframeworkspring-context5.3.23org.projectlomboklombok1.18.24

在这里插入图片描述

接着,在工程的com.zhz.bean包下创建一个Person类,作为测试的JavaBean,代码如下所示。

package com.zhz.bean;import lombok.*;/*** @author zhouhengzhe* @description: 实体类* @date 2022/11/4 10:12* @since v1*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
public class Person {private String name;private Integer age;
}

紧接着,在工程的src/main/resources目录下创建Spring的配置文件,例如beans.xml,通过该配置文件将Person类注入到Spring的IOC容器中,该配置文件中的内容如下所示。(我这里一次性全部导入,后面就不需要导入了)


4.0.0org.examplespring-annotation-learn1.0-SNAPSHOT885.3.23org.springframeworkspring-context${spring.version}org.springframeworkspring-core${spring.version}org.projectlomboklombok1.18.24org.springframeworkspring-beans${spring.version}org.springframeworkspring-aop${spring.version}org.springframeworkspring-aspects${spring.version}org.aspectjaspectjrt1.9.9.1org.springframeworkspring-tx${spring.version}org.springframeworkspring-web${spring.version}org.springframeworkspring-webmvc${spring.version}org.springframeworkspring-test${spring.version}junitjunit4.13.2testcom.alibabatransmittable-thread-local2.14.2

至此,我们使用XML配置文件的方式注入JavaBean就完成了。
接下来,我们在工程的com.zhz包下创建一个MainTest类来进行测试,该类的代码如下所示。

package com.zhz;import com.zhz.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @author zhouhengzhe* @description: 测试类* @date 2022/11/4 10:15* @since v1*/
public class MainTest {public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-bean.xml");Person person = (Person) applicationContext.getBean("person");System.out.println(person);}
}

在这里插入图片描述

通过注解注入JavaBean

通过XML配置文件的方式,我们可以将JavaBean注入到Spring的IOC容器中。那使用注解又该如何实现呢?别急,其实使用注解比使用XML配置文件要简单的多,我们在项目的com.meimeixia.config包下创建一个MainConfig类,并在该类上添加@Configuration注解来标注该类是一个Spring的配置类,也就是告诉Spring它是一个配置类,最后通过**@Bean注解将Person类注入到Spring的IOC**容器中。

package com.zhz.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import com.zhz.bean.Person;
/*** 以前配置文件的方式被替换成了配置类,即配置类==配置文件* @author zhz**/
// 这个配置类也是一个组件 
@Configuration // 告诉Spring这是一个配置类
public class MainConfig {// @Bean注解是给IOC容器中注册一个bean,类型自然就是返回值的类型,id默认是用方法名作为id@Beanpublic Person person() {return new Person("zhz", 20);}}

没错,通过MainConfig类我们就能够将Person类注入到Spring的IOC容器中,是不是很Nice啊!!主要是我们在类上加上@Configuration注解,并在方法上加上@Bean注解,就能够将方法中创建的JavaBean注入到Spring的IOC容器中。

然后,我们修改MainTest类中的main方法,以测试通过注解注入的Person类,如下所示。

package com.zhz;import com.zhz.bean.Person;
import com.zhz.config.MainConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @author zhouhengzhe* @description: 测试类* @date 2022/11/4 10:15* @since v1*/
public class MainTest {public static void main(String[] args) {
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-bean.xml");
//        Person person = (Person) applicationContext.getBean("person");
//        System.out.println(person);ApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfig.class);Person person = annotationConfigApplicationContext.getBean(Person.class);System.out.println(person);}
}

在这里插入图片描述

到这里,我们已经明确了,通过XML配置文件和注解这两种方式都可以将JavaBean注入到Spring的IOC容器中。那么,使用注解将JavaBean注入到IOC容器中时,使用的bean的名称又是什么呢?我们可以在MainTest类的main方法中添加如下代码来获取Person这个类型的组件在IOC容器中的名字。

// Person这个类型的组件在IOC容器中的名字是什么呢?
String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
for (String name : namesForType) {System.out.println(name);
}

这样,MainTest类的main方法的完整代码如下所示。

package com.zhz;import com.zhz.bean.Person;
import com.zhz.config.MainConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @author zhouhengzhe* @description: 测试类* @date 2022/11/4 10:15* @since v1*/
public class MainTest {public static void main(String[] args) {
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-bean.xml");
//        Person person = (Person) applicationContext.getBean("person");
//        System.out.println(person);ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);Person person = applicationContext.getBean(Person.class);System.out.println(person);String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);for (String s : beanNamesForType) {System.out.println(s);}}
}

在这里插入图片描述

我们可以思考一下person是什么?我们修改下MainConfig中的person()方法的名字,改成person1,如下:

package com.zhz.config;import com.zhz.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author zhouhengzhe* @description: todo* @date 2022/11/4 10:27* @since v1*/
@Configuration
public class MainConfig {@Beanpublic Person person1(){return new Person("zhz", 20);}
}

再次运行MainTest,发现名字变了,所以可以得知,其拿的是方法名做beanName(Bean的名字)
在这里插入图片描述

我们可以想一想@Bean里面是不是也可以命名呢,如下

package com.zhz.config;import com.zhz.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author zhouhengzhe* @description: todo* @date 2022/11/4 10:27* @since v1*/
@Configuration
public class MainConfig {/*** @Bean注解是给IOC容器中注册一个bean,类型自然就是返回值的类型,id默认是用方法名作为id*/@Bean(name = "person")public Person person1(){return new Person("zhz", 20);}
}

运行MainTest可知:确实可以改变。
在这里插入图片描述

总结

我们在使用注解方式向Spring的IOC容器中注入JavaBean时,如果没有在**@Bean注解中明确指定bean的名称**,那么就会使用当前方法的名称来作为bean的名称;如果在**@Bean注解中明确指定了bean的名称**,那么就会使用**@Bean注解中指定的名称来作为bean的名称**。

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...