加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – 如何依赖注入依赖注入在Spring @Bean方法参数中工作

发布时间:2020-12-15 01:35:28 所属栏目:大数据 来源:网络整理
导读:我理解Spring DI以及它的工作原理. 但是我在这里无法理解的是@Bean方法参数注入的情况,spring如何知道参数名称,以便它可以根据参数的名称从bean的工厂注入bean? 例如,在以下示例中,方法fernas1和fernas2参数在运行时被擦除.但是,spring仍然可以将正确的Abba

我理解Spring DI以及它的工作原理.

但是我在这里无法理解的是@Bean方法参数注入的情况,spring如何知道参数名称,以便它可以根据参数的名称从bean的工厂注入bean?

例如,在以下示例中,方法fernas1和fernas2参数在运行时被擦除.但是,spring仍然可以将正确的Abbas bean实例注入其中.

@SpringBootApplication
public class DemoApplication {

    @Autowired
    private Abbas abbas1;    // this is understandable,hence the field name is available at runtime

    @Autowired
    private Abbas abbas2;   // this is understandable,hence the field name is available at runtime

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(DemoApplication.class,args);

        Map

编辑:@Javier的相同问题和解决方案都适用于方法和构造函数参数.

最佳答案
如果参数名称反射不可用,它将使用类文件本身中的信息.见DefaultParameterNameDiscoverer

Default implementation of the ParameterNameDiscoverer strategy
interface,using the Java 8 standard reflection mechanism (if
available),and falling back to the ASM-based
LocalVariableTableParameterNameDiscoverer for checking debug
information in the class file.

例如,DemoApplication.fernas2的LocalVariableTable是

    Start  Length  Slot  Name   Signature
        0      16     0  this   Lcom/example/demo/DemoApplication;
        0      16     1 abbas2   Lcom/example/demo/DemoApplication$Abbas;
        9       7     2 fernas2   Lcom/example/demo/DemoApplication$Fernas;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读