Sunday, December 2, 2012

Spring and Thymeleaf with JavaConfig (Part 3)

Review


In the previous section, we focused on the view layer and created an HTML mockup template. In this section, we will focus on configuration and declare them using JavaConfig. We will also provide an XML-based config.

Table of Contents

Click on a link to jump to that section:
  1. Functional Specs
  2. Creating the View
    • HTML Mockup
    • Thymeleaf Integration
  3. JavaConfig
    • ApplicationContext.java
    • SpringDataConfig.java
    • ThymeleafConfig.java
    • ApplicationInitializer.java
  4. Layers
    • Domain
    • Service
    • Controller
  5. Running the application
    • Clone from GitHub
    • Create the Database
    • Run with Maven and Tomcat 7
    • Run with Maven and Jetty 8
    • Import to Eclipse
    • Validate with W3C

JavaConfig


As stated in the introduction, we will be using JavaConfig-based configuration instead of the usual XML config. However, I don't want to alienate our readers who are used to XML. As a result, I've decided to provide both implementations. However, our focus here is still on JavaConfig.

Note: With JavaConfig, we can now omit the ubiquitous web.xml. But in order to that, we need to run a Servlet 3.0 web container. For this tutorial, I have tested the application with Tomcat 7.0.30 (Maven plugin), 7.0.33 (standalone Tomcat), and Jetty 8.1.5.v20120716 (Maven plugin).

ApplicationContext.java


The ApplicationContext.java contains our main configuration. It's responsible for loading other configurations, either as JavaConfig or XML config.



Let's describe each annotation:
  • @Configuration
    - Marks a class as a JavaConfig
  • @ComponentScan(basePackages = {"org.krams"})
    - Configures scanning of Spring components

    This is equivalent in XML as:

  • @EnableWebMvc

    - Activates Spring's MVC support

    This is equivalent in XML as:

  • @Import({SpringDataConfig.class, ThymeleafConfig.class})
    - This allows us to import JavaConfig-based config. Notice we are importing two external configuration classes: SpringDataConfig and ThymeleafConfig
  • @ImportResource("classpath:trace-context.xml")
    - This allows us to import XML-based config files. (As a side note why can't we just declare this as a JavaConfig? It turns out there's no direct translation for the trace-context.xml, so we'll have to import it as XML).

    This is equivalent in XML as:

  • @PropertySource("classpath:spring.properties")
    - This allows us to import property files
  • @Bean
    - Declares a Spring bean

Here's the equivalent XML configuration:



SpringDataConfig.java


The SpringDataConfig.java contains our Spring Data configuration. This is where we declare our data source, transaction manager, and JPA entity manager.



Here's the equivalent XML configuration:



ThymeleafConfig.java


The ThymeleafConfig.java contains our Thymeleaf configuration. This is where we declare our Thymeleaf view resolver.



Here's the equivalent XML configuration:



ApplicationInitializer.java


The ApplicationInitializer.java is the equivalent of web.xml. Here's where we declare the DispatcherServlet.



Here's the equivalent XML configuration:



Next

In the next section, we will focus on the remaining layers of our Java application. We'll study the Domain, Repository, Service, and Controller layers. Click here to proceed.

9 comments:

  1. Hello, i am using apache tile, thymeleaf and jsp view resolver together, but getting an error. Following is the link for detail of my question.

    http://stackoverflow.com/questions/28494408/integrate-thymeleaf-tiles-and-jsp-view-resolver-together-in-spring-mvc

    ReplyDelete
  2. Boost your career with our salesforce development course
    designed to teach you hands-on skills in Apex, Lightning, and CRM customization. Gain practical expertise and become a certified Salesforce developer ready to excel in the tech industry.

    ReplyDelete
  3. Boost your career with our salesforce admin course
    designed to equip you with essential skills in CRM management, automation, and reporting. Gain hands-on experience and become a certified Salesforce Admin, ready to streamline business processes efficiently.

    ReplyDelete
  4. Advance your data skills with a comprehensive tableau course online
    designed to help you master dashboards, visual analytics, and real-time reporting from anywhere. This flexible tableau course online offers hands-on projects and expert guidance to boost your career in business intelligence and data visualization.

    ReplyDelete
  5. salesforce administrator classes
    provide hands-on training to help professionals master user management, data security, automation, and reporting within the Salesforce platform.

    ReplyDelete
  6. salesforce dev training
    equips aspiring developers with the skills to build, customize, and deploy applications on the Salesforce platform using Apex, Lightning Web Components, and integrations.

    ReplyDelete
  7. Great post! dell boomi training online
    helps IT professionals automate workflows and manage APIs efficiently.

    ReplyDelete