天天看點

@Configuration用法

1 帶@Configuration注解的類,該類本身首先會被SpringBoot注入到IOC容器中;

2 SpringBoot同時會掃描該類中帶@Bean注解的方法,調用該方法來建立Bean,并且将該Bean注入到IOC容器中

示例如下:

@Configuration
@EnableConfigurationProperties(PhoneProperties.class)
public class DemoConfig {
    @Autowired
    private PhoneProperties phoneProperties;

    @Bean
    public Phone phone() {
        return new Phone(phoneProperties.getType(), phoneProperties.getPrice());
    }
}
           

關于@EnableConfigurationProperties,PhoneProperties,Phone的更多詳情,可以參看@ConfigurationProperties

繼續閱讀