Spring Boot 自动装配是一种便捷的功能,它可以自动发现和配置项目中的 bean,从而简化 Spring 应用程序的配置。自动装配的目标是让开发者能够专注于业务逻辑,而不需要花费大量时间手动配置各种组件、服务和数据源
首先加载核心注解@SpringBootApplication
package com.it.myservice;
import com.it.myservice.service.MyService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class AutomaticDemoApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(AutomaticDemoApplication.class, args);
        MyService myService = context.getBean(MyService.class);
        System.out.println(1+myServi
        ce.getMessage());
    }
}
2.Spring Boot 读取 spring.factories 文件
在 src/main/resources/META-INF 目录下创建 spring.factories 文件,以便 Spring Boot 能够发现自动装配类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.it.myservice.config.MyServiceAutoConfiguration3.创建一个 MyServiceAutoConfiguration 类,用于自动装配 MyService
package com.it.myservice.config;
import com.it.myservice.service.MyService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration  @EnableConfigurationProperties(MyServiceProperties.class)
public class MyServiceAutoConfiguration {  
    @Bean
    @ConditionalOnMissingBean
    public MyService myService(MyServiceProperties properties) {
        return new MyService(properties.getMessage());
    }
}4.创建一个 MyServiceProperties 类,用于绑定配置属性
package com.example.myservice;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "myservice")
public class MyServiceProperties {
    private String message = "Default message";
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}5.创建一个简单的 MyService 类
package com.example.myservice;
public class MyService {
    private String message;
    public MyService(String message) {
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
}
  1.Spring Boot 在启动时扫描项目中的类和包,发现 MyServiceAutoConfiguration 类。
  2.Spring Boot 读取 spring.factories 文件,找到 MyServiceAutoConfiguration 类作为自动装配类。
  3. MyServiceAutoConfiguration 类使用 @EnableConfigurationProperties 注解
  启用 MyServiceProperties 类的配置属性绑定。
  4.MyServiceProperties 类使用 @ConfigurationProperties 注解
  将 application.properties 文件中的 myservice.message 属性绑定到 message 字段。
  5.MyServiceAutoConfiguration 类中的 myService() 方法
  使用 @Bean 和 @ConditionalOnMissingBean 注解定义了一个 MyService 类型的 bean。
  当没有其他 MyService bean 时,Spring Boot 会自动装配这个 bean。
  6.MyService bean 使用 MyServiceProperties 类中的 message 属性值进行初始化。原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。