1. 程式人生 > >springboot入門(二)

springboot入門(二)

第二種啟動方式:

package wyh.controller;

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


@RestController
public class SecondController {

	
	/*
	 * 使用@EnableAutoConfiguration這個註解只能掃描到當前類
	 */
	@RequestMapping("/index")
	public String index() {
		return "this is the second day to study springboot.";
	}
}
package wyh.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**  
* <p>Title: MyController</p>  
* <p>Description:springboot第一個案例 </p>  
* @author wyh
* @date Nov 25, 2018  
*/ 
/*
 * springboot的啟動原理:使用springmvc註解方式啟動,因為它內建了http伺服器(預設是Tomcat),代替了我們傳統的使用web.xml的方式啟動。
 * 這個我們可以在pom檔案中點進去<artifactId>spring-boot-starter-web</artifactId>,一直點,最後我們就可以看到有關於Tomcat的配置資訊。
 */
/*
* @RestController註解表示該類中的所有方法返回json格式(也就是相當於在類上使用@Controller註解,
  然後在方法上使用@ResponseBody是一樣的,結果都是返回json格式),@RestController是springmvc4為微服務返回json格式提供的
*/
/*
 * @EnableAutoConfiguration該註解就是自動配置。
 * 作用就是讓springboot根據應用所宣告的依賴來對spring框架進行自動配置。
 * 這個註解告訴springboot根據新增的jar依賴猜測你想如何配置spring。由於spring-boot-starter-web添加了Tomcat和springmvc,
 * 所以auto-configuration將假定你正在開發一個web應用。
 *該註解藉助@Import的幫助,將所有符合自動配置條件的bean定義載入到IoC容器。
 *@EnableAutoConfiguration的掃包範圍:當前類。
 *要想使其他類也被掃到,就需要使用@ComponentScan
 */
//@ComponentScan("wyh.controller")
@RestController
@EnableAutoConfiguration
public class MyController {

	@RequestMapping("/myIndex")
	public String Index() {
		return "this is my first day to study springboot!";
	}
	
	public static void main(String[] args) {
		//是整個專案的入口,就是啟動springboot專案,建立內建Tomcat伺服器,使用Tomcat載入springmvc註解啟動類
		SpringApplication.run(MyController.class, args);//第一個引數是當前類的class
	}
}

此時,當我們去執行MyController的main方法時,可以正常訪問到/myIndex的路徑,但是訪問不到SecondController中的/index路徑,因為在Mycontroller中使用的@EnableAutoConfiguration只能掃描當前類(MyController),要想定義擴大掃描範圍,我們需要使用@ComponentScan("wyh.controller"),這樣就可以掃描到整個包。此時再去訪問時,這兩個Controller中的訪問路徑都可以成功訪問到。

但是,為了程式碼規範,我們一般將他們提取為單獨的一個類:此處特別要注意的是,在使用了@ComponentScan("wyh.controller")後,仍需要新增上@EnableAutoConfiguration,否則啟動不成功。

package wyh.controller;

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


@ComponentScan("wyh.controller")
@EnableAutoConfiguration
@RestController
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

此時,其他類中就不需要使用@ComponentScan("wyh.controller")和@EnableAutoConfiguration了。

package wyh.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {

	@RequestMapping("/myIndex")
	public String Index() {
		return "this is my first day to study springboot!";
	}
	
}
package wyh.controller;

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


@RestController
public class SecondController {

	@RequestMapping("/index")
	public String index() {
		return "this is the second day to study springboot.";
	}
}

第三種啟動方式:使用@SpringBootApplication啟動,它的底層相當於@ComponentScan("wyh.controller")和@EnableAutoConfiguration,它掃描的是當前類所在的包及其子包。