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.md
source 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

  1. 实现接口:必须实现
    BeanDefinitionRegistryPostProcessor
    EnvironmentAware
    接口。
  2. 生命周期处理:由于
    BeanDefinitionRegistryPostProcessor
    执行极早,此时使用
    @Autowired
    @Resource
    注入的配置类(如
    JFXConfig
    )会为
    null
    。必须通过
    EnvironmentAware
    接口的
    setEnvironment
    方法获取
    Environment
    对象,并使用
    environment.getProperty("key", "defaultValue")
    来获取配置路径。
  3. 资源扫描:使用注入的
    ResourcePatternResolver
    根据配置路径扫描所有
    .fxml
    资源文件。
  4. Bean定义:为每个资源创建
    GenericBeanDefinition
    • 设置
      BeanClass
      Parent.class
    • 设置
      Scope
      BeanDefinition.SCOPE_PROTOTYPE
    • 设置
      InstanceSupplier
      为一个 Lambda 表达式,内部调用
      FXMLLoader.load(resource.getURL())
      ,确保每次获取Bean时都重新加载FXML。
  5. 解析Controller
    • 读取 FXML 文件内容,查找包含
      fx:controller
      的行。
    • 使用正则表达式
      Pattern.compile("fx:controller=\\"(.*?)\\"")
      进行非贪婪匹配,提取完整的 Controller 类名。
    • 如果找不到
      fx:controller
      ,抛出明确的异常。
  6. 生成Bean名称
    • 将提取的完整类名按
      .
      分割。
    • 取最后一部分(类名),使用
      StringUtils.uncapitalize
      将其首字母小写作为 Bean 名称。
  7. 注册:将生成的 Bean 名称和 Bean 定义注册到
    BeanDefinitionRegistry

Anti-Patterns

  • 不要在
    BeanDefinitionRegistryPostProcessor
    中使用
    @Value
    @Resource
    注入自定义配置类,这会导致空指针异常。
  • 不要在
    InstanceSupplier
    外部加载 FXML,否则所有 Bean 实例将共享同一个 Parent 对象。

Interaction Workflow

  1. 用户提供 FXML 扫描路径配置(如
    spring.fx.fxml-scan
    )。
  2. 系统自动扫描并注册 Bean。
  3. 用户可以在代码中直接通过 Controller 类名(首字母小写)注入 FXML 视图。

Triggers

  • 自动注册fxml bean
  • spring boot javafx fxml integration
  • 根据controller注册fxml
  • BeanDefinitionRegistryPostProcessor fxml
  • jfxConfig为null