Spring Boot 选择注入相同类型 @Bean 的几种方式
Spring 默认是通过类型注入 Bean 的,所以当存在多个相同类型的 Bean 时,需要指定 Bean 的名称才可正确注入。
项目目录结构:
C:. │ .classpath │ .project │ pom.xml │ ├───.settings │ org.eclipse.core.resources.prefs │ org.eclipse.jdt.apt.core.prefs │ org.eclipse.jdt.core.prefs │ org.eclipse.m2e.core.prefs │ └───src └───main ├───java │ └───net │ └───wuxianjie │ └───demo │ Config.java │ DemoApplication.java │ VersionController.java │ └───resources application.yml
@Autowired
和 @Qualifier
Config.java
代码为:
package net.wuxianjie.demo; import java.util.HashMap; import java.util.Map; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class Config { @Bean("v1") public Map<String, String> versionConfig() { return new HashMap<String, String>() { { put("version", "v1.0"); } }; } @Bean("v2") public Map<String, String> version2Config() { return new HashMap<String, String>() { { put("version", "v2.0"); } }; } }
VersionController.java
代码为:
package net.wuxianjie.demo; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class VersionController { @Autowired @Qualifier("v2") private Map<String, String> config; @GetMapping("/test") public String test() { return config.get("version"); } }
固定方法名
Config.java
代码为:
package net.wuxianjie.demo; import java.util.HashMap; import java.util.Map; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class Config { @Bean public Map<String, String> versionConfig() { return new HashMap<String, String>() { { put("version", "v1.0"); } }; } @Bean public Map<String, String> version2Config() { return new HashMap<String, String>() { { put("version", "v2.0"); } }; } }
VersionController.java
代码为:
package net.wuxianjie.demo; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class VersionController { @Autowired private Map<String, String> version2Config; @GetMapping("/test") public String test() { return version2Config.get("version"); } }
通过 @Resource
(名称)
Config.java
代码为:
package net.wuxianjie.demo; import java.util.HashMap; import java.util.Map; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class Config { @Bean public Map<String, String> versionConfig() { return new HashMap<String, String>() { { put("version", "v1.0"); } }; } @Bean public Map<String, String> version2Config() { return new HashMap<String, String>() { { put("version", "v2.0"); } }; } }
VersionController.java
代码为:
package net.wuxianjie.demo; import java.util.Map; import javax.annotation.Resource; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class VersionController { @Resource(name = "version2Config") private Map<String, String> config; @GetMapping("/test") public String test() { return config.get("version"); } }
通过 @Resource
和 @Qualifier
(类型)
Config.java
代码为:
package net.wuxianjie.demo; import java.util.HashMap; import java.util.Map; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class Config { @Bean("v1") public Map<String, String> versionConfig() { return new HashMap<String, String>() { { put("version", "v1.0"); } }; } @Bean("v2") public Map<String, String> version2Config() { return new HashMap<String, String>() { { put("version", "v2.0"); } }; } }
VersionController.java
代码为:
package net.wuxianjie.demo; import java.util.Map; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class VersionController { @Resource(type = Map.class) @Qualifier("v2") private Map<String, String> config; @GetMapping("/test") public String test() { return config.get("version"); } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论