How to do it...

Adding more ApplicationContext definition files might require some changes to be made on the servlet and Spring containers. Follow the given steps:

  1. In the ch02-web-xml project, it is recommended to create another XML definition file for application-related model objects such as Employee and Department. Using STS Eclipse or a template, define beans-context.xml and save the file in the directory where the root context is located:
  2. Open beans-context.xml, together with the required XSD schemas and metadata including all the Employee- and Department-related bean objects created in the previous recipes:
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns:context="http://www.springframework.org/schema/context" 
   xmlns:mvc="http://www.springframework.org/schema/mvc" 
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
   http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/ 
spring-context.xsd 
http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 
 
    <bean id="empRec1" 
      class="org.packt.starter.ioc.model.Employee" /> 
    
    
   <bean id="empRec2" 
      class="org.packt.starter.ioc.model.Employee"> 
      // refer to sources 
   </bean> 
   <bean id="empRec3" 
      class="org.packt.starter.ioc.model.Employee"> 
      // refer to sources 
      </bean> 
      // refer to sources 
    </beans> 
  1. All ApplicationsContexts are considered child-contexts of WebApplicationContext. The listener org.springframework.web.context.ContextLoaderListener is responsible for the initialization of both root and child Spring containers with the help of the DispatcherServlet. Thus, register ContextLoaderListener together with the ApplicationContext beans-context.xml in the ServletContext by adding the following configuration in web.xml:
<context-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/beans-context.xml</param-value> 
</context-param> 
<listener> 
   <listener-class> 
org.springframework.web.context.ContextLoaderListener 
</listener-class> 
</listener> 
  1. The context parameter contextConfigLocation may look familiar and it is still the placeholder for all the context files needed to be loaded to the servlet container. If there are other context files, we can accommodate them in contextConfigLocation as:
<context-param> 
   <param-name>contextConfigLocation</param-name> 
   <param-value>/WEB-INF/beans-context.xml 
                /WEB-INF/xxx-context.xml 
                /WEB-INF/yyy-context.xml 
</param-value> 
</context-param> 
  1. The next changes must be reflected inside the root definition file. Since WebApplicationContext is the parent of ApplicationContextbeans-context.xml, it recognize all the loaded beans of its child. We attach the ApplicationContext definition to the root context ch02-servlet.xml through <import> metadata invoked by the root context itself:
<import resource="beans-context.xml"/>
  1. To test whether beans-context.xml is considered a valid child ApplicationContext, create a BeanController and fetch all objects through @Autowired, @Inject, and @Resource as follows:
@Controller 
public class BeanController { 
 
@Autowired 
@Qualifier(value="empRec2") 
private Employee empRecs; 
 
@Inject 
private Department dept2; 
 
@Resource(name="listEmployees") 
private ListEmployees listEmps; 
 
} 
  1. For @Inject to work properly, attach the following Maven dependencies for JSR-330 to pom.xml:
<dependency> 
  <groupId>javax.inject</groupId> 
  <artifactId>javax.inject</artifactId> 
  <version>1</version> 
</dependency> 
  1. To further validate the correctness of our context configuration, create a @Component class DataService in the org.packt.starter.ioc.model package:
@Component 
public class DataService { 
    
   public String getTitle(){ 
      return "Spring 5.0 Cookbook"; 
   } 
} 
  1. Let us @Autowire the DataService in BeanController. If the controller can successfully fetch DataService then we can say that our WebApplicationContext is still functional after the configuration:
@Autowired 
private DataService dataService;
  1. At this point, we introduce org.springframework.ui.Model and org.springframework.ui.ModelMap, which are both responsible for transporting our model objects to different views for rendering. Add the following handler methods to our BeanController to complete our MVC code snippet:
@RequestMapping("/list_emps.html") 
   public String showEmployee(ModelMap model){ 
      model.addAttribute("firstName", empRecs.getFirstName()); 
      model.addAttribute("title", dataService.getTitle()); 
      return "list-employees"; 
   } 
    
   @RequestMapping("/show_dept.html") 
   public String showDepartment(Model model){ 
      model.addAttribute("deptNo", dept2.getDeptNo()); 
      return "show-dept"; 
   } 
    
   @RequestMapping("/view_emps.html") 
   public String viewEmps(Model model){ 
      model.addAttribute("empList", listEmps.getListEmps()); 
      return "view-emps"; 
   } 
  1. In ch02-web-jc, not many changes need to be done because the only way to add a child ApplicationContext is to create a definition similar to our SpringDispatcherConfig, only with a different component package to scan. Create the child context class SpringContextConfig with the inclusion of all the @Beans created previously:
@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages = "org.packt.starter.ioc.model") 
public class SpringContextConfig { 
    
    @Bean(name="empRec1") 
    public Employee getEmpRecord1(){ 
       Employee empRec1 = new Employee(); 
       return empRec1; 
    } 
 
    @Bean(name="empRec2") 
    public Employee getEmpRecord2(){ 
       // refer to sources 
       return empRec2; 
    } 
    
    
    @Bean(name="empRec3") 
    public Employee getEmpRecord3(){ 
       // refer to sources 
       return empRec3; 
    } 
    
    @Lazy 
    @Bean 
    public Employee empRec4(){ 
       // refer to sources 
       return empRec4; 
    } 
} 
  1. Perform Steps 4 to 7 for the rest of the JavaConfig project.
  2. Build and deploy both the projects. Test both the projects and run all the URLs.