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,它扫描的是当前类所在的包及其子包。 

猜你喜欢

转载自blog.csdn.net/QYHuiiQ/article/details/84591023