Spring源码从入门到精通---@Configuration&@Bean(一)

简介: Spring源码从入门到精通---@Configuration&@Bean(一)

先贴上目录结构:

image.png

1、配置文件getBean


传统的spring,建立Person实体类,beans.xml文件,在里面指定bean,指定id,property指定实体类的name和age,实例类记得写上get,set方法,有参构造函数和无参构造函数,toString方法。之后写个mainTest类就可以测试从配置文件获取bean。下面附上代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true" 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"
       xmlns:context="http://www.springframework.org/schema/context">
    <bean class="com.alibaba.bean.Person" id="person" scope="singleton">
        <property name="age" value="18"></property>
        <property name="name" value="张三"></property>
    </bean>
    <!--包扫描,只要标注:@Controller,@Service,@Repository,@Component都能扫到-->
    <!--1、可以在配置文件配置扫描路径
        2、可以用注解@ComponentScan
    -->
   <!-- <context:component-scan base-package="com.alibaba"></context:component-scan>-->
</beans>
/**
 * 人
 *
 * @author keying
 * @date 2021/6/24
 */
public class Person {
    private String name;
    private Integer age;
    @Override
    public String toString() {
        return "Person{" +
            "name='" + name + '\'' +
            ", age=" + age +
            '}';
    }
    public Person() {
    }
    public Person(String name, Integer age) {
        this.name = name;
        this.age = 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;
    }
}

main方法getBean:通过classPathXmlApplicationContext对象加载配置文件,之后getBean强转成之前写的person实体类,打印出来【Person{name='张三', age=18}】。

public class MainTest {
    public static void main(String[] args) {
        //配置文件加载对象
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Person person = (Person)applicationContext.getBean("person");
        System.out.println(person.toString());
    }
}


2、通过DI注解getBean


文章开头的图有一个 BeanConfig类,我们在里面加上注解@Configuration,代表当前类是配置文件,和@Bean注解,代表当前对象交给spring容器管理,若@Bean没有指定value值,则当前对象的id为方法名。

/**
 * 配置文件
 * @Configuration 告诉spring这是一个配置类
 *
 *
 * @author keying
 * @date 2021/6/24
 */
@Configuration
public class BeanConfig {
    /**
     * @Bean吧对象注入给spring容器
     * 1、id默认是方法名,value方法可以指定方法名
     * @return Person
     */
    @Bean(value = "person")
    public Person getPerson(){
        return new Person("李四",19);
    }
}

然后在main方法里通过配置文件获取对象:用AnnotationConfigApplicationContext获取刚刚写的配置类BeanConfig,在获取对象并打印出来【Person{name='李四', age=19}】,还可以用下面的getBeanNamesForType方法打印他的id;

public class MainTest {
    public static void main(String[] args) {
        //配置文件加载对象
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Person person = (Person)applicationContext.getBean("person");
        System.out.println(person.toString());
        //注解加载对象
        ApplicationContext applicationContextAnnotation = new AnnotationConfigApplicationContext(BeanConfig.class);
        Person personByAnnotation = applicationContextAnnotation.getBean(Person.class);
        System.out.println(personByAnnotation.toString());
        String[] typeName = applicationContextAnnotation.getBeanNamesForType(Person.class);
        for (String type : typeName) {
            System.out.println(type);
        }
    }
}

打印结果:

image.png

相关文章
|
9天前
|
存储 监控 数据可视化
SaaS云计算技术的智慧工地源码,基于Java+Spring Cloud框架开发
智慧工地源码基于微服务+Java+Spring Cloud +UniApp +MySql架构,利用传感器、监控摄像头、AI、大数据等技术,实现施工现场的实时监测、数据分析与智能决策。平台涵盖人员、车辆、视频监控、施工质量、设备、环境和能耗管理七大维度,提供可视化管理、智能化报警、移动智能办公及分布计算存储等功能,全面提升工地的安全性、效率和质量。
|
1月前
|
XML Java 测试技术
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
136 26
|
2月前
|
监控 JavaScript 数据可视化
建筑施工一体化信息管理平台源码,支持微服务架构,采用Java、Spring Cloud、Vue等技术开发。
智慧工地云平台是专为建筑施工领域打造的一体化信息管理平台,利用大数据、云计算、物联网等技术,实现施工区域各系统数据汇总与可视化管理。平台涵盖人员、设备、物料、环境等关键因素的实时监控与数据分析,提供远程指挥、决策支持等功能,提升工作效率,促进产业信息化发展。系统由PC端、APP移动端及项目、监管、数据屏三大平台组成,支持微服务架构,采用Java、Spring Cloud、Vue等技术开发。
115 7
|
2月前
|
人工智能 自然语言处理 Java
Spring Cloud Alibaba AI 入门与实践
本文将介绍 Spring Cloud Alibaba AI 的基本概念、主要特性和功能,并演示如何完成一个在线聊天和在线画图的 AI 应用。
467 7
|
3月前
|
Java Spring
【Spring】方法注解@Bean,配置类扫描路径
@Bean方法注解,如何在同一个类下面定义多个Bean对象,配置扫描路径
196 73
|
3月前
|
存储 Java Spring
【Spring】获取Bean对象需要哪些注解
@Conntroller,@Service,@Repository,@Component,@Configuration,关于Bean对象的五个常用注解
|
3月前
|
存储 Java 应用服务中间件
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式
|
3月前
|
存储 安全 Java
Spring Security 入门
Spring Security 是 Spring 框架中的安全模块,提供强大的认证和授权功能,支持防止常见攻击(如 CSRF 和会话固定攻击)。它通过过滤器链拦截请求,核心概念包括认证、授权和自定义过滤器。配置方面,涉及密码加密、用户信息服务、认证提供者及过滤器链设置。示例代码展示了如何配置登录、注销、CSRF防护等。常见问题包括循环重定向、静态资源被拦截和登录失败未返回错误信息,解决方法需确保路径正确和添加错误提示逻辑。
157 2
Spring Security 入门
|
3月前
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
89 6
|
3月前
|
XML Java 数据格式
🌱 深入Spring的心脏:Bean配置的艺术与实践 🌟
本文深入探讨了Spring框架中Bean配置的奥秘,从基本概念到XML配置文件的使用,再到静态工厂方式实例化Bean的详细步骤,通过实际代码示例帮助读者更好地理解和应用Spring的Bean配置。希望对你的Spring开发之旅有所助益。
225 4