码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot学习helloworld

时间:2017-02-17 23:50:01      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:接口   ons   work   class   autowire   apach   type   group   控制台   

  这几天开始学习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

标签:接口   ons   work   class   autowire   apach   type   group   控制台   

原文地址:http://www.cnblogs.com/bianqi/p/6411690.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!