Spring @Primary 注解,指定主要实现

ゝ一纸荒年。 2022-04-15 05:40 328阅读 0赞

作用:

Spring @Primary 注解,指定主要实现,一个接口有多个实现时,只引入接口时,可以spring 可以直接引入@Primary 注解的实现

举例: 这样尽管一个接口有多个实现,只要其中一个service 上注解了@Primary 注解,这样在其他类中@Autowire 引入这个接口时,spring 会自动引入注解了@Primary 注解的实现

@Primary 的代码

Spring4.3

  1. @Target({ElementType.TYPE, ElementType.METHOD})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Inherited
  4. @Documented
  5. public @interface Primary {
  6. }

说明:

@Target 指定注解的目标: 表明@Primary 能注解在类或接口,和方法上

@Retention 表示作用的时间,表示作用在程序运行的时候

@Inherited 表示注解了@Primary 的注解的类或接口的子类或实现类,自动继承此注解

@Documented 表示会提现在文档中

示例代码

TestController 引入一个接口 TestService

  1. package com.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. import com.service.TestService;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. @Controller
  10. @RequestMapping("test")
  11. public class TestController {
  12. @Autowired
  13. private TestService testService;
  14. @ResponseBody
  15. @RequestMapping("test")
  16. public String test(HttpServletRequest request, HttpServletResponse response){
  17. return testService.test();
  18. }
  19. }

TestService 代码

  1. package com.service;
  2. import org.springframework.stereotype.Service;
  3. @Service
  4. public interface TestService {
  5. public String test();
  6. }

两个实现类

  1. package com.service.impl;
  2. import com.service.TestService;
  3. import org.springframework.stereotype.Service;
  4. @Service
  5. public class TestServiceImpl1 implements TestService {
  6. @Override
  7. public String test() {
  8. return this.toString();
  9. }
  10. }
  11. package com.service.impl;
  12. import com.service.TestService;
  13. import org.springframework.context.annotation.Primary;
  14. import org.springframework.stereotype.Service;
  15. @Primary
  16. @Service
  17. public class TestServiceImpl2 implements TestService {
  18. @Override
  19. public String test() {
  20. return this.toString();
  21. }
  22. }

测试结果

启动项目,在浏览器输入,项目名/test/test ,这个时候TestServiceImpl2 标注有@Primary,浏览器上打印 testServiceImpl2对象

去掉@Primary ,在浏览器输入.项目名/test/test. 显示500错误,提示无法实例化TestService ,因为他有两个实现

  1. 26-Nov-2018 09:31:43.190 严重 [http-nio-8083-exec-4] org.springframework.web.servlet.DispatcherServlet.initServletBean Context initialization failed
  2. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'testService'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.service.TestService' available: expected single matching bean but found 2: testServiceImpl1,testServiceImpl2
  3. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
  4. at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
  5. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
  6. ...
  7. ...
  8. ...
  9. Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.service.TestService' available: expected single matching bean but found 2: testServiceImpl1,testServiceImpl2
  10. at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:173)
  11. at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
  12. at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
  13. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
  14. ... 39 more

发表评论

表情:
评论列表 (有 0 条评论,328人围观)

还没有评论,来说两句吧...

相关阅读

    相关 @Primary注解

    在看项目的时候看到了@Primary,因为之前没留意过,所以就去学习了一下,在此记录一下自己的理解,感叹一下还要学习的东西太多了,慢慢加油。 1.概述 @Pri

    相关 Spring中@Primary注解

    1.概述 讨论Spring的@Primary注解,该注解是框架在3.0版中引入的。 其作用与功能,当有多个相同类型的bean时,使用@Primary来赋予bean更高的