gpt4 book ai didi

java - spring-boot: Autowiring 2 个变量但指向一个 bean

转载 作者:行者123 更新时间:2023-11-29 02:58:03 24 4
gpt4 key购买 nike

例如,有2个 bean 。parentClass 是泛型类

// parentClass
@Service
public class ParentService<T> {
public ParentService(){
System.out.println("ParentService: class"+this.getClass()+" "+this);
}
}

//子类subClass扩展了parentClass并表明泛型是一个“字符串”类型

@Service
public class ChildService extends ParentService<String> {
public ChildService(){
System.out.println("ChildService: class"+this.getClass()+" "+this);
}
}

//测试用例 Autowiring 泛型为 String 的 subClass 和 parentClass

@RunWith(SpringRunner.class)
@SpringBootTest
public class OneBeanApplicationTests {
@Autowired
private ChildService childService;
@Autowired
private ParentService<String> stringParentService;

@Test
public void contextLoads() {
System.out.println(childService == stringParentService);// true
}

}

答案是:正确
我对此感到困惑

=====================================如果我编辑测试类

    @Autowired
private ParentService parentService;

......

parentService==stringParentService; // false


这是示例项目:https://github.com/AshameL/WhyIsSameBean你可以拉它并运行测试类

最佳答案

我创建了一个 Controller 类并尝试了这个

@Autowired
ParentService<String> stringParentService;

@Autowired
ChildService childService;

@Autowired
ParentService parentService; // Object class



@GetMapping("/test123")
public void contextLoads() {
System.out.println(childService.hashCode()+" : "+stringParentService.hashCode());
System.out.println(childService == stringParentService);
System.out.println(childService.equals(stringParentService));
System.out.println(Integer.toHexString(System.identityHashCode(childService)));
System.out.println(Integer.toHexString(System.identityHashCode(stringParentService)));
System.out.println("=====================");
System.out.println(parentService == stringParentService);
System.out.println(parentService.hashCode()+" : "+stringParentService.hashCode());
}

OUTPUT:

563182512 : 563182512
true
true
21917bb0
21917bb0
=====================
false
196061929 : 563182512

当我们有 ParentService<String>, 时这是预期的因为哈希码没有被覆盖,父类和子类共享相同的对象

如果是ParentService默认类型 pass 是 Object,因此不同的哈希码和不同的 Object。

编辑 1:

在服务器启动期间我可以看到以下日志

ParentService:   classclass com.example.demo.service.ChildService  com.example.demo.service.ChildService@2dc6b83f
ChildService: classclass com.example.demo.service.ChildService com.example.demo.service.ChildService@2dc6b83f
ParentService: classclass com.example.demo.service.ParentService com.example.demo.service.ParentService@349131e3

当我用下面的代码做同样的事情时

public static void main(String[] args) {
ParentService parentService = new ChildService();
ParentService parentService1 = new ParentService();
}

OUTPUT:

ParentService: classclass com.example.demo.service.ChildService com.example.demo.service.ChildService@1d44bcfa
ChildService: classclass com.example.demo.service.ChildService com.example.demo.service.ChildService@1d44bcfa
ParentService: classclass com.example.demo.service.ParentService com.example.demo.service.ParentService@266474c2

结论是

@Autowired
ChildService childService;

@Autowired
ParentService<String> stringParentService;

ChildService的实例由于扩展类。

关于java - spring-boot: Autowiring 2 个变量但指向一个 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59495721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com