3

I am creating spring boot application. In this application there have set of logic class and this logic class instance create 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 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.

EDIT:

public ILogic getLogicInstance(String className) {
    try {
        String packageName = MultiTenantManager.currentTenant.get();//  this return required logic class included  package name. 
        Class<?> constructors = Class.forName("lk.example."+packageName+"."+className);
        return (ILogic) constructors.getConstructor().newInstance();
    } catch () {}
}
  • Hi, welcome to StackOverflow. Please show the code where you create the `ILogic` instance. If you just create it manually via constructor, then Spring won't be able to inject any of its dependencies. You have to request the instance from Spring's bean context. – Forketyfork Mar 02 '19 at 14:32
  • Thanks for the reply. I edit the question and add that code. – Ridma Tharanga Mar 02 '19 at 14:52
  • `ARepository extend extends`? `implement`? that is not even a compilant code. Please, paste the valid one. – Andronicus Mar 02 '19 at 14:53
  • Andronicus sorry, it's mistake. it should `extends` – Ridma Tharanga Mar 02 '19 at 14:57

1 Answers1

5

Spring can't inject anything in an instance that you create yourself with new or via reflection.

One of the ways to do what you want would be to request the beans from the application context, something along the following lines:


@Autowired 
private ApplicationContext applicationContext;

public void createUser(Class<?> beanClass) {
    ILogic logic = applicationContext.getBean(beanClass);
    logic.saveUser();
}

Forketyfork
  • 7,416
  • 1
  • 26
  • 33
  • Thanks for your quick response and the answer. ( In hear I want to create `ALogic ` class instance this class should annotated as @Component("ALogic") `beanClass = ALogic` ) – Ridma Tharanga Mar 02 '19 at 23:14