无法自动装配相同类型的2个配置Bean

时间:2012-10-24 02:35:54

标签: spring annotations

我有两个在HandlerConfiguration.java中定义的GenericHandlerResolver bean,如:

@Bean(autowire = Autowire.BY_NAME)
@Scope(value = "prototype")
public GenericHandlerResolver defaultHandlerResolver() {
    return new GenericHandlerResolver(){{
        setHandlers(new ArrayList<Handler>(){{
            add(loggingHandler());
            add(sessionHandler());
        }});
    }};
}

@Bean(autowire = Autowire.BY_NAME)
@Scope(value = "prototype")
public GenericHandlerResolver maskingHandlerResolver() {
    return new GenericHandlerResolver(){{
        setHandlers(new ArrayList<Handler>(){{
            add(maskingLoggingHandler());
            add(sessionHandler());
        }});
    }};
}

然后我想将两个不同的bean自动装入WebServiceConfiguration.java并在几十个JaxWsPortProxyFactoryBean bean中使用它们,如:

@Configuration
public class WebServiceConfiguration {
...

public Integer paymentServiceTimeout;
@Bean @DependsOn("applicationProperties")
public Map<String, Object> paymentServiceProperties(){
    return new HashMap<String, Object>(){
        {put(timeoutKey, paymentServiceTimeout);}
    };
}

...
// Logging handler resolver:    
/*@Autowired
protected GenericHandlerResolver defaultHandlerResolver;*/

// Logging handler resolver that will mack Credit Cards:
@Autowired
protected GenericHandlerResolver maskingHandlerResolver;

...

@Bean
@Lazy
@DependsOn("applicationProperties")
public JaxWsPortProxyFactoryBean paymentServicePort() throws Exception {
    JaxWsPortProxyFactoryBean jppfb = new JaxWsPortProxyFactoryBean() {{
        setServiceInterface(PaymentServicePortType.class);
        setWsdlDocumentUrl(new URL(paymentServiceEndpoint + "?wsdl"));
        setServiceName("PaymentService");
        setEndpointAddress(paymentServiceEndpoint);
        setCommonProperties(this,
                paymentServiceProperties(),
                maskingHandlerResolver,
                LEGACY_NAMESPACE);
    }};
    jppfb.afterPropertiesSet();
    return jppfb;
}

protected void setCommonProperties(JaxWsPortProxyFactoryBean bean,
                                   Map<String, Object> customProperties,
                                   GenericHandlerResolver handlerResolver,
                                   String namespace) {
    bean.setMaintainSession(false);
    bean.setLookupServiceOnStartup(false);
    bean.setCustomProperties(customProperties);
    bean.setHandlerResolver(handlerResolver);
    bean.setNamespaceUri(namespace);
}

我的问题是这可以在XML配置中工作,但现在我将它移到javaConfig,如果我在WebServiceConfiguration.java中有多个GenericHandlerResolver,处理程序不起作用(没有记录),但我没有得到任何错误消息,所以我不知道发生了什么。

1 个答案:

答案 0 :(得分:0)

尝试使用资源注释而不是自动连接:

@Resource(name="defaultHandlerResolver")
protected GenericHandlerResolver defaultHandlerResolver;

@Resource(name="maskingHandlerResolver")
protected GenericHandlerResolver maskingHandlerResolver

或者,您可以将@Qualifier与@Autowired

一起使用
@Autowired
@Qualifier("defaultHandlerResolver")
protected GenericHandlerResolver defaultHandlerResolver;*/

@Autowired
@Qualifier("maskingHandlerResolver")
protected GenericHandlerResolver maskingHandlerResolver;