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

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/kzcming/article/details/84335412

作用:

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

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

@Primary 的代码

Spring4.3

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Primary {

}

说明:

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

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

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

@Documented 表示会提现在文档中

示例代码

TestController 引入一个接口 TestService

package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.service.TestService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("test")
public class TestController {

    @Autowired
    private TestService testService;

    @ResponseBody
    @RequestMapping("test")
    public String test(HttpServletRequest request, HttpServletResponse response){
        return testService.test();
    }

}

TestService 代码

package com.service;

import org.springframework.stereotype.Service;

@Service
public interface TestService {

    public String test();

}

两个实现类

package com.service.impl;

import com.service.TestService;
import org.springframework.stereotype.Service;

@Service
public class TestServiceImpl1 implements TestService {
    @Override
    public String test() {
        return this.toString();
    }
}
package com.service.impl;

import com.service.TestService;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

@Primary
@Service
public class TestServiceImpl2  implements TestService {
    @Override
    public String test() {
        return this.toString();
    }
}

测试结果

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

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

26-Nov-2018 09:31:43.190 严重 [http-nio-8083-exec-4] org.springframework.web.servlet.DispatcherServlet.initServletBean Context initialization failed
 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
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
...
...
...

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
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:173)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
	... 39 more

猜你喜欢

转载自blog.csdn.net/kzcming/article/details/84335412