Create a Spring Boot Application part1

Create a Spring Boot Application

Now, let’s create a spring boot application and go through more details. The following application is created in IntellijIDEA 15 CE. The project is developed based on JDK 1.8 and uses maven 4.
First of all, create a Maven project in your IDEA and configure the pom.xml file to include all required dependencies in the project. In this tutorial, we use spring-boot-1.3.3-RELEASE to configure the spring boot application. Also, we use webjars libraries to include all necessary js files for Angularjs.
pom.xml
01<?xml version="1.0" encoding="UTF-8"?>
02<project xmlns="http://maven.apache.org/POM/4.0.0"
03         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05    <modelVersion>4.0.0</modelVersion>
06
07    <groupId>springboot-angularjs</groupId>
08    <artifactId>springboot-angularjs</artifactId>
09    <version>1.0-SNAPSHOT</version>
10
11    <parent>
12        <groupId>org.springframework.boot</groupId>
13        <artifactId>spring-boot-starter-parent</artifactId>
14        <version>1.3.3.RELEASE</version>
15    </parent>
16    <dependencies>
17        <dependency>
18            <groupId>org.springframework.boot</groupId>
19            <artifactId>spring-boot-starter-web</artifactId>
20        </dependency>
21        <dependency>
22            <groupId>org.webjars</groupId>
23            <artifactId>angularjs</artifactId>
24            <version>1.4.9</version>
25            <scope>runtime</scope>
26        </dependency>
27        <dependency>
28            <groupId>org.webjars</groupId>
29            <artifactId>bootstrap</artifactId>
30            <version>3.3.6</version>
31            <scope>runtime</scope>
32        </dependency>
33    </dependencies>
34</project>
WebJars is simply taking the concept of a JAR and applying it to client-side libraries or resources. For example, the Angularjs library may be packaged as a JAR and made available to your Spring Boot application. Many WebJars are available through Maven Central with a GroupID for org.webjars. A complete list is available at webjars.org.
JavaScript package management is not a new concept. In fact, npm and bower are two of the more popular tools, and currently offer solutions to managing JavaScript dependencies. Spring’s Understanding JavaScript Package Managers guide has more information on these. Most JavaScript developers are likely familiar with npm and bower and make use of those in their projects. However, WebJars utilizes Maven’s dependency management model to include JavaScript libraries in a project, making it more accessible to Java developers.

Spring boot application configuration

The SpringApplication class provides a convenient way to bootstrap a Spring boot application that will be started from a main() method. In many situations you can just delegate to the static SpringApplication.run method similar to the following class:
WebAppInitializer.java
01package com.javacodegeeks.examples;
02
03import org.springframework.boot.SpringApplication;
04import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
05import org.springframework.context.annotation.ComponentScan;
06import org.springframework.context.annotation.Configuration;
07
08@Configuration
09@EnableAutoConfiguration
10@ComponentScan("com.javacodegeeks.examples")
11public class WebAppInitializer{
12
13    public static void main(String[] args) throws Exception{
14        SpringApplication.run(WebAppInitializer.class, args);
15    }
16}
@Configuration tags the class as a source of bean definitions for the application context.
@EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans and various property settings.
@ComponentScan tells Spring to look for other components, configurations and services in the specified package which allows the application to find the MainController.
By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext. Here, the static content is under the /resourcesdirectory.

 A simple Controller

The following class is only a simple controller which is implemented to handle the request to '/' and render the request to index.html.
MainController.java
01package com.javacodegeeks.examples.controller;
02
03import org.springframework.stereotype.Controller;
04import org.springframework.web.bind.annotation.RequestMapping;
05import org.springframework.web.bind.annotation.RequestMethod;
06
07@Controller
08public class MainController {
09
10    @RequestMapping(value="/",method = RequestMethod.GET)
11    public String homepage(){
12        return "index";
13    }
14}

continue on ,

Create a Spring Boot Application part2

Comments

Popular Posts