Monday, December 10, 2012

Spring Social with JavaConfig (Part 5)


In the previous section, we have discussed the Spring Security-related configurations. In this section we will discuss the remaining configuration of our application.

Table of Contents

Click on a link to jump to that section:
  1. Functional Specs
  2. Generate OAuth keys
    • Facebook
    • Twitter
  3. Spring Social configuration
  4. Spring Security configuration
  5. JavaConfig
  6. View with Thymeleaf
  7. Layers
    • Domain
    • Repository
    • Service
    • Controller
  8. 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


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).

The is the equivalent of web.xml. Here's where we declare the DispatcherServlet and also we've registered two filters: one for Spring Security and another for Spring Social.

Here's the equivalent XML configuration:

The 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({DataConfig.class, ThymeleafConfig.class, SocialConfig.class, SecurityConfig.class})
    - This allows us to import JavaConfig-based config. Notice we have imported four configuration classes
  • @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("")
    - This allows us to import property files
  • @Bean
    - Declares a Spring bean

Here's the equivalent XML configuration:

The 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:

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

We've declared some special settings on our Thymeleaf configuration:
// Declare virtual paths

// Disable cache for testing purposes

These allows to redirect virtual path requests to specific templates within our application. We need to do this because the ConnectController from Spring Social has its own built-in controller path requests. And we need to redirect the resulting views that matches our template path.

For example, when connecting to Facebook, ConnectController will use the connect/facebookConnect path and you are required to provide a view. Using the addTemplateAlias() method, we can provide a custom view, in this case, the view points to the directory in the WEB-INF/templates/facebook/connect.

Also, we've disabled the caching feature so that we can easily update and test our html pages.

Here's the equivalent XML configuration:

The contains a single bean DelegatingFilterProxy. This is required for Spring Security. contains the property settings of our application. You need to declare your Facebook and Twitter OAuth settings here. Here's also where you declare your database.

This is for internationalization of messages. The default language is English. If you need to provide custom language, create a new properties file and replace the values according to the language you've chosen. Please see the Spring documentation for more info on internationalization.


In the next section, we will discuss the Domain, Service, Controller, and View layers. Click here to proceed.
StumpleUpon DiggIt! Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring Social with JavaConfig (Part 5) ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share


  1. Good Post. really It takes such plenty time to push and notice traffic from social media, the matter is it never consistent. we have a tendency to should take under consideration Organic searches whereas victimization social name and getting links from good social networks like Google and can not merely offer some traffic but collectively good links to diary.

  2. Pretty section of content. I just stumbled upon your website and in accession capital to
    assert that I acquire actually enjoyed account your blog posts.
    Visit our php tutorial

  3. I have read your blog its very attractive and impressive. I like it your blog.

    Spring online training Spring online training Spring Hibernate online training Spring Hibernate online training Java online training

    spring training in chennai spring hibernate training in chennai

  4. thanks for grate tutorial


  5. مدونة العاب فلاش الصعيدي تقدم لكم مجموعة العاب بنات 2018 اكثر من رائعة ومميزة جدا ونتمني ان تنال اعجابكم لانها مجموعة مميزة جدا وايضا نقدم لكم العاب بنات 2017 كما تقدم لكم المدونة العاب طبخ 2018 مدونة العاب فلاش الصعيدي رائعة جدا ونتمني منك ان تلقي نظرة عليها انها حقا رائعة واكثر من رائعة

  6. I was recommended to this site by our Thesis and Dissertation Proposal Editors and I have proved that this is a great site with interesting content that is worth reading and commenting on. I will bookmark this site so that I can visit I occasionally to read new information.

  7. Ever wanted to get free Google+ Circles?
    Did you know you can get them AUTOMATICALLY & ABSOLUTELY FREE by getting an account on Like 4 Like?

  8. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    core java training in Electronic City

    Hibernate Training in electronic city

    spring training in electronic city

    java j2ee training in electronic city