Spring使用注解开发演变过程

示例结构为:三个类,People,Cat,Dog,其中People内有Cat和Dog属性

一、不使用注解

  1. 创建实体类,实体类中无任何注解
Public class People {
    private String name;
    private Dog dog;
    private Cat cat;
    ...(sette/getter/toString)
}
Public class Dog {
    private String name;
    ...(sette/getter/toString)
}
Public class Cat {
    private String name;
    ...(sette/getter/toString)
}
  1. 在xml配置文件中注册实体类
<bean id="people" class="com.zzz.pojo.People">
    <property name="name" value="Zzz"/>
    <property name="dog" ref="dog"/>
    <property name="cat" ref="cat"/>
</bean>
<bean id="dog" class="com.zzz.pojo.Dog">
    <property name="name" value="LittleDog"/>
</bean>
<bean id="cat" class="com.zzz.pojo.Cat">
    <property name="name" value="LittleCat"/>
</bean>
  1. 在测试类中使用ClassPathXmlApplicationContext读取xml配置文件获取容器,进而使用对象
@Test
public void test(){
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    People people = context.getBean("people",People.class);	//获取实体类
    System.out.println(people);	//打印看结果
}

二、使用@Autowired自动装配

  1. 在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					←←←←←←
        http://www.springframework.org/schema/context/spring-context.xsd">		←←←←←←

    <context:annotation-config/>	←←←←←←!!这行容易忘记!!
    
    <bean id="people" class="com.zzz.pojo.People"/>
    <bean id="dog" class="com.zzz.pojo.Dog"/>
    <bean id="cat" class="com.zzz.pojo.Cat"/>

</beans>
  1. 在People类中的Dog和Cat属性上添加@Autowired注解自动装配dog和cat
Public class People {
    @Value("Zzz")
    private String name;
    
    @Autowired
    private Dog dog;
    
    @Autowired
    private Cat cat;
    ...(sette/getter/toString)
}
  1. xml文件只进行了注册,没有指定属性值,因此在各类的属性上使用@Value("xxx")进行赋值,如上代码;Dog类、Cat类同,略
  2. 测试类不用改变,进行测试,效果相同

小结

实体类仍在xml配置文件中注册,只不过People中的引用类型的指配用了@Autowired来自动完成。

三、使用@Component进行实体类的注册

  1. 修改xml文件,去掉,不在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
        http://www.springframework.org/schema/context/spring-context.xsd">		

    <context:component-scan base-package="com.zzz.pojo"/>	<!--添加组件扫描-->
    <context:annotation-config/>	
    
</beans>
  1. 在People类、Dog类、Cat类中添加@Component,实现注册
@Component
Public class People {
    @Value("Zzz")
    private String name;
    
    @Autowired
    private Dog dog;
    
    @Autowired
    private Cat cat;
    ...(sette/getter/toString)
}
@Component
public class Dog {
    @Value("LittleDog")
    private String name;
    ...(sette/getter/toString)
}
@Component
public class Cat {
    @Value("LittleCat")
    private String name;
    ...(sette/getter/toString)
}
  1. 测试类不用改变,进行测试,结果相同

小结

实体类的注册不再需要在xml配置文件中完成了,xml文件中的标签

<context:component-scan base-package="com.zzz.pojo"/>

会扫描该包下标注了@Component的类,也就是说@Component是用于和上面这个标签配合使用的;

@Autowired仍然需要,因为它是装配用的,相当于赋值,和注册两码事。

四、使用@Configuration定义配置类,可完全脱离xml文件

  1. 删掉xml文件,不用了
  2. 在pojo包下新建一个ZzzConfig类(名字自定,以Config结尾),在类中使用@Bean注册实体类,每一个方法返回对应实体类
@Configuration
public class ZzzConfig {

    @Bean
    public People people(){
        return new People();
    }

    @Bean
    public Dog dog(){
        return new Dog();
    }

    @Bean
    public Cat cat(){
        return new Cat();
    }
}
  1. 实体类中去掉@Component,因为它是配合xml组件扫描标签使用,用来注册的,我们已经在配置类中注册了,所以删掉
  2. 修改测试类,使用AnnotationConfigApplicationContext读取配置类
@Test
public void test(){
    ApplicationContext context = new AnnotationConfigApplicationContext(ZzzConfig.class);
    People people = context.getBean("people",People.class);	//获取实体类
    System.out.println(people);	//打印看结果
}
  1. 进行测试,结果相同

小结

使用@Configuration定义配置类,在配置类中使用@Bean注册实体类,完成脱离xml配置文件

五、使用@ComponentScan扫描包下实体类

  1. 配置类中每个实体类都要写个方法,还是有点麻烦,现在修改实体类,删掉里面的每个实体类对应方法,加上@ComponentScan
@Configuration
@ComponentScan(basePackages = "com.zzz.pojo")	//包名为自己的实体类包名
public class ZzzConfig {

}
  1. 每个实体类前加上@Component,与上述第三点类似
@Component
public class People {...}
@Component
public class Dog {...}
@Component
public class Cat {...}
  1. 测试类仍然采用读取配置类的方式,进行测试,效果相同

小结

在配置类上使用@ComponentScan扫描包下所有实体类,要注册的实体类就在类前添加@Component标注,未标注的无法扫描

总结

使用xml配置文件可以实现更复杂的操作,所有的实体类注册、赋值在一起,一次性全部展出,便于修改管理;

使用注解可以更加使代码更加简便,熟练后可在写代码时直接把注解一起写了,更加便捷,但只适用于简单直接的情况;

需根据自己的需求灵活使用xml配置文件与注解进行开发,常见做法为:在xml配置文件中进行实体类的注册,而属性的赋值则用注解完成;

使用配置类,并在类中写方法注册实体类的方式,一般在获取外部类时使用,如获取数据库连接getConnection,返回的是一个Connection类型的对象,其上加一个@Bean即可实现获取第三方类;自己定义的实体类则不会在配置类中注册。