Does Spring create new instances of constructor parameters of the same type for different beans?
For example, I have two REST controllers:
First:
@RestController
@RequestMapping ("/getHwidData")
public class GetHwidDataController {
private final ApiKeysDatabase apiKeysDb;
private final BanwareDatabase banwareDb;
@Autowired
public GetHwidDataController(ApiKeysDatabase apiKeysDb, BanwareDatabase banwareDb) {
this.apiKeysDb = apiKeysDb;
this.banwareDb = banwareDb;
}
}
Second:
@RestController
@RequestMapping ("/setHwidData")
public class SetHwidDataController {
private final ApiKeysDatabase apiKeysDb;
private final BanwareDatabase banwareDb;
@Autowired
public SetHwidDataController(ApiKeysDatabase apiKeysDb, BanwareDatabase banwareDb) {
this.apiKeysDb = apiKeysDb;
this.banwareDb = banwareDb;
}
}
As you may see, both controllers' constructors are @Autowired
, and both accept the same object types: an ApiKeysDatabase
and a BanwareDatabase
.
I have some caching and other instance-dependent stuff inside those *Database
classes, so I'd like to know: when the two above-stated REST controllers are created, will their apiKeysDb
and banwareDb
fields be respectively equal (hold the same instance of the ApiKeysDatabase
and BanwareDatabase
objects respectively)? That is,
GetHwidDataController ctrlOne = ...
SetHwidDataController ctrlTwo = ...
assertTrue ctrlOne.apiKeysDb == ctrlTwo.apiKeysDb
&& ctrlOne.banwareDb == ctrlTwo.banwareDb