一、Spring之组件注册-@Configuration&@Bean给容器中注册组件
xml配置方式
首先我们创建一个实体类Person
public class Person {
private String name;
private Integer age;
private String nickName;
// 省略getter and setter ,tostring ,Constructor
1}
以往,我们在spring的配置文件中注册一个bean,采用以下写法;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<bean id="person1" class="com.atguigu.bean.Person" >
<property name="age" value="24"></property>
<property name="name" value="zhangsan"></property>
</bean>
</beans>
写个测试类
public class MainTest {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Person bean = (Person) applicationContext.getBean("person1");
System.out.println(bean);
}
}
控制台打印结果:
Person [name=zhangsan, age=24, nickName=null]
nickName我们没有注入,所以为null,
可以看到我们已经成功在spring的ioc容器中注入了这个Person对象
采用javaConfig的方式注入
创建一个配置类,作用等同于spring的配置文件
@Configuration //告诉Spring这是一个配置类
public class MainConfig {
//给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id
@Bean("person")
public Person person01(){
return new Person("lisi", 20);
}
}
写一个测试类
public class MainTest {
@SuppressWarnings("resource")
public static void main(String[] args) {
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
// Person bean = (Person) applicationContext.getBean("person");
// System.out.println(bean);
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
Person bean = applicationContext.getBean(Person.class); // 获取Bean,注意这里的容器是AnnotationConfigApplicationContext
System.out.println(bean); // 输出Bean
//获取类型为Person.class的容器中所有的bean,
String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
// 遍历
for (String name : namesForType) {
System.out.println(name);
}
}
}
控制台输出结果,发现已经成功注入了
Person [name=lisi, age=20, nickName=null]
person
通过配置类的形式注入bean,那bean的名字默认为方法名的小写,即person01,如何手动指定bean的名字呢?
@Bean("person") // 指定bean的名字为person,如果不写为方法名的小写。
// 以下两种写法也是可以的
// @Bean(value ="person")
// @Bean(name = "person")
一、Spring之组件注册-@Configuration&@Bean给容器中注册组件的更多相关文章
- 2、组件注册-@Configuration&@Bean给容器中注册组件
2.组件注册-@Configuration&@Bean给容器中注册组件 2.1 创建maven项目 spring-annotation pom.xml文件添加 spring-context 依 ...
- 【Spring注解开发】组件注册-使用@Configuration和@Bean给容器中注册组件
写在前面 在之前的Spring版本中,我们只能通过写XML配置文件来定义我们的Bean,XML配置不仅繁琐,而且很容易出错,稍有不慎就会导致编写的应用程序各种报错,排查半天,发现是XML文件配置不对! ...
- 向Spring容器中注册组件的方法汇总小结
1.通过xml定义 <bean class=""> <property name="" value=""></ ...
- spring 给容器中注册组件的几种方式
1.@Bean 导入第三方的类或包的组件 2.包扫描+组件的标注注解(@ComponentScan: @Controller,@service,@Reponsitory,@Componet), 自己写 ...
- spring注解开发:容器中注册组件方式
1.包扫描+组件标注注解 使用到的注解如下,主要针对自己写的类 @Controller @Service @Repository @Component @ComponentScan 参考 spring ...
- 【Spring注解驱动开发】在@Import注解中使用ImportBeanDefinitionRegistrar向容器中注册bean
写在前面 在前面的文章中,我们学习了如何使用@Import注解向Spring容器中导入bean,可以使用@Import注解快速向容器中导入bean,小伙伴们可以参见<[Spring注解驱动开发] ...
- 【String注解驱动开发】如何按照条件向Spring容器中注册bean?这次我懂了!!
写在前面 当bean是单实例,并且没有设置懒加载时,Spring容器启动时,就会实例化bean,并将bean注册到IOC容器中,以后每次从IOC容器中获取bean时,直接返回IOC容器中的bean,不 ...
- 【String注解驱动开发】面试官让我说说:如何使用FactoryBean向Spring容器中注册bean?
写在前面 在前面的文章中,我们知道可以通过多种方式向Spring容器中注册bean.可以使用@Configuration结合@Bean向Spring容器中注册bean:可以按照条件向Spring容器中 ...
- Spring注解驱动开发04(给容器中注册组件的方式)
给容器中注册组件的方式 1. 组件注解标注 + 包扫描(适用于自己写的类) //控制层组件 @Controller public class PersonController { } //业务逻辑层组 ...
随机推荐
- Flume架构以及应用介绍(转)
在具体介绍本文内容之前,先给大家看一下Hadoop业务的整体开发流程: 从Hadoop的业务开发流程图中可以看出,在大数据的业务处理过程中,对于数据的采集是十分重要的一步,也是不可避免的一步,从而引出 ...
- c语言的可变参数实例
可变参数函数实现的步骤如下: 1.在函数中创建一个va_list类型变量 2.使用va_start对其进行初始化 3.使用va_arg访问参数值 4.使用va_end完成清理工作 接下来我们来实现一个 ...
- ARDUNIO IMU processing姿态数据可视化
https://www.arduino.cn/thread-42852-1-1.html 关键数据打包 float roll, pitch, heading; Serial.print("O ...
- React-Redux常见API
React-Redux是在Redux的基础上,将其大量重复的代码进行了封装. 1. Provider 利用context对象给所有子组件提供store.不再需要在每个组件都引用store. impor ...
- Redis的移库操作
1.Redis默认有16个数据库,一般情况下使用0库: 2.移库操作: 将mysets移到一号库: 通过Redis查看器查看: 通过命令查看:
- sdcf day1 qwq比赛题解
目录 写在前面 A 链接 思路 代码 B 链接 翻译 思路 代码 C 链接 翻译 思路 代码 写在前面 来到夏令营的第一场比赛,全是水题(第一题除外,不过是原题还是之前做过的,而且是并查集的果题,咕咕 ...
- 查全率(Recall),查准率(Precision),灵敏性(Sensitivity),特异性(Specificity),F1,PR曲线,ROC,AUC的应用场景
之前介绍了这么多分类模型的性能评价指标(<分类模型的性能评价指标(Classification Model Performance Evaluation Metric)>),那么到底应该选 ...
- SQL基础-操纵表及插入、查询
一.操纵表 1.表的关键信息 2.更新表名 更新表名:使用RENAME TABLE关键字.语法如下: RENAME TABLE 旧表名 TO 新表名; 比如,生产环境投产前备份teacher表,使用如 ...
- 基于Linux(中标麒麟)上QT的环境搭建——解决cannot find lGL的问题
接上一篇,QT在中标麒麟环境安装完成后遇到运行报错的问题 一.问题描述: 在中标麒麟上配置好QT的环境后,新建一个工程,不做其他的任何操作,直接运行都会报cannot find lGL的错误.如图所示 ...
- linux df 日志删除命令分析
在部署文件的时候 发现 文件太多了,需要删除: 使用命令行df -h; [sankuai@set-gh-qcs-regulation-wanganbu-test01 com.sankuai.qcs.r ...