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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TrueDei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值