Skip to content
Back to skills

Spring动态选择接口实现类

ASecurity

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

  • 611 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 11, 2026
educationjavaspring

Security analysis

A100/100

Scanned November 11, 2026

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

Installs into .claude/skills of the current project.

Are you the author of Spring动态选择接口实现类?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Spring动态选择接口实现类
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ecnu-icalk-spring/badge)](https://www.skillsdirectory.com/skills/ecnu-icalk-spring)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
id: "0e5a2371-b1d7-4fd1-9027-394769a22af1"
name: "Spring动态选择接口实现类"
description: "针对Spring框架中一个接口有多个实现类的场景,提供不使用@Qualifier和@Value,而是根据运行时参数动态选择具体实现类的解决方案(通常使用Map注入或策略模式)。"
version: "0.1.0"
tags:
  - "Spring"
  - "Java"
  - "依赖注入"
  - "策略模式"
  - "动态选择"
triggers:
  - "Spring接口多实现怎么根据参数选"
  - "不使用Qualifier动态注入Bean"
  - "Spring根据参数选择实现类"
  - "Spring策略模式Map注入"
---

# 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注入

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in education →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.