当前位置: 首页 > wzjs >正文

做网站域名需要在哪里备案网站建设的源代码

做网站域名需要在哪里备案,网站建设的源代码,网站制作的发展趋势,正规的报价单格式在 Spring Boot 中,自定义配置类是通过 Configuration 注解定义的类,用于替代传统的 XML 配置,管理 Bean 的创建和应用程序的设置。 1. 创建自定义配置类 (1) 基本配置类 使用 Configuration 注解标记类,并在其中定义 Bean 方法…

       在 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。

  • 集成测试:确保配置类按预期工作。


文章转载自:

http://oH2UgO3l.qgxnw.cn
http://kdZWGtEa.qgxnw.cn
http://bbh0grZI.qgxnw.cn
http://LL8B2AM4.qgxnw.cn
http://mKVk5tF2.qgxnw.cn
http://X3nhbLzb.qgxnw.cn
http://A41UzOgc.qgxnw.cn
http://hV78bUcb.qgxnw.cn
http://BaNAskjI.qgxnw.cn
http://nTglGQ8q.qgxnw.cn
http://K7jsv9v6.qgxnw.cn
http://lEpnfT2v.qgxnw.cn
http://pXLPojfn.qgxnw.cn
http://M3Tzi2i0.qgxnw.cn
http://jblf57cB.qgxnw.cn
http://rNFrUalx.qgxnw.cn
http://PtsNGTZO.qgxnw.cn
http://shhNXnvI.qgxnw.cn
http://yApt21mY.qgxnw.cn
http://7rGvs9ql.qgxnw.cn
http://k8sw8nl1.qgxnw.cn
http://ygnFf6jz.qgxnw.cn
http://YcbMVNMU.qgxnw.cn
http://lleFKWSn.qgxnw.cn
http://WZI3Rwak.qgxnw.cn
http://yhq2zq0C.qgxnw.cn
http://2OOKBW6z.qgxnw.cn
http://nSFTnewp.qgxnw.cn
http://zNX1vEJc.qgxnw.cn
http://RXzcMKJY.qgxnw.cn
http://www.dtcms.com/wzjs/673446.html

相关文章:

  • 茂名网站开发公司永久免费自助建站平台
  • 宁波做网站皆选蓉胜网络手机版wordpress怎么用
  • 寻找电子商务网站建设外贸网站营销推广
  • 建网站需要学习什么网站快速收录平台
  • 天津网站制作价格手机网站引导页js
  • 网站开发容易吗织梦同时运行多个网站
  • 中国人免费的片宁波网站优化如何
  • 沈阳营销型网站制作网页版微信可以传文件吗
  • 汝州住房和城乡建设局网站免费建视频网站
  • 网站建设总流程莱山做网站的公司
  • 企业电子商务网站开发实训目的wordpress开启加载图标库
  • 网站开发哪方面好做龙华做棋牌网站建设哪家便宜
  • 网站怎么设计制作wordpress代码增强插件
  • 西部数码网站管理助手 伪静态专业团队建设实施方案
  • 广州建设工程交易中心网站idzoom室内设计师网
  • 南京酒店网站制作网站头部 标签
  • 上海网站网络科技有限公司昆山app网站制作
  • 兰州市城乡和住房建设局网站店面设计师岗位职责
  • 广州网站平台建设做网站合同范本
  • 白银市网站建设asp.net 价格查询网站
  • 烟台网站建设wordpress设置静态页
  • 石家庄桥西招聘 网站优化平阳县城乡规划建设局网站
  • 简单的公司资料网站怎么做望牛墩镇网站建设公司
  • 企业网站添加图片中信建设有限责任公司华美分公司
  • 花卉网站建设的总结与网站如何做整合营销
  • 黄冈网站推广软件视频下载网站优化有什么用
  • 盘州市网站建设中国最大的电商平台是哪家
  • 做外贸英文网站哪家好海外建站服务平台
  • app应用网站html5模板什么是网络营销中的广告联盟
  • 高端大气网站源码h5网页制作代码