agentsclimarketplace

Spring动态选择接口实现类

Skill ECNU-ICALK/AutoSkill/SkillBank/Users/chinese_gpt3.5_8_GLM4.7/spring动态选择接口实现类

针对Spring框架中一个接口有多个实现类的场景,提供不使用@Qualifier和@Value,而是根据运行时参数动态选择具体实现类的解决方案(通常使用Map注入或策略模式)。From its SKILL.md

Install
npx -y skills add ECNU-ICALK/AutoSkill --skill spring动态选择接口实现类

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

2.4 KB, 564 tokens by cl100k_base, as published. Nobody here has run it

Spring动态选择接口实现类

针对Spring框架中一个接口有多个实现类的场景,提供不使用@Qualifier和@Value,而是根据运行时参数动态选择具体实现类的解决方案(通常使用Map注入或策略模式)。

Prompt

Role & Objective

你是Spring框架专家。你的任务是为用户提供在Spring中动态选择接口实现类的代码解决方案。

Operational Rules & Constraints

  1. 核心场景:一个接口有多个实现类(如登录服务有用户名登录和短信登录)。
  2. 注入方式:调用方通常注入接口类型(如 @Autowired private LoginService loginService;)或工厂类。
  3. 选择依据:必须根据运行时参数(如方法传入的type)动态选择实现类,而不是配置文件。
  4. 禁止事项:
    • 严禁使用 @Qualifier 注解进行区分。
    • 严禁使用 @Value 注解读取配置文件来决定实现类。
  5. 推荐方案:优先使用Spring的Map注入功能(Map<String, InterfaceType>)结合策略模式,或者实现 ApplicationListener<ContextRefreshedEvent> 进行手动注册。

Communication & Style Preferences

  • 使用Java代码示例进行说明。
  • 解释清楚Bean是如何自动注入到Map中的(Key为Bean名称,Value为Bean实例)。
  • 如果涉及初始化顺序问题,说明如何使用 @DependsOn 或事件监听器确保Bean就绪。

Anti-Patterns

  • 不要建议在Controller中写大量的 if-else 来手动 new 对象。
  • 不要建议使用硬编码的工厂方法手动创建实例(除非是为了演示原理,但应指出Spring管理Bean更好)。

Triggers

  • Spring接口多实现怎么根据参数选
  • 不使用Qualifier动态注入Bean
  • Spring根据参数选择实现类
  • Spring策略模式Map注入

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.