SpringBoot first experience (1)

       It mainly introduces the Spring and SpringMVC frameworks. In the process of learning, the friends probably also found that there are many places where these two frameworks need to be manually configured, but the friends who are JavaEE developers must have heard that "convention is greater than The word "configuration" means that the system, class library, and framework should assume reasonable default values, rather than requiring unnecessary configuration. However, if you use Spring or SpringMVC, there are still many such things that we need to configure, which is not only useless It increases the workload and is prone to problems when deploying across platforms. OK, because of these existing problems, Spring Boot came into being. Using Spring Boot allows us to quickly create a Spring-based project, and we only need very little configuration to make this Spring project run. Spring Boot mainly has the following core functions:

1. Standalone Spring project

  Spring Boot can be run in the form of a jar package. To run a Spring Boot project, we only need to run it through the java -jar xx.jar class. Very convenient.

2. Embedded Servlet container

  Spring Boot can embed Tomcat so that we don't need to deploy the project in the form of a war package.

3. Provide starter to simplify Maven configuration

  Using Spring or SpringMVC, we need to add a lot of dependencies, and many of these dependencies are fixed. Here, Spring Boot can help us simplify Maven configuration through starter.

4. Automatically configure Spring 
5. Pre-production application monitoring 
6. No code generation and xml configuration

  OK, you can also search for more detailed advantages and disadvantages of SpringBoot. I will not list them here. Let's take a look at the code.

 

1. Create a WEB project:

mvn archetype:generate -DgroupId=com.zzstxx.springboot -DartifactId=springboot-helloword -DarchetypeArtifactId=maven-archetype-webapp

Directory Structure:

2. Add dependencies

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.zzstxx.springboot</groupId>
	<artifactId>springboot-helloword</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>springboot-helloword Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<!-- Public spring-boot configuration, the following dependent jar files do not need to write the version number-->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<!-- The following information is automatically included: -->
		<!-- 1. Use Java6 compilation level-->
		<!-- 2. Make UTF-8 encoding -->
		<!-- 3. Implemented a common test framework (JUnit, Hamcrest, Mockito). -->
		<!-- 4. Intelligent resource filtering-->
		<!-- 5. Smart plugin configuration (exec plugin, surefire, Git commit ID, shade). -->
		<artifactId>spring-boot-starter-parent</artifactId>
		<!-- spring boot 1.x last stable version-->
		<version>1.4.1.RELEASE</version>
		<!-- Indicates the relative path of the parent module pom, there is no value here -->
		<relativePath />
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<!-- web -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- Exclude tomcat when packaging -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<!-- test-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<!-- Only run in the test test -->
			<scope>test</scope>
		</dependency>
		<!-- spring boot warm start-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>

	<build>
		<finalName>spring-boot-hello-world</finalName>
		<plugins>
			<!-- jdk compilation plugin -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
			<!-- spring boot warm start-->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<fork>true</fork>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

 

3. Application.java This is the entry class of our entire project. This class has a @SpringBootApplication annotation, which is the core annotation of the entire Spring Boot. Its purpose is to enable the automatic configuration of Spring Boot.

package com.zzstxx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Description: It is recommended to put it under the root directory, mainly used for some framework configuration
 * It is @SpringBootApplication:@SpringBootConfiguration+@EnableAutoConfiguration+@ComponentScan, etc.
 */
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

 4. Controller class, which provides external services

package com.zzstxx.springboot;

import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//@RestController is the combination of @Controller+@ResponseBody, which supports RESTful access, and the returned results are all json strings
@RestController
public class HelloWorldController {
	
	/**
	 * Read the configuration information in application.properties
	 */
	@Value("${com.zzstxx.unitmc}")
        private String unitMc;

	@RequestMapping("/hello1/one")
	public String hello1() {
		return "Hello World周旭锋"+unitMc;
	}

	@RequestMapping("/hello2/two")
	public List<String> hello2() {
		return Arrays.asList(new String[] { "A", "B", "C" });
	}
}

 5. Configuration file application.properties

com.zzstxx.unitmc=Zhengzhou Sote Information Technology Co., Ltd.

#log information
logging.path=e:/
logging.level.com.favorites=DEBUG
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=ERROR

#Access port,
server.port=8888
server.context-path=/springboot

 

Original link: https://blog.csdn.net/u012702547/article/details/53740047

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326005689&siteId=291194637