第1次Spring源码学习之@Bean、@Configuration、xml、分析

时间:2022-07-22
本文章向大家介绍第1次Spring源码学习之@Bean、@Configuration、xml、分析,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1、准备环境

idea
jdk1.8
maven3.5
Spring 4.3.12

首先这是一个maven项目,为了简单明了此处就不多说了

pom.xml文件引入spring坐标

 <dependency>
    <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>4.3.12.RELEASE</version>
 </dependency>

创建一个bean对象

package com.zhenghui.bean;

public class Person {

    private String name;

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + ''' +
                ", age=" + age +
                '}';
    }
}

2、传统的加载Bean模式

在resources目录下创建一个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.zhenghui.bean.Person">
        <property name="name" value="zhenghui"/>
        <property name="age" value="18"/>
    </bean>
</beans>

解释

<bean></bean>标签:

id:取的一个名字
class:需要加载的bean的实体类

<property/>标签:

name:实体类的变量名
value:需要赋值的数据

测试类

package com.zhenghui;

import com.zhenghui.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {

    public static void main(String[] args) {
        //配置的方式
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Person person = (Person)applicationContext.getBean("person");
        System.out.println(person.toString());
    }
}

测试结果 可以看出成功的打印出了我们想要的结果

Person{name='zhenghui', age=18}

评价使用xml加载bean的方式这种方式 太笨拙了。

3、使用注解加载bean

创建一个MainConfig.java文件内容如下:

package com.zhenghui.config;

import com.zhenghui.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//配置类(java代码的方式)==以前的配置文件(xml文件的方式)
@Configuration //告诉Spring这是一个配置类
public class MainConfig {

    //给容器中注册一个Bean;类型就是返回值的类型,id模式是使用的方法名作为id
    @Bean()
    public Person person(){
        return new Person("里斯",20);
    }
}

修改MainTest.java

package com.zhenghui;

import com.zhenghui.bean.Person;
import com.zhenghui.config.MainConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {

    public static void main(String[] args) {
       
        ApplicationContext acac = new AnnotationConfigApplicationContext(MainConfig.class);
        Person person = acac.getBean(Person.class);
        System.out.println(person.toString());

    }
}

运行结果

Person{name='里斯', age=20}

分析与评价 可见这种方式比加载配置文件的方式要好的多。