Review
In the previous section, we have discussed the Domain, Repository, Service, Controller layers. In this section, we will build and run our sample application. We will verify if our application is able to communicate with Facebook and Twitter. We will also show how to import the project in Eclipse.
Table of Contents
Click on a link to jump to that section:
- Functional Specs
- Generate OAuth keys
- Spring Social configuration
- Spring Security configuration
- JavaConfig
- ApplicationInitializer.java
- ApplicationContext.java
- DataConfig.java
- ThymeleafConfig.java
- spring.properties
- View with Thymeleaf
- Layers
- Domain
- Repository
- Service
- Controller
- 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
Running the Application
Clone from GitHub
To clone from GitHub, follow these instructions:- Open a Git terminal
- Enter the following command:This will clone the JavaConfig-based application.
git clone https://github.com/krams915/spring-social-javaconfig.git
If you prefer the XML-based version,git clone https://github.com/krams915/spring-social-xmlconfig.git
Remember:
There are two versions of our application: a JavaConfig-based and an XML config-based app. Both versions are identical in their feature set.
Create the Database
- Run MySQL
- Create a new database:
spring_social_tutorial
- Import the following SQL files:
spring_social_tutorial.sql
JdbcUsersConnectionRepository.sql
These files can be found at the src/main/resources path
Run with Maven and Tomcat 7
Ensure Maven is installed first, and you have created the MySQL database- Open a terminal
- Browse to the directory where you've cloned the project
- Enter the following command:
mvn tomcat7:run
- You should see the following output:
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------------------------------------------
[INFO] Building spring-social-tutorial Maven Webapp 0.0.1-SNAPSHOT
[INFO] -------------------------------------------------------------
[INFO]
[INFO] >>> tomcat7-maven-plugin:2.0:run (default-cli) @ spring-social-tutorial >>>
...
...
...
Dec 10, 2012 9:50:56 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Dec 10, 2012 9:50:56 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Dec 10, 2012 9:50:56 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.30
Dec 10, 2012 9:51:07 AM org.apache.catalina.core.ApplicationContext log
INFO: Spring WebApplicationInitializers detected on classpath: [org.krams.config.ApplicationInitializer@73b8cdd5]
Dec 10, 2012 9:51:08 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Dec 10, 2012 9:51:18 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Dec 10, 2012 9:51:18 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"] - Open a browser
- Visit the entry page:
http://localhost:8080/spring-social-tutorial
- The primary admin credentials are the following:
username: john
password: admin
You can create a new account but by default it doesn't have any admin powers.
Run with Maven and Jetty 8
Ensure Maven is installed first, and you have created the MySQL database.- Open a terminal
- Browse to the directory where you've cloned the project
- Enter the following command:
mvn jetty:run
- You should see the following output:
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------------------------------------------
[INFO] Building spring-social-tutorial Maven Webapp 0.0.1-SNAPSHOT
[INFO] -------------------------------------------------------------
[INFO]
[INFO] >>> jetty-maven-plugin:8.1.5.v20120716:run (default-cli) @ spring-social-tutorial >>>
...
...
...
2012-12-10 09:53:55.980:INFO:/spring-social-tutorial:Initializing Spring FrameworkServlet 'dispatcher'
2012-12-10 09:53:56.140:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server - Open a browser
- Visit the entry page:
http://localhost:8080/spring-social-tutorial
- The primary admin credentials are the following:
username: john
password: admin
You can create a new account but by default it doesn't have any admin powers.
Import to Eclipse
Ensure Maven is installed first- Open a terminal
- Enter the following command:
mvn eclipse:eclipse -Dwtpversion=2.0
- You should see the following output:
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------------------------------------------
[INFO] Building spring-social-tutorial Maven Webapp 0.0.1-SNAPSHOT
[INFO] -------------------------------------------------------------
[INFO]
[INFO] >>> maven-eclipse-plugin:2.9:eclipse (default-cli) @ spring-social-tutorial >>>
...
...
...
[INFO] -------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] -------------------------------------------------------------
[INFO] Total time: 9.532s
[INFO] Finished at: Mon Dec 10 09:55:31 PHT 2012
[INFO] Final Memory: 17M/81M
[INFO] -------------------------------------------------------------
This command will add the following files to your project:.classpath
You may have to enable "show hidden files" in your file explorer to view them.
.project
.settings
target - Run Eclipse and import the application as Maven project
Validate with W3C Markup Validation Service
One of the promises of using Thymeleaf is it produces valid HTML pages. Let's test that out using W3C validation service.- Run the application
- Open a browser
- Visit the following URLs and validate them all:
http://localhost:8080/spring-social-tutorial/
http://localhost:8080/spring-social-tutorial/login
http://localhost:8080/spring-social-tutorial/users
http://localhost:8080/spring-social-tutorial/fb/profile
http://localhost:8080/spring-social-tutorial/tw/profile
http://localhost:8080/spring-social-tutorial/fb/post
http://localhost:8080/spring-social-tutorial/tw/post - View the HTML source (right-click or go to menu)
- Copy the HTML source
- Open W3C Markup Validation Service at http://validator.w3.org/#validate_by_input
- Paste the HTML source and wait for the validation result
You should see a similar output:
Conclusion
We've have completed our Spring Social-based application using JavaConfig. We're able to post and retrieve profile information from Facebook and Twitter. To provide authentication and security we've added Spring Security. For managing the view layer, we've integrated Thymeleaf as our template engine. As a bonus, we've also provided an XML-based application.
I hope you've enjoyed this tutorial. Don't forget to check my other tutorials at the Tutorials section.
Revision History
Revision | Date | Description |
---|---|---|
1 | Dec 10 2012 | Uploaded tutorial and GitHub repositories |
2 | Dec 21 2012 | Update to Spring 3.2.0.RELEASE |
0 komentar:
Post a Comment