ComponentScan

这个注解可以扫描带@Component的类。众所皆知,@RestController和@Configuration和@Service和@Configuration等都有带Component这个注解。所以如果要注入controller和service等,我们可以直接在类上面注解下,并且开启ComponentScan,这样会自动装载并注入这个实例,我们使用的时候可以直接@Autowired使用,下面以controller为栗子:

a:首先在spring boot启动入口开启自动扫描Component的注解(在类上面使用@ComponentScan),这个注解可以配置扫描的路径,我这里配置com.xx,也就是我把controller都放这里,系统会自动注入

由于@ComponentScan其实在SpringBootApplication这里已经有了,我们可以直接用scanBasePackages扫描的路径。即不用ComponentScan这个注解了,取而代之的是用SpringBootApplication
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@SpringBootApplication(scanBasePackages = "com.xx")
@ServletComponentScan(basePackages="com.example.demo")public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} }

b:controller层编写

package com.xx;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @author zhujianming
* @date 2021-05-10 10:24
*/
@RestController
public class PPController {
@RequestMapping("/testp")
public String hello(){
return"Hello world!";
} }

这样直接访问localhost:8080/testp就会返回json了

ServletComponentScan

上面一步在spring boot中启动类加了@ServletComponentScan(basePackages="com.example.demo"),所以扫描路径是com.example.demo,我们下一步在com.example.demo编写个Servlet作为案例

编写@WebServlet

package com.example.demo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; @WebServlet(name="TestServlet",urlPatterns="/test")
public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("doGet");
}
}

这样直接访问/test就会在控制台输出doGet,当然这个注解也会扫描Filter等控件,有空可以去试试

spring boot的ComponentScan和ServletComponentScan注解的更多相关文章

  1. spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,guava限流,定时任务案例, 发邮件

    本文介绍spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例 集成swagger--对于做前后端分离的项目,后端只需要提供接口访问,swagger提供了接口 ...

  2. Spring Boot 2.0 教程 | @ModelAttribute 注解

    欢迎关注微信公众号: 小哈学Java 文章首发于个人网站: https://www.exception.site/springboot/spring-boot-model-attribute Spri ...

  3. Spring Boot(四)@EnableXXX注解分析

    在学习使用springboot过程中,我们经常碰到以@Enable开头的注解,其实早在Spring3中就已经出现了类似注解,比如@EnableTransactionManagement.@ Enabl ...

  4. Spring Boot集成JPA的Column注解命名字段无效的问题

    偶然发现,Spring Boot集成jpa编写实体类的时候,默认使用的命名策略是下划线分隔的字段命名. Spring Boot版本:1.5.4.release 数据表: id int, userNam ...

  5. Spring Boot系列——AOP配自定义注解的最佳实践

    AOP(Aspect Oriented Programming),即面向切面编程,是Spring框架的大杀器之一. 首先,我声明下,我不是来系统介绍什么是AOP,更不是照本宣科讲解什么是连接点.切面. ...

  6. 必须知道的Spring Boot中的一些Controller注解

    这篇文章是抄其他人的,原址:https://cloud.tencent.com/developer/article/1082720 本文旨在向你介绍在Spring Boot中controller中最基 ...

  7. 精尽Spring Boot源码分析 - @ConfigurationProperties 注解的实现

    该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...

  8. spring boot 使用拦截器,注解 实现 权限过滤

    http://www.cnblogs.com/zhangXingSheng/p/7744997.html spring boot 使用拦截器 实现 用户登录拦截 http://www.cnblogs. ...

  9. Spring Boot Validation,既有注解不满足,我是怎么暴力扩展validation注解的

    前言 昨天,我开发的代码,又收获了一个bug,说是界面上列表查询时,正常情况下,可以根据某个关键字keyword模糊查询,后台会去数据库 %keyword%查询(非互联网项目,没有使用es,只能这样了 ...

随机推荐

  1. Python的web开发

    一.Web开发 Tcp   udp       Cs即客户端.服务器端编程,客户端和服务器端之间需要使用socket,约定协议.版本(协议使用的是tcp或者udp).Tcp协议和udp协议,指定地址和 ...

  2. python常见错误和异常

    1.BaseExeception 所有异常的基类 2.SystemEixt 解释器请求退出 3.KeyboardInterrupt 用户中断执行 4.Exception 常规错误的基类 5.StopI ...

  3. 050_Servlet详解

    目录 Servlet Servlet简介 HelloServlet Servlet原理 servlet-mapping Servlet请求路径 ServletContext Servlet上下文 Se ...

  4. kmp&字典树 模板

    kmp: const int maxn=1e5+5; char s[maxn],p[maxn]; int nex[maxn]; int KmpSearch(char* s, char* p) { in ...

  5. 【JVM进阶之路】一:Java虚拟机概览

    1.Java简史 Java语言是一门通用的.面向对象的.支持并发的程序语言.全球从事Java相关开发的人员已经数以百万计. 从1995年"Java"正式出现以来,Java已经经历了 ...

  6. LevelDB 源码解析之 Varint 编码

    GitHub: https://github.com/storagezhang Emai: debugzhang@163.com 华为云社区: https://bbs.huaweicloud.com/ ...

  7. Kafka 常见问题汇总

    Kafka 常见问题汇总 1. Kafka 如何做到高吞吐.低延迟的呢? 这里提下 Kafka 写数据的大致方式:先写操作系统的页缓存(Page Cache),然后由操作系统自行决定何时刷到磁盘. 因 ...

  8. Distributed | Google File System

    本文为我读GFS论文后做的总结,包括文件系统的读写流程,和一些重要机制. [Google File System] 设计理念 组件失效被认为是常态事件.GFS包括数百上千台普通设备,在任何时间都有可能 ...

  9. BUAA_2020_OO_UNIT3_REVIEW

    OO第三单元总结 1. JML语言的理论基础.应用工具链情况 1.1 JML理论基础 我觉得就是<离散数学>中的数理逻辑 由于我的<离散数学>是速成的,导致我不会写规格,只能勉 ...

  10. 研发效率破局之道 Facebook工作法

    如果你问中国和美国互联网公司都有什么差别,很多人会回答:低效加班文化.最近爆出的996大讨论,通过糙快猛打拼和996加班去抢占市场获得机会的成功案例越来越少.至此,只有提高效能才是出路. 由于软件开发 ...