Spring Boot管理接口Bean

时间:2017-08-22 18:54:23

标签: java spring spring-boot interface javabeans

我有一个具有如下实现的接口:

public interface Vehicle<T> {
    void stuff(T param);
}

@Component
public class Car<T> implements Vehicle<T> {
    private final Strategy<T> strat;

    public Car(@Lazy Strategy strat) {
        this.strat = strat;
    }

    void stuff(T param) {
        strat.stuff(param);
    }
}

策略接口/类看起来像这样:

public interface Strategy<T> {
    void stuff(T param);
}

@Component
public class FooAStrategy implements Strategy<FooA> {
    void stuff(FooA param) { //do stuff }
}

@Component
public class FooBStrategy implements Strategy<FooB> {
    void stuff(FooABparam) { //do stuff }
}

最后,我有一个这样的服务类:

@Service
public class ServiceClassA {
    private FooA fooA;
    private FooB fooB;
    private FooC fooC;
    private Vehicle<FooD> vehicle; //how to inject correct implementation?

    public ServiceClass(FooA fooA, FooB fooB, FooC fooC) {
        this.fooA = fooA;
        this.fooB = fooB;
        this.fooC = fooC;
    }
}

@Service
public class ServiceClassB {
    private FooA fooA;
    private FooB fooB;
    private FooC fooC;
    private Vehicle<FooE> vehicle; //how to inject correct implementation?

    public ServiceClass(FooA fooA, FooB fooB, FooC fooC) {
        this.fooA = fooA;
        this.fooB = fooB;
        this.fooC = fooC;
    }
}

基本上,对于第一个服务类ServiceClassA,我想这样做:

Strategy strat = new FooAStrategy();
Vehicle vehicle = new Car(strat);
ServiceClassA class = new ServiceClassA(fooA, fooB, fooC, vehicle);

但每次运行应用程序时都会出现此错误:

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.test.notification.Strategy<?>' available: expected single matching bean but found 2: fooAStrategy,fooBStrategy
    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.context.annotation.ContextAnnotationAutowireCandidateResolver$1.getTarget(ContextAnnotationAutowireCandidateResolver.java:83)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192)
    at com.sun.proxy.$Proxy259.generateNotifications(Unknown Source)
    at com.test.notification.RabbitMQEventPublisher.process(RabbitMQEventPublisher.java:24)
    at com.test.services.ServiceClassA.testConncetion(ServiceClassA.java:400)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy260.testConnection(Unknown Source)
    at com.test.controllers.ControllerTest.get(ControllerTest.java:130)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at com.test.security.jwt.JwtTokenAuthenticationProcessingFilter.successfulAuthentication(JwtTokenAuthenticationProcessingFilter.java:52)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:240)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:96)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

由于我让spring引导使用@Component注释和构造函数自行连接所有内容,因此我没有任何配置类。我不知道如何指定1依赖的具体实现。

最好的方法是什么?

2 个答案:

答案 0 :(得分:0)

从汽车中移除@Component并注入工厂。就像消息所说,Spring对于需要创建的东西是不明确的。一种解决方案是通过工厂自己创建汽车,例如FooACarFactory(可以实现通用FooAVehicleFactory)。您可以让Spring创建FooACarFactory并将其注入到具有vehicle = fooAVehicleFactory.create()的服务构造函数中。注意:如果组件扫描路径上同时包含FooACarFactoryFooATruckFactory,则可以使用@Kenny提到的@Primary注释强制Spring注入所需的注释。或者将jar与正确的实现放在java类路径中。

或者,您可以拥有一个通用工厂,而不是创建汽车工厂的变种:

@Component
public class VehicleFactory {

    @Autowired
    FooAStrategy fooAStrategy;

    @Autowired
    FooBStrategy fooBStrategy;

    boolean buildTruck = true; //get from external system parameter instead

    public  <T> Vehicle<T> getObject(Class<?> clazz) throws Exception {
        if (clazz.equals(FooA.class)) {
            if (buildTruck) {
                return (Vehicle<T>) new Truck<FooA>(fooAStrategy);
            }
            return (Vehicle<T>) new Car<FooA>(fooAStrategy);
        } 
        return (Vehicle<T>) new Car<FooB>(fooBStrategy);
    }

然后在服务类中:

@Service
public class ServiceClassA {
    private FooA fooA;
    private FooB fooB;
    private Vehicle<FooA> vehicle;

    @Autowired
    public ServiceClassA(
            FooA fooA, FooB fooB, 
            CarFactory vehicleFactory
            ) throws Exception {
        this.fooA = fooA;
        this.fooB = fooB;
        this.vehicle = vehicleFactory.<FooA>getObject(FooA.class);
    }
}

答案 1 :(得分:-1)

作为您的评论,我想纠正答案。这也是一样的想法。使用@Primary@Component@Resource

@Primary
@Component("car")
public class Car<T> implements Vehicle<T> {
//...
}

在使用车辆时,看起来像是:

@Service
public class ServiceClassA {
    private FooA fooA;
    private FooB fooB;
    private FooC fooC;

    @Resource(name="car")
    private Vehicle<FooA> vehicle;

...
}

@Primary
@Component("fooAStrategy")
public class FooAStrategy implements Strategy<FooA> {
}

@Component("fooBStrategy")
public class FooBStrategy implements Strategy<FooB> {
}

希望得到这个帮助。