2.4使用@componentscan方式启动

2.4.1 @EnableAutoConfiguration 默认只扫描当前类

@EnableAutoConfiguration 默认只扫描当前类,如果再新建一个indexcontroller类,将无法被扫描。

新建indexcontroller类:

/**

* 测试index类

* @author admin

*

*/

@RestController

public class IndexController {

@RequestMapping("/index")

public String index() {

return "index info";

}

}

启动TestController类中的main函数,首先访问http://localhost:8080/test

没有问题,再访问http://localhost:8080/index

报错信息如下:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Apr 14 22:25:20 CST 2019

There was an unexpected error (type=Not Found, status=404).

No message available

2.4.2 @ComponentScan指定扫描范围(7)

为了方便维护和规范化,将启动类和controller类分离开,新建一个IndexController类。启动类修改如下:

//@RestController
//@SpringBootApplication //或使用@EnableAutoConfiguration配置
@EnableAutoConfiguration //自动配置,根据pom文件引入的依赖信息,自动配置对应的组件;扫包范围,默认在当前类中
@ComponentScan("com.springboot2demo") //指定扫描范围
public class FirstApplication { /**
* 程序入口
* SpringApplication.run 相当于java代码创建内置tomcat,加载springmvc注解启动
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(FirstApplication.class, args);
} }

IndexController为:

/**
* 仅 @EnableAutoConfiguration注解方式下启动(默认只扫描当前类),/index 访问报错
* @author admin
*/
@RestController
public class IndexController { @RequestMapping("/index")
public String index() {
return "index info";
} @RequestMapping("/test")
public String test(){
return "springboot2.0 first application";
} }

启动springboot启动类

访问http://localhost:8080/index 成功返回:index info

访问http://localhost:8080/test成功返回:springboot2.0 first application

2.4.3 @SpringBootApplication方式启动(8)

1多个controller包的@componentscan多包扫描

FirstController类:

@RestController
public class FirstController { @RequestMapping("/firstIndex")
public String index() {
return "firstIndex info";
} }

SecondController类:

@RestController
public class SecondController { @RequestMapping("/secondIndex")
public String index() {
return "secondIndex info";
} }

启动类修改为:

//@RestController
//@SpringBootApplication //或使用@EnableAutoConfiguration配置
@EnableAutoConfiguration //自动配置,根据pom文件引入的依赖信息,自动配置对应的组件;扫包范围,默认在当前类中
//@ComponentScan("com.springboot2demo") //指定扫描范围
@ComponentScan(basePackages= {"com.springboot2demo.first","com.springboot2demo.second"}) //多包扫描
public class FirstApplication {

2 @SpringbootApplication注解启动

等同于@EnableAutoConfiguation+@ComponentScan

且扫描范围默认为类所在当前包下所有包(包括子包)

在原有代码基础上修改启动类

//@RestController
//@EnableAutoConfiguration //自动配置,根据pom文件引入的依赖信息,自动配置对应的组件;扫包范围,默认在当前类中
//@ComponentScan("com.springboot2demo") //指定扫描范围
//@ComponentScan(basePackages= {"com.springboot2demo.first","com.springboot2demo.second"}) //多包扫描
@SpringBootApplication //或使用@EnableAutoConfiguration+@ComponentScan配置
public class FirstApplication {

git代码:https://github.com/cslj2013/-springboot2.0_first_demo.git

springboot学习入门简易版三---springboot2.0启动方式的更多相关文章

  1. springboot学习入门简易版二---springboot2.0项目创建

    2 springboot项目创建(5) 环境要求:jdk1.8+ 项目结构: 2.1创建maven工程 Group id :com.springbootdemo Artifact id: spring ...

  2. springboot学习入门简易版八---springboot2.0多环境配置、整合mybatis mysql8+(19-20)

    2.11 SpringBoot多环境配置(19)  application.properties中配置 Spring.profiles.active=prd 配置环境: Application-dev ...

  3. springboot学习入门简易版六---springboot2.0整合全局捕获异常及log4j日志(12-13)

    使用Aop实现 1创建异常请求 在原有项目基础上,jspController中创建一个可能发生异常的请求: /** * 全局捕获异常测试 * @param i * @return */ @Reques ...

  4. springboot学习入门简易版五---springboot2.0整合jsp(11)

    springboot对jsp支持不友好,内部tomcat对jsp不支持,需要使用外部tomcat,且必须打包为war包. 1 创建maven项目 注意:必须为war类型,否则找不到页面. 且不要把js ...

  5. springboot学习入门简易版九---springboot2.0整合多数据源mybatis mysql8+(22)

    一个项目中配置多个数据源(链接不同库jdbc),无限大,具体多少根据内存大小 项目中多数据源如何划分:分包名(业务)或注解方式.分包名方式类似多个不同的jar,同业务需求放一个包中. 分包方式配置多数 ...

  6. springboot学习入门简易版四---springboot2.0静态资源访问及整合freemarker视图层

    2.4.4 SpringBoot静态资源访问(9) Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则 /static  /public  /resourc ...

  7. springboot学习入门简易版一---springboot2.0介绍

    1.1为什么用springboot(2) 传统项目,整合ssm或ssh,配置文件,jar冲突,整合麻烦.Tomcat容器加载web.xml配置内容 springboot完全采用注解化(使用注解方式启动 ...

  8. springboot学习入门简易版七---springboot2.0使用@Async异步执行方法(17)

    1启动类开启异步调用注解 @SpringBootApplication @EnableAsync //开启异步调用 public class StartApplication { 不开启则异步调用无效 ...

  9. SpringBoot学习入门之Hello项目的构建、单元测试和热部署等(配图文,配置信息详解,附案例源码)

    前言: 本文章主要是个人在学习SpringBoot框架时做的一些准备,参考老师讲解进行完善对SpringBoot构建简单项目的学习汇集成本篇文章,作为自己对SpringBoot框架的总结与笔记. 你将 ...

随机推荐

  1. 【Redis】Redis 主从模式搭建

    主从模式介绍 Redis虽然读取写入的速度都特别快,但是也会产生读压力特别大的情况.为了分担读压力,Redis支持主从复制,Redis的主从结构可以采用一主多从或者级联结构,Redis主从复制可以根据 ...

  2. 【PHP】 解决array_filter会过滤0 false的问题

    定义和用法 array_filter() 函数用回调函数过滤数组中的元素. 该函数把输入数组中的每个键值传给回调函数.如果回调函数返回 true,则把输入数组中的当前键值返回给结果数组.数组键名保持不 ...

  3. 宣化上人:大佛顶首楞严经四种清净明诲浅释(10-11) -------------------------------------------------------------------------------- (转自学佛网:http://www.xuefo.net/nr/article23/230920.html)

    大佛顶首楞严经四种清净明诲浅释(10) 唐天竺·沙门般剌密帝译 宣化上人主讲 一九八三年四月十七日晚讲于万佛圣城 是故阿难.若不断偷修禅定者.譬如有人.水灌漏卮.欲求其满.纵经尘劫.终无平复. 是故阿 ...

  4. Spring Cloud简介 4.1

    什么是Spring Cloud Spring Cloud是在Spring Boot的基础上构建的,用于简化分布式系统构建的工具集.该工具集为微服务架构中所涉及的配置管理.服务发现.智能路由.断路器.微 ...

  5. 石子合并 区间DP模板题

    题目链接:https://vjudge.net/problem/51Nod-1021 题意 N堆石子摆成一条线.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的一堆,并将新的一堆石 ...

  6. angular2 select 联动

    界面操作触发大分类id改变,根据id获取二级分类的数据进行绑定显示. html: <div style="overflow: hidden;float: left;padding-le ...

  7. spring boot如何打印mybatis的执行sql

    方案一 application.properties配置: logging.level.com,后面的路径指的是mybatis对应的方法接口所在的包.并不是mapper.xml所在的包. 1. log ...

  8. 用anaconda保证64位和32位的python共存

    conda info # 查看当前工作平台 set CONDA_FORCE_32BIT=1 # 切换到32位 set CONDA_FORCE_32BIT=0 # 切换到64位 conda create ...

  9. PHP判断上传图片的类型

    PHP判断上传图片的类型用getimagesize来判断上传图片的类型比$_FILES函数的type更可靠 同一个文件,使用不同的浏览器php返回的type类型是不一样的,由浏览器提供type类型的话 ...

  10. 使用qt creator来编译 调试 用CMakeLists组织的工程

                                                   爱情原如树叶一样,在人忽视里绿了,在忍耐里露出蓓蕾. -- 何其芳 使用CMake作为构建系统,需要自己写 ...