I want to autowire repository in class which is initialized by factory pattern.
Let me explain step by step.
My repository class:
@Repository
public class UserRepository {
//
}
Other class that uses the repository class and extend some Abstract class:
public class OtherClass extends AbstractClass {
@Autowired
private UserRepository userRepository;
//
}
Now I have created on Factory class which Initialized this OtherClass and put in one map.
@Component
public class TempFactory {
private final Map<Class, AbstractTest> impletationMap = new HashMap<>();
@Autowired
ApplicationContext context;
@PostConstruct
public void initialize() {
populateDataMapperMap(context.getBeansOfType(AbstractTest.class).values().iterator());
}
private void populateDataMapperMap(final Iterator<AbstractClass> classIterator) {
while (classIterator.hasNext()) {
AbstractTest abstractClassImpl = (AbstractClass) classIterator.next();
impletationMap.put(abstractClassImpl.getClass(), abstractClassImpl);
}
}
}
Now the problem here is that object which put in impletationMap is not autowire the userRepository and return null.
Output:
Otherclass oc = (AbstractClass)impletationMap.get("key")
userRepository is null in oc object.
I come to this solution by This link
Please tell me how to proceed further.
Thank You.