前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring源码从入门到精通---@Configuration&@Bean(一)

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

作者头像
用户9919783
发布2022-07-26 11:37:03
1970
发布2022-07-26 11:37:03
举报
文章被收录于专栏:后端从入门到精通

想了很久,决定整理份 连载 的Spring源码(良心干货),供大家参考学习,本文是连载的第一篇文章,主要从spring加载实例bean开始讲起。

先贴上目录结构:

1、配置文件getBean

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

代码语言:javascript
复制
<?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>
代码语言:javascript
复制
 
/**
 * 人
 *
 * @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}】。

代码语言:javascript
复制
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为方法名。

代码语言:javascript
复制
/**
 * 配置文件
 * @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;

代码语言:javascript
复制
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);
        }
 
    }
}

打印结果:

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-06-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 后端从入门到精通 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 2、通过DI注解getBean
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档