Spring boot entry example

wrote
Spring Boot is a new framework provided by the Pivotal team, designed to simplify the initial setup and development of new Spring applications. The framework uses a specific way to configure, so that developers no longer need to define boilerplate configuration. In this way, Boot aims to be a leader in the burgeoning field of rapid application development.
You can build the project with Maven | Gradle | Ant | Starters, refer to: http://start.spring.io/  You can choose Maven or Gradle to generate the Demo, and the Spring boot microservice architecture runs with the Docker container.
  For software version running requirements, please refer to the official website:  Spring boot official website
  Software version of this example: JDK1.7 + Spring boot 1.3.5 + Spring 4.2.6 

  Commonly used starters and their uses can be listed as follows:

  • spring-boot-starter: This is the core Spring Boot starter that provides most of the basic functionality, other starters depend on it, so there is no need to define it explicitly.
  • spring-boot-starter-actuator: mainly provides functions to monitor, manage and review applications.
  • spring-boot-starter-jdbc: This starter provides support for JDBC operations, including connecting to the database, operating the database, and managing database connections, etc.
  • spring-boot-starter-data-jpa: The JPA starter provides dependency libraries that use the Java Persistence API (such as Hibernate, etc.).
  • spring-boot-starter-data-*: Provides support for MongoDB, Data-Rest or Solr.
  • spring-boot-starter-security: Provides all Spring-security dependencies.
  • spring-boot-starter-test: This starter includes spring-test dependencies and other test frameworks such as JUnit and Mockito, etc.
  • spring-boot-starter-web: This starter includes dependencies for web applications.

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>ygnet</groupId>
	<artifactId>boot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>Springboot</name>
	<url>http://maven.apache.org</url>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.7</java.version>
	</properties>
	
	<!-- Spring Boot starts parent dependencies -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.5.RELEASE</version>
		<relativePath />
	</parent>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>4.2.6.RELEASE</version>
        </dependency>
        	
		<!-- Spring Boot web dependencies-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>	
		<!--dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId><scope>runtime</scope>
		</dependency>
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
	</dependencies>
	<build>
	    <pluginManagement>  
	        <plugins>  
	          <plugin>  
	            <groupId>org.eclipse.m2e</groupId>  
	            <artifactId>lifecycle-mapping</artifactId>  
	            <version>1.0.0</version>  
	            <configuration>  
	              <lifecycleMappingMetadata>  
	                <pluginExecutions>  
	                  <pluginExecution>  
	                    <pluginExecutionFilter>  
	                      <groupId>org.apache.maven.plugins</groupId>  
	                      <artifactId>maven-dependency-plugin</artifactId>  
	                      <versionRange>[2.0,)</versionRange>  
	                      <goals>  
	                        <goal>copy-dependencies</goal>  
	                      </goals>  
	                    </pluginExecutionFilter>  
	                    <action>  
	                      <ignore />  
	                    </action>  
	                  </pluginExecution>  
	                </pluginExecutions>  
	              </lifecycleMappingMetadata>  
	            </configuration>  
	          </plugin>  
	        </plugins>  
	    </pluginManagement>
		<plugins>
		    <!-- Jar package (META-INF) -->
		    <plugin>
	            <groupId>org.apache.maven.plugins</groupId>  
	            <artifactId>maven-jar-plugin</artifactId>  
	            <configuration>  
	                <archive>  
	                    <manifest>  
	                       <addClasspath>true</addClasspath>  
	                       <classpathPrefix>lib/</classpathPrefix>  
	                       <mainClass>yg.boot.App</mainClass>  
	                    </manifest>  
	                </archive>  
	            </configuration>  
	        </plugin>
	        <!-- Project resource file-->
			<plugin>  
			    <groupId>org.apache.maven.plugins</groupId>  
			    <artifactId>maven-resources-plugin</artifactId>  
			    <version>2.5</version>  
			    <executions>  
			        <execution>  
			            <phase>compile</phase>  
			        </execution>  
			    </executions>  
			    <configuration>  
			        <encoding>${project.build.sourceEncoding}</encoding>  
			    </configuration>  
			</plugin>
			<!-- Whether to start the test-->
		    <plugin>
		        <groupId>org.apache.maven.plugins</groupId>  
		        <artifactId>maven-surefire-plugin</artifactId>
		        <version>2.17</version>
		        <configuration>
		          <skipTests>true</skipTests>  
		        </configuration>
		    </plugin>
		    <!-- Copy the dependencies to the project lib folder-->
			<plugin>  
			    <groupId>org.apache.maven.plugins</groupId>  
			    <artifactId>maven-dependency-plugin</artifactId>  
			    <version>2.8</version>  
			    <executions>  
			        <execution>  
			            <phase>package</phase>  
			            <goals>  
			                <goal>copy-dependencies</goal>  
			            </goals>  
			        </execution>  
			    </executions>
			    <configuration>
			    	<outputDirectory>${project.basedir}/lib</outputDirectory>
			        <includeScope>compile</includeScope>  
			    </configuration>  
			</plugin>
			<!-- Spring boot packaging -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
Controller
The mechanism provided by the Spring Boot framework is convenient for engineers to implement standard RESTful interfaces and write Controller code. First, we need to add the corresponding starter to the pom file, that is, spring-boot-starter-web. The corresponding xml code example is:
<dependency> 
< groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
The @RestController annotation is a collection of @Controller and @ResponseBody, indicating that this is a controller bean and is Filling the return value of the function directly into the HTTP response body is a REST-style controller.
@RequestMapping("/test" ) indicates that the controller handles all "/test" URL requests, and which function handles them, depending on the HTTP method: GET means query, POST means submit, PUT means update, and DELETE means delete.
Please refer to the Restful design guide: The role of the RESTFul
Controller, as you can see, I confuse a lot of business code in the code of the Controller. In fact, according to the article " Programmers must know the history of front-end evolution , what the Controller layer should do is: Render and redirect the parameters of the request, select Model and Service, and deal with Session and Cookies. I basically agree with this point of view, plus at most OAuth authentication (implemented with an interceptor). The real business logic should be handled in a separate layer, that is, the common service layer;

 

package yg.boot.action;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@RequestMapping("/test")
public class AppController {	
	@RequestMapping("/sayhello")
	public String sayHello(){
		return "Hello World!";
	}
}

 

Spring Boot startup writes
@SpringBootApplication is that this annotation is the sign of the entry of the application, and then there is a familiar main function to run the Spring Boot application through SpringApplication.run(xxxApplication.class, args). Open the SpringBootApplication annotation and you can find that it is composed of several other classes: @Configuration (equivalent to the xml configuration file in spring, using Java files for configuration can check type safety), @EnableAutoConfiguration (automatic configuration), @ComponentScan (Component scanning, which everyone is very familiar with, can automatically discover and assemble some beans)
    As you can see in the pom file, the scope of the org.postgresql library is runtime, that is, when the application starts, if Spring Boot is in The existence of org.postgresql is detected under the classpath, and the postgresql database connection will be automatically configured

 

# DataSource settings
spring.datasource.url=jdbc:postgresql://localhost:5432/jcbk
spring.datasource.username=jcbk
spring.datasource.password=123456
spring.datasource.driver-class-name=org.postgresql.Driver

# Tomcat Server settings (ServerProperties)
server.port= 9080
server.address= 127.0.0.1
server.sessionTimeout= 30
server.contextPath= /

# Tomcat specifics
tomcat.accessLogEnabled= false
tomcat.protocolHeader= x-forwarded-proto
tomcat.remoteIpHeader= x-forwarded-for
tomcat.basedir=
tomcat.backgroundProcessorDelay=30 \# secs

 

package yg.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * Hello world!
 */
@SpringBootApplication
public class App {	
    public static void main(String[] args ){
    	SpringApplication.run(App.class,args);
    }
}
wrote
After running the app directly, the result is as shown below. After startup, visit http://localhost:9080/test/sayhello, and output Hello World!





 
 

 

The project package writes
The project can be packaged using the maven-jar-plugin plugin to generate boot-0.0.1-SNAPSHOT.jar. The spring-boot-maven-plugin plugin renames boot-0.0.1-SNAPSHOT.jar to boot-0.0.1-SNAPSHOT.jar.original, and then generates a new boot-0.0.1-SNAPSHOT.jar package with a directory structure of :
+---yg
          boot
+---org
          springframework
              boot
                 loader
+----lib
+----META-INF
+----application.properties

 

Manifest-Version: 1.0
Implementation-Vendor: Pivotal Software, Inc.
Implementation-Title: Springboot
Implementation-Version: 0.0.1-SNAPSHOT
Implementation-Vendor-Id: ygnet
Built-By: oy
Build-Jdk: 1.7.0_45
Class-Path: lib/spring-test-4.2.6.RELEASE.jar lib/spring-core-4.2.6.RE
 LEASE.jar lib/spring-boot-starter-web-1.3.5.RELEASE.jar lib/spring-bo
 ot-starter-tomcat-1.3.5.RELEASE.jar lib/tomcat-embed-core-8.0.33.jar
 lib/tomcat-embed-el-8.0.33.jar lib/tomcat-embed-logging-juli-8.0.33.j
 ar lib/tomcat-embed-websocket-8.0.33.jar lib/spring-boot-starter-vali
 dation-1.3.5.RELEASE.jar lib/hibernate-validator-5.2.4.Final.jar lib/
 validation-api-1.1.0.Final.jar lib/jboss-logging-3.3.0.Final.jar lib/
 classmate-1.1.0.jar lib/jackson-databind-2.6.6.jar lib/jackson-annota
 tions-2.6.6.jar lib/jackson-core-2.6.6.jar lib/spring-web-4.2.6.RELEA
 SE.jar lib/spring-aop-4.2.6.RELEASE.jar lib/aopalliance-1.0.jar lib/s
 pring-beans-4.2.6.RELEASE.jar lib/spring-context-4.2.6.RELEASE.jar li
 b/spring-webmvc-4.2.6.RELEASE.jar lib/spring-expression-4.2.6.RELEASE
 .jar lib/spring-boot-starter-1.3.5.RELEASE.jar lib/spring-boot-1.3.5.
 RELEASE.jar lib/spring-boot-autoconfigure-1.3.5.RELEASE.jar lib/sprin
 g-boot-starter-logging-1.3.5.RELEASE.jar lib/logback-classic-1.1.7.ja
 r lib/logback-core-1.1.7.jar lib/slf4j-api-1.7.21.jar lib/jcl-over-sl
 f4j-1.7.21.jar lib/jul-to-slf4j-1.7.21.jar lib/log4j-over-slf4j-1.7.2
 1.jar lib/snakeyaml-1.16.jar lib/spring-boot-starter-jdbc-1.3.5.RELEA
 SE.jar lib/tomcat-jdbc-8.0.33.jar lib/tomcat-juli-8.0.33.jar lib/spri
 ng-jdbc-4.2.6.RELEASE.jar lib/spring-tx-4.2.6.RELEASE.jar lib/postgre
 sql-9.4.1208.jre7.jar lib/spring-boot-starter-actuator-1.3.5.RELEASE.
 jar lib/spring-boot-actuator-1.3.5.RELEASE.jar
Start-Class: yg.boot.App
Created-By: Apache Maven 3.0.4
Spring-Boot-Version: 1.3.5.RELEASE
Main-Class: org.springframework.boot.loader.JarLauncher
Archiver-Version: Plexus Archiver

    Start-Class is the Spring boot startup class, and Main-Class is the main method entry

    Please download the attachment for the relevant source code

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327037848&siteId=291194637