持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第1天,点击查看活动详情
1. spring 基本概念 bean
Bean是Spring框架中最核心的两个概念之一(另一个是面向切面编程AOP)。
Spring IoC容器管理的对象称为bean。bean是一个由Spring IoC容器实例化、组装和管理的对象。
- bean 是一个对象,一个或多个不限定
- bean 是由spring中一个叫 IOC 的东西管理
- 我们的应用程序由一个个的 bean 构成
IOC:控制反转,不用自己手动去 new 创建,让 spring控制 new 过程。spring 启动时会把所需的类实例化成对象,开发人员只需要去调用就好了。
spring已经创建了这些bean,并且已经加载到spring的容器中去了,现在只需要从spring中调用即可;相当于写代码的时候,service dao,你可以直接通过注解注入然后直接调用,但是一般的其他对象,都需要手动new一下,然后才能操作
2. 使用 xml 配置文件进行
2.1 resource 目录下添加 beanconfig.xml 文件
首先 Person 类
public class Person {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
// public Person(String name, Integer age) {
// this.name = name;
// this.age = age;
// }
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
<?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"
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">
<bean id="person" class="com.shanggushenlong.bean.Person" >
<property name="name" value="张三"></property>
<property name="age" value="25"></property>
</bean>
</beans>
再 xml 文件中增加 bean 标签,使用 <property> 属性给字段注入值;
2.2 加载获取配置文件
@Test
public void testBean01() {
// 加载读取 beanconfig.xml 配置文件
// 使用 ClassPathXmlApplicationContext 类读取,获取 application 应用
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanconfig.xml");
// 从 application 应用中获取 bean,指定名称获取 'person'
Object person = applicationContext.getBean("person");
System.out.println(person);
}
可以从 application 容器中获取到了 bean
3. 通过注解 @Configuration 与 @bean 向容器中注入 bean
3.1 首先创建一个类 config 类
@Configuration
public class MainBeanConfig {
@Bean(value = {"person01"})
public Person person() {
return new Person("张三", 28);
}
}
创建 MainBeanConfig 类上增加注解 @Configuration,表示这是一个配置类,spring 启动时候,会将其当作配置类,配置类等同于xml配置文件;
同时增加 @Bean 注解,表示向IOC容器中注册一个 bean ,类型是返回值类型,id默认用方法名作为id
@Configuration 注解表示这也是一个组件;
3.2 测试
@Test
public void testBean02() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainBeanConfig.class);
Person bean = applicationContext.getBean(Person.class);
System.out.println(bean);
// 获取 bean 的名称
// String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);
// for (String name : beanNamesForType) {
// System.out.println(name);
// }
}
由于不是 xml 配置文件,现在增加的是注解配置,那么使用 ``
可以看到,已经通过注解将 Person 注入到Spring 的 IOC 容器中
现在获取 Bean 的名称
String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);
for (String name : beanNamesForType) {
System.out.println(name);
}
可以看到获取到 person01 的bean,因为@Bean(value = {"person01"}) 取名为 person01,若不添加此属性,那么默认情况下,是以方法名作为 bean 的名称
4. 总结
两种向 spring 容器中注册 bean 的方法
- 通过在 classpath 目录下创建 xml 配置文件,spring 容器会自动扫描
- 通过创建java类,并在类上添加注解
@Configuration,告诉 spring 这是一个配置类,作用等同于 xml 配置文件;并且在类中通过增减@Bean注解,这样就会向 spring 容器中注册 bean,并且默认以方法名为 bean 的名字,也可以直接指定bean的名字 - 获取加载 xml 配置文件,使用 ClassPathXmlApplicationContext
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanconfig.xml")来获取相关配置文件中的bean - 通过配置类注解获取
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainBeanConfig.class)