CloudOptions 配置参考
This content is not available in your language yet.
本文档详细说明 CloudOptions 的所有配置参数及其使用方法。
CloudOptions 是 Feat Cloud 服务的核心配置类,继承自 ServerOptions,因此支持 ServerOptions 中的所有配置项(如端口、线程池、SSL 等)。
CloudOptions extends ServerOptionspackages
Section titled “packages”指定 Feat Cloud 启动时扫描的包路径数组。
| 属性 | 值 |
|---|---|
| 类型 | String[] |
| 默认值 | null(扫描所有包) |
| 必填 | 否 |
用途:限制组件扫描范围以提升启动性能。
示例:
FeatCloud.cloudServer(options -> { options.setPackages( "com.example.controller", "com.example.service" );}).listen();CloudOptions options = new CloudOptions();options.setPackages("com.example.controller", "com.example.service");FeatCloud.cloudServer(options).listen();registerBean
Section titled “registerBean”向容器中注册外部 Bean 实例。
| 属性 | 值 |
|---|---|
| 方法签名 | public CloudOptions registerBean(String key, Object value) |
| 返回值 | CloudOptions(支持链式调用) |
| 必填 | 否 |
用途:集成第三方组件或手动创建的 Bean 实例。
示例:
FeatCloud.cloudServer(options -> { options.registerBean("dataSource", createDataSource()); options.registerBean("redisTemplate", createRedisTemplate()); options.registerBean("mqProducer", createMQProducer());}).listen();@Controllerpublic class UserController {
@Autowired private DataSource dataSource;
@Autowired private RedisTemplate redisTemplate;
@RequestMapping("/user/{id}") public User getUser(@PathParam Integer id) { // 使用注入的 Bean 处理业务 return userService.findById(id); }}staticLocations
Section titled “staticLocations”指定静态资源文件的存放位置。
| 属性 | 值 |
|---|---|
| 类型 | String |
| 默认值 | "classpath:static" |
| 必填 | 否 |
用途:配置静态 HTML、CSS、JavaScript 等资源的根路径。
示例:
// 使用默认路径FeatCloud.cloudServer().listen();
// 自定义类路径FeatCloud.cloudServer(options -> { options.setStaticLocations("classpath:assets");}).listen();
// 使用文件系统路径FeatCloud.cloudServer(options -> { options.setStaticLocations("file:/var/www/static");}).listen();完整配置示例
Section titled “完整配置示例”public class Application { public static void main(String[] args) { FeatCloud.cloudServer(options -> { // 配置扫描包 options.setPackages("com.example");
// 注册外部 Bean options.registerBean("dataSource", DataSourceBuilder.create() .url("jdbc:mysql://localhost:3306/test") .username("root") .password("password") .build());
// 配置静态资源路径 options.setStaticLocations("classpath:public");
}).listen(); }}Bean 名称冲突
Section titled “Bean 名称冲突”tech.smartboot.feat.core.common.exception.FeatException: bean dataSource already exists解决方案:使用唯一名称,添加业务前缀。
若配置的包路径不存在,扫描结果为空,组件可能无法加载。建议在配置前验证包路径有效性。
CloudOptions 继承自 ServerOptions,因此也支持以下常用配置:
| 配置项 | 说明 |
|---|---|
port | 服务端口,默认 8080 |
threadNum | 线程池大小 |
readBufferSize | 读缓冲区大小 |
writeBufferSize | 写缓冲区大小 |
ServerOptions配置参考 - 继承的基础配置项- 快速开始 - CloudOptions 的入门使用
- 控制器开发 - 使用注册 Bean 开发控制器