使用Java配置Spring

不适用Spring自己的xml配置,使用Java来配置

JavaConfig原本是Spring的一个子项目,在Spring 4之后为核心功能

在SpringBoot中普遍使用

官方示例:

1
2
3
4
5
6
7
8
@Configuration
public class AppConfig {

@Bean
public MyService myService() {
return new MyServiceImpl();
}
}

The preceding AppConfig class is equivalent to the following Spring <beans/> XML:

1
2
3
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>
  1. 建一个实体类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    package zone.yiqing.pojo;

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;

    /**
    * @Author Yiqing Zhang
    * @Date 2020-10-26 7:20 p.m.
    * @Version 1.0
    */
    @Component // 这个注解表明这个类被Spring接管,注册到容器中
    public class User {
    private String name;

    public String getName() {
    return name;
    }
    @Value("Yiqing") // 属性注入值
    public void setName(String name) {
    this.name = name;
    }

    @Override
    public String toString() {
    return "User{" +
    "name='" + name + '\'' +
    '}';
    }
    }
    1. 建一个配置类

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      package zone.yiqing.config;

      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.ComponentScan;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.context.annotation.Import;
      import zone.yiqing.pojo.User;

      /**
      * @Author Yiqing Zhang
      * @Date 2020-10-26 7:21 p.m.
      * @Version 1.0
      */
      // 这个也会被Spring托管,注册到容器中,因为它也是一个@Component
      // @Configuration表名这个类是一个配置类,等同于xml
      @Configuration
      @ComponentScan("zone.yiqing.pojo")
      @Import(YiqingConfig2.class)
      public class YiqingConfig {

      // 注册一个bean,就相当于bean标签
      // 方法名为bean中的id,返回值为bean中的class
      @Bean
      public User getUser(){
      return new User(); // 就是返回要注入到bean的对象
      }
      }

    2. 测试

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.annotation.AnnotationConfigApplicationContext;
      import zone.yiqing.config.YiqingConfig;
      import zone.yiqing.pojo.User;

      /**
      * @Author Yiqing Zhang
      * @Date 2020-10-26 7:24 p.m.
      * @Version 1.0
      */
      public class MyTest {
      public static void main(String[] args) {
      // 完全使用了配置类去做, 需要通过AnnotationConfig上下文来获取容器,通过配置类的class对象加载
      ApplicationContext context = new AnnotationConfigApplicationContext(YiqingConfig.class);
      User user = (User)context.getBean("getUser");//方法名
      System.out.println(user.getName());
      }