Springboot Entry 1 hello world !, Springboothello

Source: Internet
Author: User

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-tomcatApplication. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.