Spring注解驱动开发 第一节

Spring注解驱动开发 第一节

为什么要学Spring注解版的开发?

因为由于分布式、微服务的兴起以前的巨型单体项目由于配置繁琐,团队协作能力不好,修改后的需要进行大量的测试,等等暴露出了很多的问题,这是Spring团队又发布了Spring Boot产品,它用几乎零配置的方式,开箱即用,使用超级方便,在几秒内搭建一个简单的web服务已经成为现实,这也就表示以前用xml配置web服务的年代已经成为了历史,注解配置的方式成为主流,所以要想学好微服务,首先要学好Spring Boot,要学好Spring Boot,就要学好Spring注解的使用方式,所以从现在开始,要把Spring注解学到家。那现在就开始吧

在以前使用Spring时一直使用的时xml配置文件的方式使用Spring,例如如下这样:
首先在工程的类路径下新建一个xml配置文件,命名为applicationContext.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.meng.Person">
        <property name="name" value="孟兆坤" />
        <property name="age" value="23"/>
    </bean>
</beans>

稍微用过Spring的人都知道通过如下配置就可以吧Person类注入到Spring容器中,并初始化了相应的数据。然后就是pojo类

private String name;
    private String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

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

最后就是main主函数初始化Spring容器,并加载配置文件,最后获取Spring容器中的bean。

 public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person) context.getBean("person");
        System.out.println(person);
    }

结果:

"C:\Program Files (x86)\Java\jdk1.8.0_60\bin\java" -
四月 22, 2019 10:28:44 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1d56ce6a: startup date [Mon Apr 22 10:28:44 GMT+08:00 2019]; root of context hierarchy
四月 22, 2019 10:28:44 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
Person{name='孟兆坤', age='23'}

Process finished with exit code 0

从结果可以看出,我们的Person类已经成功注入到Spring容器中,在syso时,调用了Person类的toString方法,打印了Person类中的内容,上面只是一开始使用Spring最原始的使用方法,下面就要开始使用注解的方式配置使用Spring

注解的方式配置Spring

首先要配置一个配置类,这个配置类与刚才用到的配置文件的作用一致,都是起到的配置作用

@Configuration
public class MainConfig {
    @Bean
    public Person person(){
        return  new Person("张三","23");
    }
}

上面的事例中在类上有一个注解@Configuration注解,只要在这个类上标注此注解,就表示这个类等同于applicationContext.xml,而方法上的@Bean就表示xml文件中的标签,返回值表示配置文件中class的类路径,方法名表示注入类的id。

ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
          Person person = context.getBean(Person.class);
          System.out.println(person);

这段代码表示使用注解的方式加载配置类,并获取注入类的信息。可以看出加载配置类放入的是注解类型,获取注入类时传入的也是注解类型。

要获取person类型的bean在容器中的id名称,可以用如下的方式

ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
Person person = context.getBean(Person.class);
System.out.println(person);
String[] names = context.getBeanNamesForType(Person.class);
 for(String name : names){
     System.out.println(name);
 }

可以看出getBeanNamesType()根据组件的类型获取组件在Spring容器中的id名称,打印结果:

四月 22, 2019 11:04:13 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@433c675d: startup date [Mon Apr 22 11:04:13 GMT+08:00 2019]; root of context hierarchy
Person{name='张三', age='23'}
person

Process finished with exit code 0

可以看出,容器中组件id名称就是person,如果更改方法的名称,id结果是否会变化。

四月 22, 2019 11:08:01 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@433c675d: startup date [Mon Apr 22 11:08:01 GMT+08:00 2019]; root of context hierarchy
Person{name='张三', age='23'}
person01

Process finished with exit code 0

可以看出,修改配置类的方法名称后,组件id发生了变化。如果我不想让方法名称与容器中的组件名称一致怎么办?

@Configuration
public class MainConfig {
    @Bean("person")
    public Person person01(){
        return  new Person("张三","23");
    }
}

在注解bean中里加入字符串,因为默认是value,所以组件名称就不受方法名称的干扰了。打印结果:

四月 22, 2019 11:10:04 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@433c675d: startup date [Mon Apr 22 11:10:04 GMT+08:00 2019]; root of context hierarchy
Person{name='张三', age='23'}
person

又变成了person

猜你喜欢

转载自blog.csdn.net/William_HoF/article/details/89438406
今日推荐