1. 程式人生 > >1-helloWorld

1-helloWorld

control 新建 gpo bean [] pos 幫我 beans ping

新建一個maven工程,並新建各層目錄

技術分享圖片

編寫pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.haerwang</groupId>
	<artifactId>spring.boot</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<!--必須引用父模塊,指明這是個springboot工程,幫我們實現很多jar包的依賴,不需要自己依賴jar包 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.6.RELEASE</version>
	</parent>

	<!--springboot默認集成mvc,遇到依賴以下web即可,相當於搭建好了web環境-->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

</project>

  

新建一個controller

package com.haerwang.springboot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author haerwang
 * @RestController ----相當於加了 @Controller 和 @ResponseBody(返回josn)
 */
@RestController
@RequestMapping("/hello")
public class HelloWorldController {
@RequestMapping("/sayHello") public String sayHello() { return "hello"; } }

  

新建一個主程序main入口(啟動用的,這也是網上說,springboot是一個獨立的應用程序)

package com.haerwang.springboot.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;


/**
 * @author haerwang
 * @ComponentScan 指明需要掃描的路徑,掃描到的含有標簽類會自動裝載成bean,比如contoller層的@RestController
 * @EnableAutoConfiguration 程序啟動標簽,主程序入口,開始在這裏加載各種配置
 */
@ComponentScan(basePackages = ("com.haerwang.springboot"))
@EnableAutoConfiguration
public class APP {
	public static void main(String[] args) {
		SpringApplication.run(APP.class, args);
	}
}

  

到此,可以直接右鍵main啟動,在瀏覽器輸入網址:http://localhost:8080/hello/sayHello即可

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

接下來新增一層,service,將邏輯延伸到新的一層

寫一個service及實現類(習慣問題,可以直接寫一個類)

技術分享圖片

package com.haerwang.springboot.service;

public interface HelloWorldService {
	String sayHello();
}

  

package com.haerwang.springboot.service.impl;


import org.springframework.stereotype.Service;

import com.haerwang.springboot.service.HelloWorldService;

@Service
public class HelloWorldServiceImpl implements HelloWorldService{

	@Override
	public String sayHello() {
		
		return "hello";
	}

}

  註意這個@Service

修改controller

package com.haerwang.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.haerwang.springboot.service.HelloWorldService;

/**
 * @author haerwang
 * @RestController ----相當於加了 @Controller 和 @ResponseBody(返回josn)
 */
@RestController
@RequestMapping("/hello")
public class HelloWorldController {

	@Autowired
	HelloWorldService helloWorldService;

	@RequestMapping("/sayHello")
	public String sayHello() {
		return helloWorldService.sayHello();
	}
}

  

老方法啟動,效果依舊

1-helloWorld