1. Build a MAVEN Project
Click Finish to complete the MAVEN project creation
Add the following code to the Pom.xml file:
1 <Parent>2 <groupId>Org.springframework.boot</groupId>3 <Artifactid>Spring-boot-starter-parent</Artifactid>4 <version>1.4.4.RELEASE</version>5 </Parent>6 <Dependencies>7 <Dependency>8 <groupId>Org.springframework.boot</groupId>9 <Artifactid>Spring-boot-starter-web</Artifactid>Ten </Dependency> One </Dependencies>
The role of spring-boot-starter-parent is to inherit some of the springboot default configurations, such as:
- Java 8 is used by default
- Using UTF-8 encoding
- A reference management function, in the dependencies part of the configuration can not fill in the version information, these version information will be inherited from the spring-boot-dependencies.
- Identify the resource filter (sensible resource filtering.)
- Identifies the configuration of the plug-in (Sensible plugin configuration (exec plugin, surefire, Git commit ID, shade).)
- Ability to identify files of type application.properties and APPLICATION.YML, as well as support for profile-specific types of files (such as: Application-foo.properties and APPLICATION-FOO.YML, this feature can be used to better configure the configuration files in different production environments.
If you do not want to use the default configuration, you can override the default configuration by using a custom configuration, such as changing the default JDK version to 1.7
< Properties > < java.version >1.7</java.version></properties>
Establish the Application.class class, which acts as the initiator of the Springboot
1 Packagemain;2 3 Importorg.springframework.boot.SpringApplication;4 Importorg.springframework.boot.autoconfigure.SpringBootApplication;5 6@SpringBootApplication (scanbasepackages={"Com.myproject"})7 Public classApplication {8 Public Static voidMain (string[] args) {9Springapplication.run (Application.class, args);Ten //The following is whether to disable the boot identity One //springapplication application = new Springapplication (application.class); A //Application.setbannermode (mode.off); - //Application.Run (args); - } the}
Where @SpringBootApplication is a combination of annotations, @SpringBootApplication = (default property) @Configuration + @EnableAutoConfiguration + @ Componentscan.
@Configuration
1, @Configuration: Mention @configuration will mention his partner @bean. Using these two annotations, you can create a simple spring configuration class that can be used in place of the corresponding XML configuration file.
1 <Beans>2 <BeanID= "Car"class= "Com.test.Car">3 < Propertyname= "Wheel"ref= "Wheel"></ Property>4 </Bean>5 <BeanID= "Wheel"class= "Com.test.Wheel"></Bean>6 </Beans>
Equivalent:
1 @Configuration2 Public classConf {3 @Bean4 PublicCar Car () {5Car car =NewCar ();6 Car.setwheel (Wheel ());7 returncar;8 }9 @BeanTen PublicWheel Wheel () { One return NewWheel (); A } -}
The @Configuration annotation class identifies that the class can use the spring IOC container as the source for the bean definition. @Bean note tells Spring that an annotation method with @bean returns an object that should be registered as a bean in the context of the spring application.
2, @EnableAutoConfiguration: The ability to automatically configure the spring context, to try to guess and configure the Bean class you want, is usually automatically configured according to your classpath and your bean definition.
3, @ComponentScan: will automatically scan the specified package under all the @component class, and registered as a bean, of course, including @component sub-annotations @service, @Repository, @Controller.
Note here: The location of the scan package must be followed @SpringBootApplication, otherwise the controller class will not be mapped to the address and cannot be accessed.
Create a new package and set up a controller class
The HelloWorld class is a controller class with the following code:
1 PackageCom.myproject.controller;2 3 Importorg.springframework.web.bind.annotation.RequestMapping;4 ImportOrg.springframework.web.bind.annotation.RestController;5 6 @RestController7 Public classHelloWorld {8 9@RequestMapping ("/hello")Ten PublicString SayHello () { OneSystem.out.println ("HelloWorld"); A return"HelloWorld"; - } -}
@RestController annotation, which indicates that the current class is a controller component, which is a combination of @Controller and @responsebody annotations.
@responseBody annotations are written to the body area of the response object after the object returned by the controller is converted to the specified format by the appropriate converter, usually to return JSON data or XML data.
This annotation indicates that the return result of the method is written directly to the HTTP response body and is generally used when fetching data asynchronously.
After using @requestmapping, the return value is usually resolved to a jump path. After adding @responsebody, the returned result is written directly to the HTTP response body and will not be resolved to a jump path. For example, an asynchronous request that expects the result of a response to be JSON data, then, with @responsebody, will return the JSON data directly.
At this point, a simple Springboot project is set up, Access Http://localhost:8080/hello, display HelloWorld on the page, and print "HelloWorld" in the console.
Build the Spring Boot project