Spring组件扫描<context:component-scan/>使用详解(转)

1.如果不想在xml文件中配置bean,我们可以给我们的类加上spring组件注解,只需再配置下spring的扫描器就可以实现bean的自动载入。

<!-- 注解注入 -->

<context:annotation-config></context:annotation-config>

<context:component-scan base-package="com.liantuo.hotel.common.service.impl" />

<context:component-scan base-package="com.liantuo.hotel.common.dao.ibatis" />

<context:component-scan base-package="com.liantuo.hotel.app.dao.ibatis" />

<context:component-scan base-package="com.liantuo.hotel.app.service" />

<context:component-scan base-package="com.liantuo.hotel.app.service.ibatis" />

2.下面是引用spring framework开发手册中的一段话

Spring 2.5引入了更多典型化注解(stereotype annotations): @Component@Service和 @Controller@Component是所有受Spring管理组件的通用形式;而@Repository@Service和 @Controller则是@Component的细化,用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)。也就是说,你能用@Component来注解你的组件类,但如果用@Repository@Service 或@Controller来注解它们,你的类也许能更好地被工具处理,或与切面进行关联。例如,这些典型化注解可以成为理想的切入点目标。当然,在Spring Framework以后的版本中, @Repository@Service和 @Controller也许还能携带更多语义。如此一来,如果你正在考虑服务层中是该用@Component还是@Service,那@Service显然是更好的选择。同样的,就像前面说的那样, @Repository已经能在持久化层中进行异常转换时被作为标记使用了。”

3.有了<context:component-scan>,另一个<context:annotation-config/>标签根本可以移除掉,因为已经被包含进去了。

4.<context:component-scan>提供两个子标签:<context:include-filter>和<context:exclude-filter>各代表引入和排除的过滤。

如:<context:component-scan base-package="com.xhlx.finance.budget" >

<context:include-filter type="regex" expression=".service.*"/>
</context:component-scan>

5.filter标签在Spring3有五个type,如下:

Filter Type Examples Expression Description
annotation org.example.SomeAnnotation 符合SomeAnnoation的target class
assignable org.example.SomeClass 指定class或interface的全名
aspectj org.example..*Service+ AspectJ语法
regex org\.example\.Default.* Regelar Expression
custom org.example.MyTypeFilter Spring3新增自訂Type,实作org.springframework.core.type.TypeFilter
时间: 2024-08-06 16:02:17

Spring组件扫描<context:component-scan/>使用详解(转)的相关文章

(转载)spring组件扫描&lt;context:component-scan/&gt;使用详解

好文章,原文地址:http://blog.sina.com.cn/s/blog_57769b7b0100tt5x.html 关于spring自动检测组件的使用方式网上太多了,而且也不是我记录的重点,我想说下一点可能你还不知道的经验 我们知道如果不想在xml文件中配置bean,我们可以给我们的类加上spring组件注解,只需再配置下spring的扫描器就可以实现bean的自动载入. 先写一个小例子,剩下的在下面解释 <!-- 定义扫描根路径为leot.test,不使用默认的扫描方式 --> &l

Spring组件扫描&lt;context:component-scan/&gt;使用详解

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/sch

Spring组件扫描&lt;context:component-scan/&gt;详解

最近使用Spring,发现有很多依赖注入的内容,特别是DAO,百思不得其解,后来才知道是Spring的依赖注入.Spring可以批量将一个目录下所有的植入@Repository 注解或者@Service 注解的组件类一次性扫描出来. 事例 <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans&quo

spring在IoC容器中装配Bean详解

1.Spring配置概述 1.1.概述 Spring容器从xml配置.java注解.spring注解中读取bean配置信息,形成bean定义注册表: 根据bean定义注册表实例化bean: 将bean实例放入bean缓存池: 应用程序使用bean. 1.2.基于xml的配置 (1)xml文件概述 xmlns------默认命名空间 xmlns:xsi-------标准命名空间,用于指定自定义命名空间的schema文件 xmlns:xxx="aaaaa"-------自定义命名空间,xx

spring获取webapplicationcontext,applicationcontext几种方法详解(转)

方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("beanId"); 说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况. 方法二:通过Spring提供的工具类获取ApplicationConte

Spring Boot 启动(二) 配置详解

Spring Boot 启动(二) 配置详解 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Boot 配置文件加载顺序 Spring Boot 配置文件加载分析 - ConfigFileApplicationListener 一.Spring Framework 配置 略... 二.Spring Boot 配置 2.1 随机数配置 name.value=${random.int} name.int=${

Spring中常用的配置和注解详解

一.  Spring中常用的配置文件详解 Spring中的配置文件详解 1.<!-- 配置注解bean的扫描路径 该配置表示从cn包下开始扫描--> <context:component-scan base-package="cn"></context:component-scan> 2.<!-- 加载资源文件 其中Location表示从哪个路径加载配置文件properties--> <context:property-placeh

Spring学习(六)bean装配详解之 【通过注解装配 Bean】【基础配置方式】

通过注解装配 Bean 1.前言 优势 1.可以减少 XML 的配置,当配置项多的时候,XML配置过多会导致项目臃肿难以维护 2.功能更加强大,既能实现 XML 的功能,也提供了自动装配的功能,采用了自动装配后,程序猿所需要做的决断就少了,更加有利于对程序的开发,这就是“约定优于配置”的开发原则 IOC发现Bean的两种方式 组件扫描:通过定义资源的方式,让 Spring IoC 容器扫描对应的包,从而把 bean 装配进来. 自动装配:通过注解定义,使得一些依赖关系可以通过注解完成. 2.使用

Spring、Spring MVC、MyBatis整合文件配置详解

使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ 基本的组织结构和用法就不说了,前面的博客和官方文档上都非常的全面.jar包可以使用Maven来组织管理.来看配置文件. web.xml的配置