[英]spring-boot: autowired 2 variables but directing to ONE bean
例如,有 2 个豆子。 parentClass 是泛型类
// parentClass
@Service
public class ParentService<T> {
public ParentService(){
System.out.println("ParentService: class"+this.getClass()+" "+this);
}
}
//subClass subClass 扩展了 parentClass 并指示泛型是“字符串”类型
@Service
public class ChildService extends ParentService<String> {
public ChildService(){
System.out.println("ChildService: class"+this.getClass()+" "+this);
}
}
// TestCase 自动装配了 subClass , parentClass 泛型是 String
@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你可以拉它并运行测试类
我创建了一个控制器类并尝试了这个
@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中“==”比较对象引用。
由于 ChildService 从 ParentService 扩展,它们本质上可能具有对内存中相同对象的引用,因此在使用“==”符号比较它们时您可能会得到正确的结果。
Spring 中的默认范围是单例,因此您将通过自动ChildService
获得相同的ChildService
实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.