Let's use spring boot to develop a simple "Hello World" web app, built using MAVEN. Before you begin, check your version of Java and Maven to see if it is a supported version of Spring boot1.3:
$ Java-versionjava Version "1.8.0_45" Java (tm) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot (tm) 64-bit Server V M (build 25.45-b02, Mixed mode)
$ mvn-vapache maven 3.2.3 (33F8C3E1027C3DDDE99D3CDEBAD2656A31E8FDF4; 2014-08-12t04:58:10+08:00) maven home:/opt/ Apache-maven-3.2.3java version:1.8.0_45, Vendor:oracle Corporationjava home:/opt/jdk1.8.0_45/jredefault locale:zh_ CN, platform Encoding:utf-8os name: "Linux", Version: "3.13.0-53-generic", Arch: "AMD64", Family: "Unix"
1. Use Eclipse to create a common MAVEN project, packaging as a jar, and write the following 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>net.scanner</groupid > <artifactId>hello_world</artifactId> < version>0.0.1-snapshot</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.0.BUILD-SNAPSHOT</version> </parent> <!-- If you are using the release version, you do not need the following configuration --> <repositories> <repository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </ repository> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </repository> </repositories> <pluginrepositories > <pluginRepository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id> Spring-milestones</id> <url >http://repo.spring.io/milestone</url> </ pluginrepository> </pluginrepositories></project>
2. Add dependencies
example, we use spring-boot-starter-parent in the parent section of Pom.xml. Spring-boot-starter-parent is an important, default parent project that provides the Dependency-management section. If we do this at this time:
$ MVN Dependency:true
You can see spring-boot-starter-parent, which does not provide anything dependent on the jar package. If we add spring-boot-starter-web dependency to Pom.xml (after the parent section):
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artif Actid>spring-boot-starter-web</artifactid> </dependency></dependencies>
You run again:
$ MVN Dependency:true
That's when the log output is different!
The original tomcat is used in embedded mode!
3. Write some source code!
In the Src/main/java directory, we create a Java source file:
Package Hello_world;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.enableautoconfiguration;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RestController, @RestController @enableautoconfigurationpublic class Example {@RequestMapping ("/") String Home () {return "Hello world!"; } public static void Main (string[] args) {Springapplication.run (example.class, args); }}
@restcontroller is an annotation in spring4.0: Restcontroller shows that each method of the class returns an object instead of a view, which is actually a shorthand method for mixing @controller and @responsebody. @RequestMapping will not explain, there is in the SPRINGMVC3! @EnableAutoConfiguration annotation is a class-level annotation that is meant to turn on automatic configuration, which tells the boot to configure the app in a specific way. This approach assumes that the other boilerplate configuration is the default convention for the framework, and of course it is based on the dependency packages you add. In this example, we added the Spring-boot-starter-web dependency, and then embedded Tomcat and spring MVC are automatically dependent, This @enableautoconfiguration annotation assumes that you are developing a Web application and configuring it automatically.
Finally, there is a Main method, which is the portal to the standard Java application. It calls Springapplication.run to boot our application, automatically configures the Tomcat service and starts spring. It requires example.class as a parameter to the Run method, telling Springapplication that it is a major spring component. The args array is passed in as a parameter through the command line.
4. Running the sample
Now that we are using spring-boot-starter-parent, we have a useful run target (Maven target) to launch the application. In the project's root directory, execute the following command:
$ MVN Spring-boot:run
If you open the browser input: localhost:8080/you will see input: Hello world! Press CTRL + C to exit gracefully!
Development of the first spring boot application