使用 Java 方式配置 Spring

PPG007 ... 2021-12-26 Less than 1 minute

# 使用 Java 方式配置 Spring

  • @Configuration:

    添加在配置类前,表示此 Java 类是一个配置文件。

    @Configuration
    public class Config {
        …………
    }
    
    1
    2
    3
    4
  • @ComponentScan:

    添加在配置类前,用于开启组件注册扫描,value 值为全包名。

    @Configuration
    @ComponentScan(value = "pojo")
    public class Config {
        …………
    }
    
    1
    2
    3
    4
    5
  • @Import:

    与 xml 中 import 标签作用相同,参数为要引用的配置类的 class。

    @Import(Config2.class)
    public class Config {
        …………
    }
    
    1
    2
    3
    4
  • @Bean:

    添加在配置类的方法名上,表示注册一个 bean,方法名就是 bean 的名字,也可以通过 name 属性指定 bean 的名字。

    @Configuration
    @ComponentScan(value = "pojo")
    @Import(Config2.class)
    public class Config {
        @Bean
        public User getUser(){
            return new User();
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
  • 通过注解进行配置,获取容器时要调用注解对应的上下文:

    ApplicationContext context=new AnnotationConfigApplicationContext(Config.class);
    User user= (User) context.getBean("getUser");
    System.out.println(user.toString());
    
    1
    2
    3
Last update: December 26, 2021 13:38
Contributors: PPG007