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. ES6深入浅出-9 Promise-3.Promise的细节

    await 拿到用户信息,函数前面加await await等待Promise成功或者失败. 如果Promise里面失败了 什么也拿不到.报了一个错误,叫做不认识. 如果想拿到正常错误,就绪try一下 ...

  2. MyBatis的学习总结:调用存储过程【参考】

    一.创建存储过程 存储过程的目的:统计edi_test_task 正在运行的任务和非运行的任务 CREATE DEFINER=`root`@`%` PROCEDURE `edihelper`.`SP_ ...

  3. Locust-参数化批量注册(还没试,目测试可以的)

    前言 实现场景:所有并发虚拟用户共享同一份测试数据,并且保证虚拟用户使用的数据不重复.例如,模拟10用户并发注册账号,总共有100个手机号,要求注册账号不重复,注册完毕后结束测试 准备数据 虚拟用户 ...

  4. 123457123456#0#-----com.ppGame.huaHua65--前拼后广--儿童填色-pp

    com.ppGame.huaHua65--前拼后广--儿童填色-pp

  5. Embedding Layer

    在深度学习实验中经常会遇Eembedding层,然而网络上的介绍可谓是相当含糊.比如 Keras中文文档中对嵌入层 Embedding的介绍除了一句 “嵌入层将正整数(下标)转换为具有固定大小的向量” ...

  6. JS的正则表达式限定开始和结尾等测试

    []:匹配该区间内人任意一个字符^:匹配以某内容开头的$:匹配以模拟内容结尾的字符\w:测试是英文字母,数字,下划线.{}:设置区间,可出现几次到几次该文学习和测试几个正则的方法,测试结果如图,不加多 ...

  7. log4net使用简明教程,快看看哟

    在项目当中经常会遇到各种各样的问题,如何可以尽快找到问题,那么就只能靠日志了,所以一个系统的日志是否完备合理就尤为重要. 在日志管理插件中log4net相当流行,下面就简单说明一下使用方法. log4 ...

  8. eclipse英语单词1

    short cut bar 捷径,快捷方法 menu bar 菜单栏 tool bar 工具栏 workbench window 工作台窗口 perspective 透视 editor 编辑器 con ...

  9. NLP | 算法 学习资料整理

    UPDATE TIME: 2019-12-12 17:06:32 NLP: 对话系统: [ ] https://www.cnblogs.com/jiangxinyang/p/10789512.html ...

  10. luoguP1823 [COI2007] Patrik 音乐会的等待

    题目描述 N个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟人.队列中任意两个人A和B,如果他们是相邻或他们之间没有人比A或B高,那么他们是可以互相看得见的. ...