一、SpringBoot中使用Servlet
在SpringBootApplication上使用@ServletComponentScan注解后,Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码。

1.在入口Application类上加入注解@ServletComponentScan

package com.hui;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication
@ServletComponentScan
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

2.新建Servlet类,继承HttpServlet并且加入注解@WebServlet(name=“TestServlet”,urlPatterns="/test")

package com.hui.qiang;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @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");
}
}

3.之后运行代码,在浏览器地址栏输入http://localhost:8080/test,若看到控制台打印doGet,则表示Servlet创建成功。
注意:
(1)如果不加@ServletComponentScan注解则会报404即找不到页面,控制台也扫描不到我们配置的servlet:/test,即无法被映射
(2)如果Application类和Servlet类不在同一包下,则@ServletComponentScan需要添加相应的路径,如Application类在包com.hui.xiao下,则写为@ServletComponentScan(“com.hui.xiao”)或@ServletComponentScan(“com.hui”)

二、Spring, Spring Boot中的@ComponentScan注解用法介绍
@ComponentScan
如果你理解了ComponentScan,你就理解了Spring.
Spring是一个依赖注入(dependency injection)框架。所有的内容都是关于bean的定义及其依赖关系。
定义Spring Beans的第一步是使用正确的注解-@Component或@Service或@Repository.
但是,Spring不知道你定义了某个bean除非它知道从哪里可以找到这个bean.
ComponentScan做的事情就是告诉Spring从哪里找到bean

由你来定义哪些包需要被扫描。一旦你指定了,Spring将会将在被指定的包及其下级的包(sub packages)中寻找bean
下面分别介绍在Spring Boot项目和非Spring Boot项目(如简单的JSP/Servlet或者Spring MVC应用)中如何定义ComponentScan

注:@ComponentScan的不同写法
1.@ComponentScan({“com.xiao.hui”,“com.xiao.qiang”})或@ComponentScan(basePackages = {“com.xiao.hui”,“com.xiao.qiang”})
2.@ComponentScan(“com.xiao”)或@ComponentScan(value = “com.xiao”)或@ComponentScan(basePackages = { “com.xiao” })
3.@ComponentScan(basePackageClasses=要扫描类.class所在位置的包) 意思是要扫描哪个类所在的包,如@ComponentScan(basePackageClasses=hehe.class),这种写法不如上面的那种写法好有局限性

Spring Boot项目
总结:
1.SpringBoot在写启动类的时候如果不使用@ComponentScan指明对象扫描范围,默认指扫描当前启动类所在的包里的对象,如果你的其他包都在使用了@SpringBootApplication注解的主类所在的包及其下级包,则你什么都不用做,SpringBoot会自动帮你把其他包都扫描了。为了方便,我一般都把主类放在了所有类的上一级包中,如项目所有的class文件都放在了包com.beauty的下级包中,则把spring boot的主类放在包com.beauty下。
2.如果当前启动类没有包,则在启动时会报错:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package错误,因为启动类不能直接放在main/java文件夹下,必须要建一个包把它放进去或者使用@ComponentScan指明要扫描的包。
3.如果你有一些bean所在的包,不在主类的包及其下级包,那么你需要手动加上@ComponentScan注解并指定那个bean所在的包。

举个栗子,看下面定义的类:

package com.xiao.qiang.qianming;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication
public class SpringbootApplication { public static void main(String[] args) {
ApplicationContext applicationContext =
SpringApplication.run(SpringbootApplication.class, args); for (String name : applicationContext.getBeanDefinitionNames()) {
System.out.println(name);
}
}
}

类SpringbootApplication在com.xiao.qiang.qianming包下,这个类使用了@SpringBootApplication注解,该注解定义了Spring将自动扫描包com.xiao.qiang.qianming及其子包下的bean
如果你项目中所有的类都定义在上面的包及其子包下,那你不需要做任何事。
但假如你一个类定义在包com.xiao.hui下,则你需要将这个新包也纳入扫描的范围,有两个方案可以达到这个目的。

方案1
定义@ComponentScan(“com.xiao”),这么做扫描的范围扩大到整个父包com.xiao

@ComponentScan("com.xiao")
@SpringBootApplication
public class SpringbootIn10StepsApplication {

方案2
定义分别扫描两个包

@ComponentScan({"com.xiao.hui","com.xiao.qiang"})
@SpringBootApplication
public class SpringbootIn10StepsApplication {

非Spring Boot项目
在非Spring Boot项目中,我们必须显式地使用@ComponentScan注解定义被扫描的包,可以通过XML文件在应用上下文中定义或在Java代码中对应用上下文定义

Java代码方式:

@ComponentScan({"com.xiao.package1","com.xiao.package2"})
@Configuration
public class SpringConfiguration {

注:@Configuration 和@ComponentScan注解背后会做什么呢?
其实很简单,@ComponentScan告诉Spring 哪个packages 的用注解标识的类 会被spring自动扫描并且装入bean容器。
例如,如果你有个类用@Controller注解标识了,那么,如果不加上@ComponentScan,自动扫描该controller,那么该Controller就不会被spring扫描到,更不会装入spring容器中,因此你配置的这个Controller也没有意义。
类上的注解@Configuration 是最新的用注解配置spring,也就是说这是个配置文件,和原来xml配置是等效的,只不过现在用java代码进行配置了 加上一个@Configuration注解就行了,是不是很方便,不需要那么繁琐的xml配置了,这样基于注解的配置,可读性也大大增高了。

XML文件方式:
<context:component-scan base-package=“com.xiao.package1, com.xiao.package2” />

三、使用@ComponentScan自动扫描组件实例
包扫描会扫描只要标注了@Controller,@Service,@Repository,@Component这四个注解都会被扫描到容器中。
1、@Controller 控制器(注入服务)
用于标注控制层,相当于struts中的action层
2、@Service 服务(注入dao)
用于标注服务层,主要用来进行业务的逻辑处理
3、@Repository(实现dao访问)
用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件.
4、@Component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)
泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。
案例:<context:component-scan base-package=”com.*”>
上面的这个例子是引入Component组件的例子,其中base-package表示为需要扫描的所有子包。
有一篇不错的文章(Spring注解详解):https://blog.csdn.net/xyh820/article/details/7303330/

新增控制层的java类:TestController和HelloController

import org.springframework.stereotype.Controller;

@Controller
public class TestController { }
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloController {
@RequestMapping(value="/hello", method = RequestMethod.GET)
public String hello() {
return "Hello, Spring Boot";
}
}

新建一个业务逻辑层类:TestService

import org.springframework.stereotype.Service;

@Service
public class TestService { }

新建一个Person:

public class Person {
public Person(String string, int i) {
}
}

主方法测试:
spring boot:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan; @SpringBootApplication
@ComponentScan(value = "com.hui")
public class Application { public static void main(String[] args) {
ApplicationContext applicationContext =
SpringApplication.run(Application.class, args); for (String name : applicationContext.getBeanDefinitionNames()) {
System.out.println(name);
}
}
}

非spring boot:

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import com.hui.entity.Person; @Configuration
@ComponentScan(value = "com.hui")
public class ComponentTest { @Bean
public Person getPerson() {
return new Person("百度好帅", 10000);
} public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext applicationContext =
new AnnotationConfigApplicationContext(ComponentTest.class);
String[] beanNames = applicationContext.getBeanDefinitionNames();
for (String bean : beanNames) {
System.out.println(bean);
}
}
}

运行Application:
控制台扫描到了/hello,即映射成功

把Application注释掉运行ComponentTest:

参考:
https://blog.csdn.net/Lamb_IT/article/details/80918704
https://jingyan.baidu.com/article/7908e85cc6930daf481ad2b6.html

————————————————
版权声明:本文为CSDN博主「小强签名设计」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_37739193/article/details/85097477

Springboot注解@ServletComponentScan和@ComponentScan(转)的更多相关文章

  1. springboot注解使用说明

    springboot注解 @RestController和@RequestMapping注解 我们的Example类上使用的第一个注解是 @RestController .这被称为一个构造型(ster ...

  2. SpringBoot注解大全 转

    2019年3月17日22:30:10 一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@Enab ...

  3. springBoot系列-->springBoot注解大全

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

  4. SpringBoot注解大全(转)

    原文链接:[springBoot系列]--springBoot注解大全 一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Co ...

  5. springboot注解大全

    springboot注解:@Service: 注解在类上,表示这是一个业务层bean@Controller:注解在类上,表示这是一个控制层bean@Repository: 注解在类上,表示这是一个数据 ...

  6. SpringBoot注解把配置文件自动映射到属性和实体类实战

    SpringBoot注解把配置文件自动映射到属性和实体类实战 简介:讲解使用@value注解配置文件自动映射到属性和实体类 1.配置文件加载 方式一 1.Controller上面配置 @Propert ...

  7. 转-spring-boot 注解配置mybatis+druid(新手上路)-http://blog.csdn.net/sinat_36203615/article/details/53759935

    spring-boot 注解配置mybatis+druid(新手上路) 转载 2016年12月20日 10:17:17 标签: sprinb-boot / mybatis / druid 10475 ...

  8. SpringBoot注解大全*(转发:http://www.cnblogs.com/ldy-blogs/p/8550406.html)

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

  9. 【转载】springboot注解

    https://blog.csdn.net/yitian_66/article/details/80866571 springboot注解:@Service: 注解在类上,表示这是一个业务层bean@ ...

随机推荐

  1. [转帖]微软宣布即将开始大规模推送Windows 10 V1903重大版本更新

    微软宣布即将开始大规模推送Windows 10 V1903重大版本更新 https://www.cnbeta.com/articles/tech/894303.htm 微软要批量更新 1903了 bu ...

  2. 【转帖】循序渐进Oracle:数据库的字符集和字符集文件

    循序渐进Oracle:数据库的字符集和字符集文件 https://blog.csdn.net/Enmotech/article/details/100869162 2019年09月15日 18:23: ...

  3. Sql 第一行某列减第二行某列

    --1. 将结果插入临时表SELECT *INTO xxx FROM( SELECT TOP 1 a.FQTY,a.fseq FROM T_SAL_ORDERENTRY as a WHERE FQTY ...

  4. 对快速排序的理解以及相关c++代码

    快速排序:在一组数据中,可以将左边的数字当作枢轴(右边也可以),接下来要做的就是,先从右边找到比枢轴小的数, 再从左边找到比枢轴大的数,接着将这两个数进行交换,重复上述步骤找出所有符合条件的数进行交换 ...

  5. python学习--12 基本数据类型

    数字 int -int  功能 1.转换 例如: a = '123' # 字符串print(type(a),a)b = int(a) # 将字符串转换成intprint(type(b),b) 运算结果 ...

  6. 深度学习-mnist手写体识别

    mnist手写体识别 Mnist数据集可以从官网下载,网址: http://yann.lecun.com/exdb/mnist/ 下载下来的数据集被分成两部分:55000行的训练数据集(mnist.t ...

  7. Python 同级目录import报错

    在使用protobuf时,我们自己了各种Options的时候,在生成的python文件会在同级目录中引用,所以这个时候我们不能做到,加个__init__.py文件了事然后在文件里面写 import m ...

  8. 安装mysql时出现 mysql Install/Remove of the Service Denied! 错误的解决办法

    用cmd在mysql的bin目录下面执行: mysqld --install 命令,出现错误: mysql Install/Remove of the Service Denied! 解决方法:以管理 ...

  9. AngularJS入门教程之数据绑定原理详解

    这篇文章主要是写给新手的,是给那些刚刚开始接触Angular,并且想了解数据帮定是如何工作的人.如果你已经对Angular比较了解了,那强烈建议你直接去阅读源代码. Angular用户都想知道数据绑定 ...

  10. 还在用ABAP进行SAP产品的二次开发?来了解下这种全新的二次开发理念吧

    Jerry从2018年底至今,已经写了一系列关于SAP Kyma的文章,您可以移步到本文末尾获得这些文章的列表.Kyma是SAP开源的一个基于Kubernetes的云原生应用开发平台,能够允许SAP的 ...