Autowiring beans from another module

I have a large application that I want to break into managed modules. I am using spring with Jpa (Hibernate as a provider). I came up with a structure in which I have a main module containing all entity classes and Tao, while other modules use the main module regarding persistence, and each of them will have its own set of service classes and controllers.

enter image description here

All Jpa and spring configuration files are in the main module. With this installation, I have to deal with the problem of autowiring dao beans in modules using the main module. So my question is: is it possible to autwire beans from the main module in other modules (or, perhaps, use the context between the modules)? I am also open to suggestions regarding structure if there is a better way to do this.

thank

+5
source share
1 answer

The main module must be the parent Spring context, which must be installed in each child context. So no ploblem with autowiring

Each child context can reach all beans from the parent, but must know that this parent does not see children

, , : i. .

  • , Spring Webapps
  • , Spring xml , :

    ClassPathXmlApplicationContext parentAppContext = new ClassPathXmlApplicationContext();
    parentAppContext.setConfigLocation("spring-core.xml"); // this is your core spring xml
    parentAppContext.refresh();
    ClassPathXmlApplicationContext moduleAppContext = new ClassPathXmlApplicationContext();
    moduleAppContext.setConfigLocation("others.xml");
    moduleAppContext.setParent(parentAppContext);
    moduleAppContext.refresh();
    
0

All Articles