一、Spring之组件注册-@Configuration&@Bean给容器中注册组件
2024-08-21 01:47:01
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")
最新文章
- java 正则匹配括号对以及其他成对出现的模式
- Extjs Panel
- ORA-06502:PL/SQL :numberic or value error: character string buffer too small
- apt-get install *** 出现 软件包***没有提供可供安装的候选者
- Medical Image Processing Conference and Journal 医学图像处理会议与期刊
- CSS那些事儿-阅读随笔1(CSS简介与选择符)
- 再次记录老K站点的工作策略
- java多态的理解----部分非原创
- iOS10 CAAnimationDelegate的适配
- 青年菜君与小农女送菜商业模式PK
- C# 曲线上的点(二) 获取距离最近的点
- 在maven pom.xml中加载不同的properties ,如localhost 和 dev master等jdbc.properties 中的链接不一样
- edu9E. Thief in a Shop
- jquery 设置cookie、删除cookie、获取cookie
- Visual Studio进行Web性能测试- Part II
- Android 网络请求超时处理方案
- iOS开发-NSDictionary
- ADF backing Bean中常用的代码
- 一名优秀的UI设计师应该具备哪些条件?
- 再看Spring Could微服务的关键组件
热门文章
- TED演讲:别不信,你只需20个小时,就能学会任何事情!
- EFK架构图
- create系列创建节点的方法
- maven install
- telegraf 学习三 telegra inputs.net_response + smtp2http+ grafana 进行tcp服务状态监控
- “知乎杯”2018 CCF 大学生计算机系统与程序设计竞赛 绝地求生(battleground)
- 用户画像(User Profile)
- URL的作用是什么?它由几部分组成?
- IDEA中设置自动build-改动代码,不用重启工程,刷新页面即可
- 【算法编程 C++ Python】二维数组查找