Training essay: Starting from scratch Spring project

Source: Internet
Author: User
Tags i18n spring initializr

What is Spring boot?

Springboot is accompanied by the birth of Spring4.0;

Literally, boot is the guiding meaning, and Springboot helps developers quickly build the spring framework;

Springboot helps developers quickly launch a Web container that inherits the good genes from the original spring framework and simplifies the process of using spring.

Spring because of its cumbersome configuration, was once considered "configuration Hell", a variety of XML, annotation configuration, dazzling, and if the error is difficult to find out why.

Spring boot is more about configuring spring with Java CONFIG.

Because spring boot is same strain with spring, the learning cost for spring is almost zero for the vast majority of Java developers.

The key to learning when you practice spring boot, or the way the mindset changes, is:

1) This is especially important for rest understanding, which requires a consensus from the design and development of multiple roles, often in the sense that the HTTP 1.1 protocol and the essence of rest are not understood, resulting in some bad effects of rest being "blinded".

2) for Yaml's understanding and understanding of Javaconfig, these two points are relatively simple, essentially simplifying the XML file and providing an equivalent configuration representation capability.

(I am still too vegetable, although has done a SPRINGMVC small project practiced hand, above some words have not heard to hear)

Start a Spring boot project

1, to IntelliJ idea official website to download the ultimate version of the IDE, because the community version of the default is no spring INITIALIZR to initialize and configure the run of Spring boot.

2, "New Project", "Spring initializr", Java JDK Best Choice 1.8 version, because 1.7 of the version is missing some key content, the runtime may be error.

The Initialized service URL is selected by default.

Click "Next", configure the project basic information, "Type" select "Maven Project", "Language" select "Java", here "Java Version" should be consistent with the previous configuration, select "8".

Click "Next", select the relevant dependent library, tick "Web" "JPA" "MySQL" "JDBC".

Click "Next", set the project name to complete the setup, a spring project is built.

The idea will then download the template from the spring website and apply the settings that you just made, the initial directory structure.

The project structure still looks very refreshing, with a lot less configuration files, let's see what the default build is:

    • DemoApplication: A class with the main () method to start the application
    • Demoapplicationtests: An empty Junit test that loads a spring application context that uses the Spring Boot dictionary configuration feature
    • Application.properties: An empty properties file, you can add configuration properties as needed
    • Pom.xml:Maven Building the documentation

We also need to create a number of classes and folders according to our own needs, the recommended settings are as follows (in fact, at first there are not so many categories):

Structure of the Code layer

Root directory: Com.springboot

1. Engineering Start-up Class (Applicationserver.java) placed under the Com.springboot.build package

2. Entity class (domain) placed in Com.springboot.domain

3. Data access Layer (DAO) placed in Com.springboot.repository

4. Data Service Layer (service) placed in Com,springboot.service, Data Service Implementation Interface (SERVICEIMPL) as for Com.springboot.service.impl

5. Front-end controllers (Controller) placed in the Com.springboot.controller

6. Tool Class (Utils) placed in Com.springboot.utils

7. The Constant interface class (constant) is placed in the Com.springboot.constant

8. Configuration information Class (config) placed in Com.springboot.config

9. Data transmission Class (VO) placed in Com.springboot.vo

Structure of the resource file

Root directory: src/main/resources

1. Configuration files (. Properties/.json, etc.) placed under Config folder

2. Internationalization (i18n)) placed under the i18n folder

3.spring.xml placed under the Meta-inf/spring folder

4. Pages and Js/css/image are placed under the static folder under their respective files

I created the "Entity" Service (data Service layer and interface) under the "src/main/java/root package" directory, stealing a lazy. "Controller" (Front controller) "Repository" (data access Layer)

"Static" in the "src/main/resources" directory holds js/css/image and other files, "Templates" stores HTML pages.

Go ahead and run a Hello world again.

Create a new Hellocontroller.java under controller:

 Package Com.example.demo.Controller; Import Org.springframework.stereotype.Controller; Import org.springframework.web.bind.annotation.RequestMapping; Import Org.springframework.web.bind.annotation.RestController; @RestController  Public class Hellocontroller {    @RequestMapping ("/hello")    public  String Hello () {        return "Hello world!" ;    }}

Then the browser accesses localhos:8080/hello,perfect!

The next time you detail how to write a Spring boot project.

Resources:

Spring Boot project directory structure

Spring boot talk (what/what to do/pros and disadvantages)

Spring Boot "Quick Start"

Training essay: Starting from scratch Spring project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.