spring boot的ComponentScan和ServletComponentScan注解
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注解的更多相关文章
- spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,guava限流,定时任务案例, 发邮件
本文介绍spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例 集成swagger--对于做前后端分离的项目,后端只需要提供接口访问,swagger提供了接口 ...
- Spring Boot 2.0 教程 | @ModelAttribute 注解
欢迎关注微信公众号: 小哈学Java 文章首发于个人网站: https://www.exception.site/springboot/spring-boot-model-attribute Spri ...
- Spring Boot(四)@EnableXXX注解分析
在学习使用springboot过程中,我们经常碰到以@Enable开头的注解,其实早在Spring3中就已经出现了类似注解,比如@EnableTransactionManagement.@ Enabl ...
- Spring Boot集成JPA的Column注解命名字段无效的问题
偶然发现,Spring Boot集成jpa编写实体类的时候,默认使用的命名策略是下划线分隔的字段命名. Spring Boot版本:1.5.4.release 数据表: id int, userNam ...
- Spring Boot系列——AOP配自定义注解的最佳实践
AOP(Aspect Oriented Programming),即面向切面编程,是Spring框架的大杀器之一. 首先,我声明下,我不是来系统介绍什么是AOP,更不是照本宣科讲解什么是连接点.切面. ...
- 必须知道的Spring Boot中的一些Controller注解
这篇文章是抄其他人的,原址:https://cloud.tencent.com/developer/article/1082720 本文旨在向你介绍在Spring Boot中controller中最基 ...
- 精尽Spring Boot源码分析 - @ConfigurationProperties 注解的实现
该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...
- spring boot 使用拦截器,注解 实现 权限过滤
http://www.cnblogs.com/zhangXingSheng/p/7744997.html spring boot 使用拦截器 实现 用户登录拦截 http://www.cnblogs. ...
- Spring Boot Validation,既有注解不满足,我是怎么暴力扩展validation注解的
前言 昨天,我开发的代码,又收获了一个bug,说是界面上列表查询时,正常情况下,可以根据某个关键字keyword模糊查询,后台会去数据库 %keyword%查询(非互联网项目,没有使用es,只能这样了 ...
随机推荐
- Java 树结构实际应用 一(堆排序2秒排完800w数据)
堆排序 1 堆排序基本介绍 1) 堆排序是利用堆这种数据结构而设计的一种排序算法,堆排序是一种选择排序,它的最坏,最好,平均时间复 杂度均为 O(nlogn),它也是不稳定排序. 2) 堆是具有以下性 ...
- C# 通过ServiceStack 操作Redis——ZSet类型的使用及示例
Sorted Sets是将 Set 中的元素增加了一个权重参数 score,使得集合中的元素能够按 score 进行有序排列 /// <summary> /// Sorted Sets是将 ...
- “/”应用程序中的服务器错误。||分析器错误消息: 未能加载类型“WebApplication1._Default”
环境VS2008 无法运行WEB项目,Winfrom程序OK. 新创建的WEB项目直接运行报下图错误. 尝试多种方法: 1,重新生成项目,运行.(失败) 2,重装VS2008(默认.完全.自定义)安装 ...
- HDU-6862 Hexagon (2020HDU 多校 D8 H)
1008 题意:半径为n的六边形(由半径为1的小六边形组成),从某一个小六边形出发有六个方向,找到一条转向次数最多的路径(用方向表示)遍历所有的六边形(一个六边形只访问一次). 题解:先画出n=3/4 ...
- C语言数组寻址
C语言数组 数组的定义 数组是用来存放数据类型相同且逻辑意义相同的数据 数组的大小 数组的大小不能是变量,必须是常量或者常量表达式,常量表达式由编译器编译时自动求值. 也可以不指定数组大小,但必须对数 ...
- [差分][二分][贪心]luogu P3634 [APIO2012]守卫
题面 https://www.luogu.com.cn/problem/P3634 给m个限制,可以是一段区间中必须有或者必须无忍者 最多有k个忍者,问有多少个位点一定有忍者 分析 首先用差分标记一下 ...
- 了解 Vue 的 Compsition API
在这篇文章中,我将讲讲 Vue 的 Composition API 为什么比之前的 Options API 要好,以及它是如何工作的. Options API 有什么问题 首先,这里不是要大家放弃 O ...
- python工业互联网应用实战11—客户端UI
这个章节我们将演示用户端界面的开发,当前演示界面还是采用先实现基本功能再逐步完善的"敏捷"模式.首先聚焦在功能逻辑方面实现普通用户与系统的交互,普通用户通过url能查看到当前任务的 ...
- .NET Request对象介绍
Request对象用于检索从浏览器向服务器所发送的请求信息.它提供对当前页请求的访问,包括标题,Cookie,客户端证书等等.它也与HTTP协议的请求消息对应 Request常用的属性 属性 具体内容 ...
- 配置动态刷新RefreshScope注解使用局限性(一)
在 Spring Cloud 体系的项目中,配置中心主要用于提供分布式的配置管理,其中有一个重要的注解:@RefreshScope,如果代码中需要动态刷新配置,在需要的类上加上该注解就行.本文分享一下 ...