samedi 2 mars 2019

@Autowired not work inside class that class instance create using reflection

I am creating spring boot application. In this application there have set of logic class and this logic class class using reflection(base on some condition logic is change that's the reason i use to reflection) In side logic class i try to Autowired my repository class but it not work. That logic class annotated using @Component but it not working.

is there have any way to do that. spring use reflection to autowired class therefor i don't know after using reflection myself, use @Autowired possible or not.

Code :

interface ILogic{
    void saveUser();
    // set of methods
}

@Repository
interface ARepository extend extends JpaRepository<ARepository,Integer>{}

@Component
class ALogic implement ILogic{
    private final ARepository aRepository;
    private final BRepository bRepository;
    @Autowired
    public Alogic(ARepository aRepository, BRepository bRepository){
    // code stuff 
    }
    // implementation of methods in ILogic interface
} 

@Component
class BLogic implement ILogic{
    private final ARepository aRepository;
    private final BRepository bRepository;
    @Autowired
    public Alogic(ARepository aRepository, BRepository bRepository){
        // code stuff 
    }
    // implementation of methods in ILogic interface
}  

class CreateConstructor{
    public ILogic getLogicInstance(){
        // give logic class instance base on some condition
    }
}

@Service
class userService{

    CreateConstructor createConstructor= new CreateConstructor();
    public void saveUser(){
        createConstructor.getLogicInstance().saveUser();
    }
}

repository class instance are not create inside Logic class.





Aucun commentaire:

Enregistrer un commentaire