context:component-scan

Scans the classpath for annotated components that will be auto-registered as Spring beans. By default, the Spring-provided @Component, @Repository, @Service, and @Controller stereotypes will be detected. Note: This tag implies the effects of the ‘annotation-config‘ tag, activating @Required, @Autowired, @PostConstruct, @PreDestroy, @Resource, @PersistenceContext and @PersistenceUnit annotations in the component classes, which is usually desired for autodetected components (without external configuration). Turn off the ‘annotation-config‘ attribute to deactivate this default behavior, for example in order to use custom BeanPostProcessor definitions for handling those annotations. Note: You may use placeholders in package paths, but only resolved against system properties (analogous to resource paths). A component scan results in new bean definition being registered; Spring‘s PropertyPlaceholderConfigurer will apply to those bean definitions just like to regular bean definitions, but it won‘t apply to the component scan settings themselves. See javadoc for org.springframework.context.annotation.ComponentScan for information on code-based alternatives to bootstrapping component-scanning.

  • @Component
  • @Repository
  • @Service
  • @Controller
<xsd:attribute name="base-package" type="xsd:string" use="required">...</xsd:attribute>
<xsd:attribute name="resource-pattern" type="xsd:string">...</xsd:attribute>
<xsd:attribute name="use-default-filters" type="xsd:boolean" default="true">...</xsd:attribute>
<xsd:attribute name="annotation-config" type="xsd:boolean" default="true">...</xsd:attribute>
<xsd:attribute name="name-generator" type="xsd:string">...</xsd:attribute>
<xsd:attribute name="scope-resolver" type="xsd:string">...</xsd:attribute>
<xsd:attribute name="scoped-proxy">...</xsd:attribute>
<xsd:element name="include-filter" type="filterType" minOccurs="0" maxOccurs="unbounded">...</xsd:element>
<xsd:element name="exclude-filter" type="filterType" minOccurs="0" maxOccurs="unbounded">...</xsd:element>

作用:

自动寻找被特定注解类标识的bean注册到 spring容器

@Component 是最基本的组件 ,和标签的名字一样 <context:component-scan

有一个属性是annotation-config 默认值是true ,默认是开启自动注入的。功能同<context:annotation-config /> 无需重复填写。

back-package 是 扫描的这个文件夹下的

use-default-filters 是扫描文件夹下的全部

filterType 有 5个值,点击看明细

时间: 2024-11-06 14:10:10

context:component-scan的相关文章

Spring MVC注解配置结合Hibernate的入门教程及其代码实例

原文:Spring MVC注解配置结合Hibernate的入门教程及其代码实例 源代码下载地址:http://www.zuidaima.com/share/1787210045197312.htm 1.概述 本文旨在搭建Spring MVC+Hibernate开发框架,通过一个简单的demo讲解Spring MVC的相关配置文件,以及通过注解方式实现简单功能. 开发框架:Spring+Spring MVC+Hibernate(Spring所用的版本为3.0.5). 数据库:MySQL(数据库名称

spring mvc 编写处理带参数的Controller

在上一随笔记录的基础上,现记录编写处理带有参数的Controller. @Controller //这个注解会告知<context:component:scan> 将HomeController自动检测为一个Bean@RequestMapping("/home")  //这是根Urlpublic class HomeController {        private UserService userService;        @Autowired    public

如何用Spring将Service注入到Servlet中

解决方法有两种(推荐使用第二种) 方法一: 直接重写Servlet的Init()方法,代码如下: public void init(ServletConfig servletConfig) throws ServletException { ServletContext servletContext = servletConfig.getServletContext(); WebApplicationContext webApplicationContext = WebApplicationCo

注解Annotation的IoC:从@Autowired到@Component

注解Annotation的IoC:从@Autowired到@Component 2017-01-20 目录 1 什么是注解2 不使用注解示例  2.1 com.springioc.animal.Monkey  2.2 com.springioc.animal.Tiger  2.3 com.springioc.bean.Zoo  2.4 com.springioc.main.AppMain  2.5 Beans.xml3 使用注解@Autowired示例  3.1对成员变量使用@Autowired

applicationContext.xml和dispatcher-servlet.xml的区别

在SpringMVC项目中我们一般会引入applicationContext.xml和dispatcher-servlet.xml两个配置文件,这两个配置文件具体的区别是什么呢? Spring 官方文档介绍如下: Spring lets you define multiple contexts in a parent-child hierarchy. The applicationContext.xml defines the beans for the "root webapp context

SpingMVC 核心技术帮助文档

SpringMVC 框架使用技巧 声明:本篇文档主要是用于参考帮助文档,没有实例,但几乎包含了SpringMVC 4.2版本的所有核心技术 对于觉得篇幅长的文档,建议大家使用快捷键crtl + F,搜索关键字查询较为方便. 欢迎加群JAVA编程交流群 574337670 21.1 Spring Web MVC框架简介 Spring的模型-视图-控制器(MVC)框架是围绕一个 DispatcherServlet 来设计的,这个Servlet会把请求分发给各个处理器,并支持可配置的处理器映射.视图渲

Spring Batch示例: 读取CSV文件并写入MySQL数据库

Spring Batch示例: 读取CSV文件并写入MySQL数据库 GitHub版本: https://github.com/kimmking/SpringBatchReferenceCN/blob/master/01_introduction/Spring_Batch_MySQL.md 原文链接: Reading and writing CVS files with Spring Batch and MySQL 原文作者: Steven Haines - 技术架构师 下载本教程的源代码: S

javax inect

Spring 3 and JSR-330 @Inject and @Named example By mkyong | September 16, 2012 | Viewed : 86,399 times Since Spring 3.0, Spring supports for the standard JSR 330: Dependency Injection for Java. In Spring 3 application, you can uses standard @Inject i

DEPENDENCY INJECTION EXAMPLE USING SPRING

Dependency Injection The Spring Framework is literally built around the concept of Dependency Injection. In this post, we’ll take a look at a simple example of Dependency Injection using the Spring Framework. If you want a deeper dive on Dependency I