Springboot中如何自定义配置类
在 Spring Boot 中,自定义配置类是通过 @Configuration
注解定义的类,用于替代传统的 XML 配置,管理 Bean 的创建和应用程序的设置。
1. 创建自定义配置类
(1) 基本配置类
使用 @Configuration
注解标记类,并在其中定义 @Bean
方法:
@Configuration
public class AppConfig {// 定义一个简单的 Bean@Beanpublic MyService myService() {return new MyServiceImpl();}// 注入依赖并配置 Bean@Beanpublic DataSource dataSource() {return DataSourceBuilder.create().url("jdbc:mysql://localhost:3306/mydb").username("root").password("123456").build();}
}
2. 条件化 Bean 配置
使用 Spring Boot 的条件注解控制 Bean 的创建条件:
(1) 根据类路径是否存在类创建 Bean
@Configuration
public class ConditionalConfig {@Bean@ConditionalOnClass(name = "com.example.ExternalService")public ExternalService externalService() {return new ExternalServiceImpl();}
}
(2) 根据配置文件属性创建 Bean
@Configuration
public class PropertyConditionConfig {@Bean@ConditionalOnProperty(prefix = "feature", name = "enabled", havingValue = "true")public FeatureService featureService() {return new FeatureServiceImpl();}
}
在 application.properties
中配置:
feature.enabled=true
3. 绑定配置属性
使用 @ConfigurationProperties
将配置文件中的属性绑定到 Java 类:
(1) 定义配置属性类
@ConfigurationProperties(prefix = "app")
public class AppProperties {private String name;private String version;// Getter 和 Setter
}
(2) 启用配置属性绑定
在配置类中注册属性类:
@Configuration
@EnableConfigurationProperties(AppProperties.class)
public class AppConfig {@Autowiredprivate AppProperties appProperties;@Beanpublic AppInfo appInfo() {return new AppInfo(appProperties.getName(), appProperties.getVersion());}
}
(3) 配置文件内容
在 application.yml
或 application.properties
中配置:
app:name: My Spring Boot Appversion: 2.0.0
4. 多环境配置
使用 @Profile
注解根据环境激活不同的配置:
(1) 定义不同环境的配置类
@Configuration
@Profile("dev")
public class DevConfig {@Beanpublic DataSource devDataSource() {// 开发环境数据源配置}
}@Configuration
@Profile("prod")
public class ProdConfig {@Beanpublic DataSource prodDataSource() {// 生产环境数据源配置}
}
(2) 激活环境
在 application.properties
中指定激活的 Profile:
spring.profiles.active=dev
5. 导入其他配置类
使用 @Import
注解组合多个配置类:
@Configuration
@Import({DatabaseConfig.class, SecurityConfig.class})
public class MainConfig {// 主配置类导入其他配置
}
6. 高级配置:自定义 Starter
(1) 创建自动配置类
@Configuration
@ConditionalOnClass(MyService.class)
@EnableConfigurationProperties(MyServiceProperties.class)
public class MyServiceAutoConfiguration {@Bean@ConditionalOnMissingBeanpublic MyService myService(MyServiceProperties properties) {return new MyServiceImpl(properties);}
}
(2) 注册自动配置
在 META-INF/spring.factories
中声明:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.MyServiceAutoConfiguration
7. 验证配置类
(1) 单元测试
使用 @SpringBootTest
验证 Bean 是否加载:
@SpringBootTest
public class AppConfigTest {@Autowiredprivate MyService myService;@Testvoid testMyServiceBean() {assertNotNull(myService);}
}
(2) 查看加载的 Bean
启动应用后访问 /actuator/beans
端点(需先启用 Actuator),查看所有注册的 Bean。
总结
通过 @Configuration
和 @Bean
可以灵活定义配置类,结合条件注解(如 @ConditionalOnProperty
)、属性绑定(@ConfigurationProperties
)和环境隔离(@Profile
),能够实现高度定制化的配置。关键点包括:
-
模块化配置:将不同功能的配置拆分到多个类。
-
条件化加载:根据环境或属性动态决定是否创建 Bean。
-
集成测试:确保配置类按预期工作。