How to do it...

Let us now apply the JavaConfig specification in building the Spring context definition:

  1. To get rid of the usual Maven bugs, immediately open the pom.xml of ch02-jc and add <properties>, <dependencies>, and <repositories>, equivalent to what was added in the Implementing the Spring Container using XML recipe.
  2. Since the time Servlet 3.0 specification was implemented, servlet containers can now support projects without using web.xml. This is done by implementing the handler abstract class called org.springframework.web.WebApplicationInitializer to programmatically configure ServletContext. Create a SpringWebInitializer class and override its onStartup() method without any implementation yet:
public class SpringWebInitializer implements 
   WebApplicationInitializer { 
     
     @Override 
     public void onStartup(ServletContext container) throws 
         ServletException { 
       } 
} 
  1. The lines in Step 2 will generate some runtime errors until you add the following Maven dependency:
<dependency> 
  <groupId>org.springframework</groupId> 
  <artifactId>spring-web</artifactId> 
  <version>${spring.version}</version> 
</dependency>
  1. In pom.xml, disable the <failOnMissingWebXml> and add the same Tomcat Maven plugin for the deployment used in Chapter 1, Getting Started with Spring.
  1. Now, create a class named BeanConfig, the ApplicationContext definition, with a class-level annotation @Configuration. The class must be inside the org.packt.starter.ioc.context package and must be an empty class at the moment:
@Configuration 
public class BeanConfig {   } 
  1. Save all the files and clean and build the Maven project.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.51.153