Spring注解开发——从配置到注解的蜕变

bean注入从xml配置到注解的蜕变

spring发展之初,所有的bean都在xml中配置,像这样(application-context.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.example.demo.bean.Person" >
        <property name="name" value="prince" />
        <property name="age" value="27" />
    </bean>

</beans>

整个xml文件可以看成一个bean容器,你要使用什么bean,首先在这里声明,使用这个bean像这样:

public class XmlConfigBeanTest {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");

        Person person = (Person) applicationContext.getBean("person");

        System.out.println(person);
    }
}

先读取这个容器,然后根据bean的id获取这个bean的实例。输出结果

setter注入name。
setter注入age。
Person{name='prince', age=27}

上面使用到的Person类:

package com.example.demo.bean;

/**
 * todo
 *
 * @author prince chen
 * @version 1.0
 * @date 2019/8/22
 **/
public class Person {
    private String name;
    private int age;
    private String gender;
    private String note;

    private Car car;

    public void setCar(Car car) {
        this.car = car;
        System.out.println("setter inject car.");
    }

    public Person(String gender, String note) {
        this.gender = gender;
        this.note = note;
        System.out.println("constructor注入。。。");
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("setter注入name。");
    }

    public void setAge(int age) {
        this.age = age;
        System.out.println("setter注入age。");
    }

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

我们可以想象一下,每增加一个bean我们都要在这里配置一下,随着bean的增多,我们要在这个xml里面维护无数的bean,后来spring想了一个办法,我们只用在xml指定要扫描哪个包,spring会把包下面带有注解的类都放到bean容器,用的时候直接取就行,此时我们只用在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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.example.demo.bean" />

</beans>

从此xml一下子干净了,整个世界都清净了。需要注入容器的bean,我们只需要在对应的类上面加入@Controller、@Component、@Service、@Repository、@Resource其中的一种,spring就能识别出来给你加入bean容器

Person类:

@Component
public class Person {
    @Value("prince")
    private String name;

    private Car car;

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

Car类:

@Resource
public class Car {
}

测试结果:

Person{name='prince', car=null}

人总是有追求的,有了好的就想要更好的,有人就会说了,我现在连配置文件都不想配置了行不行?

可以,没问题,spring也为你准备好了

接下来我们彻底抛弃配置,走上人生巅峰。

我们写一个AnnotationConfig类,像这样:

package com.example.demo.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * todo
 *
 * @author prince chen
 * @version 1.0
 * @date 2019/8/23
 **/
@Configuration()
@ComponentScan(value = "com.example.demo.bean")
public class AnnotationConfig {
}

这里的@Configuration注解其实就相当于我们那个application-context.xml文件,@ComponentScan就相当于配置文件里面的<context:component-scan />

测试结果,不用想了,一毛一样!

我们可以看到,spring由最初的完全xml配置,到xml+注解,再到完全注解,一步步带领我们走上人生巅峰

好嗨哟,感觉人生已经到达了高潮~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值