原创

Spring 中按参数名称自动装配是如何工作的?

温馨提示:
本文最后更新于 2024年04月12日,已超过 48 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

I have the following working example where beans are autowired by parameter name. Note that it is working and no qualifiers used.

How this can be done? I can't reproduce it in my project.

@Component
public class MyComponent {
    public MyComponent(
        // other parameters...
        Counter myCounterOk,
        Counter myCounterFail,
        // remaining parameters...
    ) {
        // Constructor body
    }
}

public class MyConfiguration {

    @Bean
    public Counter myCounterOk() {
        return new Counter(); // Assuming Counter instantiation is correct
    }

    @Bean
    public Counter myCounterFail() {
        return new Counter(); // Assuming Counter instantiation is correct
    }
    
}
正文到此结束
热门推荐
本文目录