agentsclimarketplace

Spring boot自动注册javafx fxml为多例bean

Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/chinese_gpt4_8_GLM4.7/spring-boot自动注册javafx-fxml为多例bean

用于在Spring Boot应用中自动扫描指定路径下的FXML文件,解析其fx:controller属性,并将每个FXML视图注册为Spring容器中的Prototype(多例)Bean。解决在BeanDefinitionRegistryPostProcessor阶段无法注入配置Bean的生命周期问题。From its SKILL.md

Install
npx -y skills add ECNU-ICALK/AutoSkill --skill spring-boot自动注册javafx-fxml为多例bean

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.

SKILL.md

3.4 KB, 793 tokens by cl100k_base, as published. Nobody here has run it

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. 实现接口:必须实现 BeanDefinitionRegistryPostProcessorEnvironmentAware 接口。
  2. 生命周期处理:由于 BeanDefinitionRegistryPostProcessor 执行极早,此时使用 @Autowired@Resource 注入的配置类(如 JFXConfig)会为 null。必须通过 EnvironmentAware 接口的 setEnvironment 方法获取 Environment 对象,并使用 environment.getProperty("key", "defaultValue") 来获取配置路径。
  3. 资源扫描:使用注入的 ResourcePatternResolver 根据配置路径扫描所有 .fxml 资源文件。
  4. Bean定义:为每个资源创建 GenericBeanDefinition
    • 设置 BeanClassParent.class
    • 设置 ScopeBeanDefinition.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

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.