Monday, December 10, 2012

Spring Social with JavaConfig (Part 1)

In this tutorial, we will create an application that can post messages and retrieve profile information from Facebook and Twitter. We will use Spring Social to implement these features. To secure our application we will use Spring Security, and to manage our views, we will use Thymeleaf.

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
    • ApplicationInitializer.java
    • ApplicationContext.java
    • DataConfig.java
    • ThymeleafConfig.java
    • spring.properties
  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

Dependencies

These are the main Maven dependencies:
  • Spring 3.2.0.RELEASE
  • Spring Data JPA 1.2.0.RELEASE
  • Spring Security 3.1.3.RELEASE
  • Thymeleaf 2.0.14
  • Hibernate 3.6.3.Final
  • See pom.xml for full details

Required Tools

These are the minimum required tools:
  • Git
  • Maven 3.0.4
  • MySQL
  • Eclipse IDE or SpringSource Tool Suite (STS)

GitHub Repository

There are two versions of the application: a JavaConfig-based and an XML config-based app. Both versions are identical in their feature set.

Functional Specs


Our application's requirements are the following:
  • Post to Facebook and Twitter
  • Retrieve profile information from Facebook and Twitter
  • Secure the application
  • Allow login and creation of new users
  • Create a page for managing users

Here's our Use Case diagrams:


[User]-(Post to Facebook)
[User]-(Post to Twitter)
[User]-(Retrieve info from Facebook)
[User]-(Retrieve info from Twitter)
[User]-(Sign in)
[User]-(Sign up)

//http://yuml.me/



[Admin]-(Edit users)
[Admin]-(Delete users)
[Admin]-(Add users)

//http://yuml.me/

Screenshots


Before we proceed, let's preview some screenshots of our application:

Sign in page


Sign up


Facebook Profile


Twitter Profile


Manage Users


Post to Facebook


Tweet to Tweeter


Connect to Social Site


Connected to Social Site



Next

In the next section, we will show how to generate the OAuth secret keys for Facebook and Twitter. Click here to proceed.

StumpleUpon DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring Social with JavaConfig (Part 1) ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share

48 comments:

  1. I am great fan of you. I learnt spring hibernate just because of your tutorials.
    Thanks a lot. I keep waiting every weekend to see new post of you

    ReplyDelete
    Replies
    1. dileep, thanks for the support. I will try my best to make new ones, but sometimes real life projects take a good chunk of my time :)

      Delete
    2. Hi
      I want only to know how to parse json array or any object in jqgrid table with one to many relation table as same in hibernate one to many sample. I am having problem when parsing data for one person to many credit card. If you get time then post only parsing logic.

      Delete
  2. Best tutorial of the year about Spring Security + Thymeleaf + Social
    I'll be your big fan too!
    Thank you!

    ReplyDelete
  3. i will be good fan of you i am daily browse your blog

    ReplyDelete
  4. BEST TUTORIAL BLOG I HAD EVER VISITED....
    I AM BEGINNER TO ANY FRAMEWORK ,SO I NEED YOUR MORE SUGGESTIONS IF YOU CAN AND I AM ADDICTED TO JAVA RELATED STUFFS...

    ReplyDelete
  5. Thanks for all your tutorial. It really helped me love Spring.

    Do you have any Spring Security + Struts Integration Tutorial? Or do you know a good source. I kinda in confusion with that matter.

    ReplyDelete
  6. Thanks for your detailed explanation! I wanted to try out your app, but was stopped by this error:

    (default-war) on project spring-social-tutorial: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)

    when doing 'mvn clean install -e' I understand that the WebAppInitialzer replaces web.xml so not sure why this error is showing up. Any sugestions?

    ReplyDelete
    Replies
    1. No worries. I set failOnMissingWebXml to false in your pom and now it works.

      Delete
  7. I've said it before and I'll say it again :: Mark You Need to Write a Book on learning the SpringFramework ... You are a great tut writer .... and I have nearly every_single_good_book on spring ....

    Thanks Buddy ...

    EJB

    ReplyDelete
    Replies
    1. Edward, thanks for the comment. You know I'm really thinking of writing a book, but I think I can approach more people if I write on the web, and the content will be open to comments and suggestions. I was approached once to lead a book on Spring-related cookbook but I have to turned it down because of time constraints. I just got a new work here in Chicago :-)

      Delete
  8. No problem Mark ... I find it quite awesome of you to write these ... I'd move cross country to get a good gig developing in Spring. I've worked with a lot of different web-frameworks and Spring is my favorite thus far. I like nearly every aspect about it (shameless pun) ...

    Thanks again Mark look forward to more when you get time ...

    ;-)

    ReplyDelete
  9. Another great tutorial! I am just wondering whether the project can be built with Java 7 or not.

    ReplyDelete
  10. It is always fun reading your blog. Keep up with good work

    ReplyDelete
  11. After cloning the Java configuration version, I can't make a build. The following is an error message:

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.3:war (default-war) on project spring-social-tutorial: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]

    I add the maven-war-plugin part into the pom file due to a build error. The problem is due to missing web.xml file, which is the nature of the version. How to let this error go away?

    ReplyDelete
  12. I will try to study this. This is useful for projects that are open for public. As most people have already social accounts

    ReplyDelete
  13. how to add the image in using web config

    ReplyDelete
  14. Thanks for telling about this application.. social ecommerce

    ReplyDelete
  15. Thanks for your tutorials.

    ReplyDelete
  16. Excellent tutorial..it helped a lot..thanks Mark.Tutorial was very helpful especially xml config example ,good learning experience

    ReplyDelete
  17. I hope I could have found your site earlier. Your tutorial is really helpful. Unlike other "hello world" tutorial, your tutorial comes with very complicated examples but with detailed and easy to understand documents. It's nice. Hope you can continue to write more.

    ReplyDelete
  18. Thank you for sharing valuable information. Nice post. I enjoyed reading this post.
    Signature:
    i like play games games 2 girls Download baixar facebook movel

    ReplyDelete
  19. Thanks for sharing this tutorial.
    More Spring Core Information can be found also at
    www.springmockexams.com

    @DavidMayer

    ReplyDelete
  20. Thanks for sharing this quality information with us. I really enjoyed reading. I think I need it.
    Games for girls|
    Kizi 1|
    Kizi 2

    ReplyDelete
  21. Thanks for sharing the information. It is very useful for my future. keep sharing
    Kizi 123|
    Friv 234|
    Friv

    ReplyDelete
  22. Thanks for your great article friend, i get new information, new ideas to do somethings, i hope you will share again, i keep waiting for next post, thanks.
    Friv 4
    Kizi 3

    ReplyDelete
  23. I love all the posts, I really enjoyed, I would like more information about this, because it is very nice., Thanks
    Friv 10

    ReplyDelete
  24. Nice Information you have written here. Really Great Stuff. I keep it bookmark for our future purpose.

    We are also Web development Company in India who provide the services in Android App Development in Nagpur , SEO Company in Nagpur , Ecommerce Website Development in Nagpur. Visit Us today

    AceZed IT Solution

    ReplyDelete
  25. Thank you for sharing a great information and useful. it really necessary and timely for me at this time. I wanted to share this information with my friends on the social network facebook.!
    Friv 10000
    Friv

    ReplyDelete
  26. Spring Social is essential for integrating Java (Java EE Training) with social media like facebook, twitter etc. To secure our application we will use Spring Security (Java Training Institutes in Chennai) , and to manage our views, we will use Thymeleaf (Java Training in Chennai)

    ReplyDelete
  27. What a wonderfull post! thank you... read with pleasure!
    Friv 1

    ReplyDelete
  28. You need to kill time, you need entertainment. Refer to our website. hope you get the most comfort.
    Thanks you for sharing!
    Yepi
    Kizi
    Friv
    Friv Games

    ReplyDelete
  29. you'd have time to look these kids active. Please visit our website and let us play the game interesting.
    Thanks for sharing !
    Friv 10
    Kizi 10
    Yepi 2

    ReplyDelete
  30. You are stressed and tired, you need entertainment. Refer to our website. hope you get the most comfortable.
    Kizi 4

    ReplyDelete
  31. Hello There. I found your blog using msn. This is an extremely well written article.
    like buy

    ReplyDelete
  32. What a wonderfull post! thank you... read with pleasure!
    Friv 100

    ReplyDelete
  33. thank you for your interest in welcome visit
    Friv 4

    ReplyDelete
  34. You need to kill time, you need entertainment. Refer to our website. hope you get the most comfort.
    Thanks for sharing !
    Friv 5
    Kizi 1
    Yepi 3

    ReplyDelete
  35. New attractive game . I will play it every day
    Friv|
    Friv 2

    ReplyDelete
  36. All member visite new matka begest site satta matka & best profit matka king satta matka

    ReplyDelete
  37. Thanks for posting this useful info. It really helped me!!!
    Friv
    Friv.com
    Friv 2016

    ReplyDelete
  38. Play the Best Free Games! We've picked out the racing games, cooking games, candy crush, games shooting, fashion games, ...
    Thanks for sharing !
    Kizi 200
    Friv 1
    Y88
    Y9

    ReplyDelete
  39. You want to relax after a stressful working hours. Refer to our website. hope you get the most comfort.
    Thanks for sharing !
    Friv 100
    Friv 8
    Friv 2

    ReplyDelete
  40. invites you to play games for a chance to win cars Corolla Altis car

    Friv Games
    Kizi 100
    Games2Win
    Kizi 5

    ReplyDelete
  41. You want to relax after a stressful working hours. Refer to our website. Hope you get the most comfort.
    Thanks for sharing !
    GirlsGoGames
    Kizi 100
    Frvi

    ReplyDelete