【spring】01 Spring容器研究

简介: 【spring】01 Spring容器研究
初步了解

spring容器本质上就是一个存放了一个个描述不同对象属性和方法的定义单元,需要使用的时候就通过反射机制根据把对象创建好,再将描述的属性初始化。容器管理着 Bean 的生命周期,控制着 Bean 的依赖注入

架构图

容器之使用@Bean注册组件

第一种:配置文件形式

新建Person类

public class Person {
    private String name;

    private Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Person() {

    }

    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;
    }
}

新建beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="person" class="com.atguigu.bean.Person">
        <property name="age" value="18"></property>
        <property name="name" value="zhangsan"></property>
    </bean>
</beans>

新建MainTest.java

public class MainTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Person bean= (Person) applicationContext.getBean("person");
        System.out.println(bean.getAge());
   }
}

执行结果

可以通过配置文件的形式读取到Person对象

第二种:@Bean配置类形式

增加配置类

// 配置类==配置文件
@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.getAge());
        ApplicationContext applicationContext= new AnnotationConfigApplicationContext(MainConfig.class);
        Person bean= applicationContext.getBean(Person.class);
        System.out.println(bean);
        String[] namesForType= applicationContext.getBeanNamesForType(Person.class);
        for (String name:namesForType){
            System.out.println(name);
        }
    }
}

执行

可以读取到对象,另外@Bean后加()里面填的参数是什么id就是什么,默认为方法名,这是因为->看@Bean源码

填入的参数就是value即为id

包扫描
<!-- 包扫描-->
<context:component-scan base-package="com.atguigu"></context:component-scan>

在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean,不需要一个一个去注册,实现了批量注册

目录
相关文章
|
2月前
|
Java 测试技术 开发者
Spring IoC容器通过依赖注入机制实现控制反转
【4月更文挑战第30天】Spring IoC容器通过依赖注入机制实现控制反转
33 0
|
3天前
|
Java Spring 容器
使用ContextLoaderListener初始化Spring容器
使用ContextLoaderListener初始化Spring容器
|
6天前
|
XML Java 数据格式
经验大分享:Spring基础篇——Spring容器和应用上下文理解
经验大分享:Spring基础篇——Spring容器和应用上下文理解
10 1
|
2月前
|
Java 容器 Spring
Spring的加载配置文件、容器和获取bean的方式
Spring的加载配置文件、容器和获取bean的方式
30 3
Spring的加载配置文件、容器和获取bean的方式
|
4天前
|
Java API Docker
使用Spring Boot和Docker进行容器化部署
使用Spring Boot和Docker进行容器化部署
|
11天前
|
XML Java 数据格式
Spring容器启动源码解析
Spring容器启动源码解析
|
2月前
|
前端开发 Java 容器
家族传承:Spring MVC中父子容器的搭建与管理指南
家族传承:Spring MVC中父子容器的搭建与管理指南
41 3
|
2月前
|
XML Java 程序员
什么是Spring的IoC容器?
【4月更文挑战第30天】什么是Spring的IoC容器?
38 0
|
2月前
|
Java 容器 Spring
【spring(一)】核心容器总结
【spring(一)】核心容器总结
|
2月前
|
XML Java 数据格式
Spring IoC容器初始化过程(xml形式)
Spring IoC容器初始化过程(xml形式)
60 0