SpringBoot study notes (a, HelloSpringBoot)

A, SpringBoot Profile

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”…Most Spring Boot applications need very little Spring configuration.

This is the introduction of official documents.
Spring the Boot (English is the "boot" meaning), was used to build a simplified Spring application development process. Application out of the box.
In the process of learning SSM (H), it is necessary to do a lot of configuration work, in fact, a lot of configuration act itself is only a means, not an end. Based on this consideration, to simplify the simplified omitted, the omission of the developer only care about providing service functions on the line, this is SpringBoot.
In other words, SpringBoot can simply as simplified, as agreed development SSM (H), can greatly enhance the speed of development.

Second, the use Eclipse to create a Maven project
of this step is the premise of your computer to complete the configuration of Maven.
Details figure:
Here Insert Picture Description

Third, the specific code is
1, pom.xml

<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>edu.hpu</groupId>
  <artifactId>SpringBoot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringBoot</name>
  <!-- Spring Boot 启动父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>
    <dependencies>
        <!-- Spring Boot web依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</project>

2, Controller layer
HelloController.java

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
   @RequestMapping("/hello")
   public String sayHello() {
       return "Hello,SpringBoot!";
   }
}

@RestController and @RequestMapping comment is a comment from the SpringMVC, they are not a specific part of SpringBoot.

(1) @RestController:. Provide an implementation of a REST API, you can serve JSON, XML or other. Here is a String rendering results.

(2) @RequestMapping:. Provides routing information, HTTP Request will "/ hello" is mapped to the path sayHello method for processing.

3, start the application class
Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*Spring Boot启动类
 * 
 */

@SpringBootApplication
public class Application {

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

	}

}

Fourth, run
run is relatively simple, it will run Application.java
Here Insert Picture Descriptiongood, see Application running without problems, visit our Web site: HTTP: // localhost: 8080 / the Hello
Here Insert Picture Description

The problem is not a good start, that this is not done, I made a point of Controller modify, refresh the page, see modify the contents, then run Application.java second time, the results reported the following error.


java.net.BindException: Address already in use: bind
	at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_144]
	at sun.nio.ch.Net.bind(Unknown Source) ~[na:1.8.0_144]
	at sun.nio.ch.Net.bind(Unknown Source) ~[na:1.8.0_144]
	at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source) ~[na:1.8.0_144]
	at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source) ~[na:1.8.0_144]
	at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:340) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
	at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:765) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
	at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:473) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
	at org.apache.catalina.connector.Connector.startInternal(Connector.java:986) [tomcat-embed-core-8.0.32.jar:8.0.32]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) [tomcat-embed-core-8.0.32.jar:8.0.32]
	at org.apache.catalina.core.StandardService.addConnector(StandardService.java:239) [tomcat-embed-core-8.0.32.jar:8.0.32]
	at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.addPreviouslyRemovedConnectors(TomcatEmbeddedServletContainer.java:194) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:151) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:293) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) [spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at edu.hpu.springboot.Application.main(Application.java:13) [classes/:na]

The wrong solution to search a bit, finally found a fly, error 8080 because the port is occupied, Application although not up and running, HTTP: // localhost: 8080 / the Hello can still visit me on SpringBoot operating mechanism It is not clear, but after running before probably know what should be generated, taking up the port. So check:
cmd -> netstat -ano
results:

Here Insert Picture Description
Which process is found to be occupied, open the Task Manager, click on the details
Here Insert Picture Descriptionthat it, javaw.exe, End Task, Application.java run again, visit http: // localhost: 8080 / hello , no problem, Controller of change content is also displayed.

参考:
【1】https://www.w3cschool.cn/springboot/springboot-w7c2245h.html
【2】http://how2j.cn/k/springboot/springboot-eclipse/1640.html
【3】https://www.cnblogs.com/13188196we/p/3278153.html

Guess you like

Origin blog.csdn.net/sinat_40770656/article/details/89328235