How to get the bean the generic type without unchecked?

Hello everybody.
There is some class with a parameterized type Storage.
All three steps lead to a warning. unchecked cast.
Storage storage = ctx.getBean("storageMemory");
Storage storage = ctx.getBean(Storage.class);
Storage storage = ctx.getBean("storageMemory", Storage.class);
Actually a question:
How to get the bean the generic type without unckecked? Annotation or xml - does not matter.
Is there any option but to make a storage box of some class-container autowired?
June 3rd 19 at 19:14
1 answer
June 3rd 19 at 19:16
Why for example not to make the heirs of the type
public class UserStorage extends Storage<user> {

and work directly with them?
Yes, it's one of the options. But imagine that you have in the system 50 data models. Have to write 50 classes that extend the Storage that contains a lot of duplicate code (violating DRY). - Taylor_Lockman commented on June 3rd 19 at 19:19
well, duplicate code, place in an abstract class. but Yes, if a lot of models - is not very nice.

public class MyConfiguration {

 public Storage<user> userStorage() {
 return new Storage<person>();

 public Storage<customer> customerStorage() {
 return new Storage<customer>();

Another solyushin. But the same code is duplicated - Melba.Gottlieb commented on June 3rd 19 at 19:22
Yes , I know the solution. Factory method instance. Described here:
1.3.2. Instantiating beans - Taylor_Lockman commented on June 3rd 19 at 19:25

Find more questions by tags SpringJava