首页 > 代码库 > SpringBoot学习helloworld

SpringBoot学习helloworld

  这几天开始学习springBoot记录一下(Hello World)

   
     pom.xml

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4     <groupId>org.bianqi.spring.first</groupId>
 5     <artifactId>SpringBootFirst</artifactId>
 6     <version>0.0.1-SNAPSHOT</version>
 7     <packaging>war</packaging>
 8     <parent>
 9         <groupId>org.springframework.boot</groupId>
10         <artifactId>spring-boot-starter-parent</artifactId>
11         <version>1.4.1.RELEASE</version>
12     </parent>
13     <dependencies>
14         <dependency>
15             <groupId>org.springframework.boot</groupId>
16             <artifactId>spring-boot-starter-web</artifactId>
17         </dependency>
18     </dependencies>
19     <build>
20         <plugins>
21             <plugin>
22                 <groupId>org.apache.maven.plugins</groupId>
23                 <artifactId>maven-compiler-plugin</artifactId>
24                 <configuration>
25                     <source>1.7</source>
26                     <target>1.7</target>
27                 </configuration>
28             </plugin>
29             <plugin>
30                 <groupId>org.springframework.boot</groupId>
31                 <artifactId>spring-boot-maven-plugin</artifactId>
32                 <configuration>
33                     <mainClass>${start-class}</mainClass>
34                     <layout>ZIP</layout>
35                 </configuration>
36                 <executions>
37                     <execution>
38                         <goals>
39                             <goal>repackage</goal>
40                         </goals>
41                     </execution>
42                 </executions>
43         </plugin>
44         </plugins>
45     </build>
46 </project>

 

     2.编写controller

 

 1 package org.bianqi.first.demo;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 6 import org.springframework.context.annotation.ComponentScan;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RestController;
 9 
10 @RestController
11 @EnableAutoConfiguration
12 @ComponentScan
13 public class FirstDemo {
14     
15     @Autowired
16     private FirstService fs;
17     
18     @RequestMapping("/")
19     String home(){
20         fs.demo();
21         return "hello world!";
22     }
23     public static void main(String[] args) {
24         SpringApplication.run(FirstDemo.class, args);
25     }
26     
27 }

3.编写service的接口

1 package org.bianqi.first.demo;
2 
3 public interface FirstService {
4   public String demo();
5 }

4.编写service层实现类

 1 package org.bianqi.first.demo;
 2 
 3 import org.springframework.stereotype.Service;
 4 
 5 @Service
 6 public class FirstServiceImpl implements FirstService{
 7 
 8     public String demo(){
 9         System.out.println("hhhhh");
10         return "helloworld我爱你";
11     }
12 }

在Controller中通过main方法启动~浏览器访问http://localhost:8080/ 显示helloworld 并且控制台打印hhhhh

SpringBoot学习helloworld