0

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.

John Martin
  • 173
  • 1
  • 5

1 Answers1

0

you should annotate OtherClass with @Component.
And i had a try.here is my code,hope this could help you.

public class AbstractClass {
}
@Component
public class OtherClass extends AbstractClass {

    @Autowired
    UserRepository userRepository;

    public UserRepository getUserRepository() {
        return userRepository;
    }
}
@Component
public class TempFactory {
    private final Map<Class, AbstractClass> impletationMap = new HashMap<>();

    @Autowired
    ApplicationContext context;

    public Map<Class, AbstractClass> getImpletationMap() {
        return impletationMap;
    }

    @PostConstruct
    public void initialize() {
        populateDataMapperMap(context.getBeansOfType(AbstractClass.class).values().iterator());
    }

    private void populateDataMapperMap(final Iterator<AbstractClass> classIterator) {
        while (classIterator.hasNext()) {
            AbstractClass abstractClassImpl = classIterator.next();
            impletationMap.put(abstractClassImpl.getClass(), abstractClassImpl);
        }
    }

}
@Repository
public class UserRepository {
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext.xml")
public class ATest {
    @Autowired
    TempFactory tempFactory;

    @Test
    public void test() {
        OtherClass otherClass = (OtherClass) tempFactory.getImpletationMap().get(OtherClass.class);
        UserRepository userRepository = otherClass.getUserRepository();
        Assert.assertNotNull(userRepository);
    }

}
denny
  • 44
  • 2