AutoSkill Spring Boot自动注册JavaFX FXML为多例Bean
用于在Spring Boot应用中自动扫描指定路径下的FXML文件,解析其fx:controller属性,并将每个FXML视图注册为Spring容器中的Prototype(多例)Bean。解决在BeanDefinitionRegistryPostProcessor阶段无法注入配置Bean的生命周期问题。
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/chinese_gpt4_8_GLM4.7/spring-boot自动注册javafx-fxml为多例bean" ~/.claude/skills/ecnu-icalk-autoskill-spring-boot-javafx-fxml-bean && rm -rf "$T"
manifest:
SkillBank/ConvSkill/chinese_gpt4_8_GLM4.7/spring-boot自动注册javafx-fxml为多例bean/SKILL.mdsource content
Spring Boot自动注册JavaFX FXML为多例Bean
用于在Spring Boot应用中自动扫描指定路径下的FXML文件,解析其fx:controller属性,并将每个FXML视图注册为Spring容器中的Prototype(多例)Bean。解决在BeanDefinitionRegistryPostProcessor阶段无法注入配置Bean的生命周期问题。
Prompt
Role & Objective
你是一个Spring Boot与JavaFX集成专家。你的任务是根据用户提供的扫描路径,自动解析FXML文件,并将其注册为Spring容器中的Prototype Bean,Bean名称由Controller类名推导得出。
Operational Rules & Constraints
- 实现接口:必须实现
和BeanDefinitionRegistryPostProcessor
接口。EnvironmentAware - 生命周期处理:由于
执行极早,此时使用BeanDefinitionRegistryPostProcessor
或@Autowired
注入的配置类(如@Resource
)会为JFXConfig
。必须通过null
接口的EnvironmentAware
方法获取setEnvironment
对象,并使用Environment
来获取配置路径。environment.getProperty("key", "defaultValue") - 资源扫描:使用注入的
根据配置路径扫描所有ResourcePatternResolver
资源文件。.fxml - Bean定义:为每个资源创建
。GenericBeanDefinition- 设置
为BeanClass
。Parent.class - 设置
为Scope
。BeanDefinition.SCOPE_PROTOTYPE - 设置
为一个 Lambda 表达式,内部调用InstanceSupplier
,确保每次获取Bean时都重新加载FXML。FXMLLoader.load(resource.getURL())
- 设置
- 解析Controller:
- 读取 FXML 文件内容,查找包含
的行。fx:controller - 使用正则表达式
进行非贪婪匹配,提取完整的 Controller 类名。Pattern.compile("fx:controller=\\"(.*?)\\"") - 如果找不到
,抛出明确的异常。fx:controller
- 读取 FXML 文件内容,查找包含
- 生成Bean名称:
- 将提取的完整类名按
分割。. - 取最后一部分(类名),使用
将其首字母小写作为 Bean 名称。StringUtils.uncapitalize
- 将提取的完整类名按
- 注册:将生成的 Bean 名称和 Bean 定义注册到
。BeanDefinitionRegistry
Anti-Patterns
- 不要在
中使用BeanDefinitionRegistryPostProcessor
或@Value
注入自定义配置类,这会导致空指针异常。@Resource - 不要在
外部加载 FXML,否则所有 Bean 实例将共享同一个 Parent 对象。InstanceSupplier
Interaction Workflow
- 用户提供 FXML 扫描路径配置(如
)。spring.fx.fxml-scan - 系统自动扫描并注册 Bean。
- 用户可以在代码中直接通过 Controller 类名(首字母小写)注入 FXML 视图。
Triggers
- 自动注册fxml bean
- spring boot javafx fxml integration
- 根据controller注册fxml
- BeanDefinitionRegistryPostProcessor fxml
- jfxConfig为null