Springboot Entry 1 hello world !, Springboothello
The microservice framework springboot is designed to simplify the initial setup and development process of new Spring applications. The cost of simplification is defined as many rules. For example, the configuration file name read by default is that application. properties must be in the config directory, and the scanning of the startup class is in the same level and sub-directory. Springboot is not a new solution to existing problems, but a new experience for platform development, simplifying complicated xml and other configuration information with little changes. The conventions are better than the configurations.
Boot simplifies the development of Spring applications, provides the ability to import dependencies in a modular manner, emphasizes the development of RESTful Web services and provides the ability to generate runable jar, all of this clearly shows that the Boot framework is a powerful tool for developing deployable microservices.
To implement a url: http: // localhost: 8080/index, the returned string is hello world !,
Previous practice: Configure web. xml spring-***. xml and then combine tomcat or jetty application server.
Spring boot (maven Project)
I. pom. xml reference package
<Parent>
<GroupId> org. springframework. boot </groupId>
<ArtifactId> spring-boot-starter-parent </artifactId>
<Version> 1.5.6.RELEASE </version>
</Parent>
<Dependencies>
<Dependency>
<GroupId> org. springframework. boot </groupId>
<ArtifactId> spring-boot-starter-web </artifactId>
</Dependency>
</Dependencies>
2. Add controller
@ RestController
Public class HomeController {
@ RequestMapping ("index ")
Public String index (){
Return "hello world! ";
}
}
3. Add a startup class
@ SpringBootApplication
Public class AppBootApplication {
Public static void main (String [] args ){
SpringApplication. run (AppBootApplication. class, args );
}
}
4. Run the startup class.
Is it very simplified? No spring-related xml files are configured throughout the process. In higher spring versions, annotations are increasingly used to replace xml configurations. Default built-inspring-boot-starter-tomcat
Application. The default port is 8080.
@ RestController is an encapsulation annotation. The set @ Controller + @ ResponseBody identifies all such routing methods and returns a string
@ SpringBootApplication is also an encapsulation annotation, @ Configuration @ EnableAutoConfiguration @ ComponentScan
@ Configuration the identification class can use Spring IoC container as the source of bean Definition
@ EnableAutoConfiguration can automatically configure the spring context, which is usually automatically defined based on your class path and your bean.
@ ComponentScan automatically scans all classes marked with @ Component under the specified package and registers them as beans.
There are three ways to run the service
1. Run the startup class and directly run the main method.
2. Use mvn spring-boot: run in the command line.
3. A jar file can be generated and executed separately.
Add a plug-in pom. xml
<Build>
<Plugins>
<Plugin>
<GroupId> org. springframework. boot </groupId>
<ArtifactId> spring-boot-maven-plugin </artifactId>
</Plugin>
</Plugins>
</Build>
Then compress the package into a jar package: mvn package.
Run: java-jar spring-boot-1.0.0-SNAPSHOT.jar.