依赖关系使用注释将一个bean注入其他bean

时间:2016-08-22 00:42:00

标签: java spring dependency-injection annotations

我有以下基于spring注释的配置:

@Bean
public MarshallingMessageConverter marshallingMessageConverter() {
    return new MarshallingMessageConverter();
}
@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
    return new Jaxb2Marshaller();
}

在Spring初始化jaxb2Marshaller之前,我需要使用基于setter的注入将marshallingMessageConverter注入marshallingMessageConverter。经过很多谷歌搜索仍然找不到这种情况。请帮忙!!

更新:我没有访问这两个类,因为它们在jar文件中。当我在第三类中将它们自动化时,例如:

@Autorwired MarshallingMessageConverter converter;  //I need something like @Autorwired(setMarshaller = jaxb2Marshaller) where setMarshaller is the setter inside `MarshallingMessageConverter`

@Autowired Jaxb2Marshaller marshaller;

2 个答案:

答案 0 :(得分:0)

您可以将<script> jQuery(function() { var $ = jQuery; $(document).on('touchstart mouseover', 'a#wsite-com-minicart-checkout-button', function(e) { var totalCount = $( "span#wsite-nav-cart-num" ).text(); var totalNeeded = 12; if (totalCount < totalNeeded) { var totalItems = totalNeeded - totalCount; $("a#wsite-com-minicart-checkout-button").click(function(event){ event.stopImmediatePropagation(); alert('Orders under ' + totalNeeded + ' packages are only available for pickup. Click "Order for Pickup" to proceed to checkout. If not, click "OK" and add ' + totalItems + ' more packages for us to ship your order.'); return false; }); } }); }); </script> 传递给转换器构造函数吗?如果是这样,您可以使用autowired创建编组器,然后在使用@Bean注释的方法中传入构造函数:

marshaller

答案 1 :(得分:0)

@Bean
public MarshallingMessageConverter marshallingMessageConverter(Jaxb2Marshaller marshaller) {
    MarshallingMessageConverter converter = new MarshallingMessageConverter();
    converter.whatEverMethodCallIsNeeded(marshaller);
    return converter;
}