`
m635674608
  • 浏览: 4923722 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

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
分享到:
评论

相关推荐

    Spring扫描器—spring组件扫描使用详解

    NULL 博文链接:https://gaozzsoft.iteye.com/blog/1523898

    springboot 基础简易实例, maven项目

    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt; &lt;version&gt;2.1.4.RELEASE&lt;/version&gt; &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt; &lt;/parent&gt; &lt;groupId&gt;com.example&lt;/groupId&gt; &lt;artifactId&gt;...

    spring-context-support-1.0.10-API文档-中文版.zip

    赠送jar包:spring-context-support-1.0.10.jar; 赠送原API文档:spring-context-support-1.0.10-javadoc.jar; 赠送源代码:spring-context-support-1.0.10-sources.jar; 赠送Maven依赖信息文件:spring-context-...

    Spring 报错:元素 "context:component-scan" 的前缀 "context" 未绑定的问题解决

    主要介绍了Spring 报错:元素 "context:component-scan" 的前缀 "context" 未绑定的问题解决的相关资料,需要的朋友可以参考下

    springweb3.0MVC注解(附实例)

    &lt;context:component-scan base-package="com.baobaotao.web"/&gt; &lt;!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 --&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation. ...

    spring boot2快速导出excel的java工具类demo源码:export-excel

    &lt;groupId&gt;org.apache.poi&lt;/groupId&gt; &lt;artifactId&gt;poi-ooxml&lt;/artifactId&gt; &lt;version&gt;RELEASE&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.poi&lt;/groupId&gt; &lt;artifactId&gt;poi&lt;/artifactId&gt; ...

    logback-ext-spring

    spring使用logback的扩展,使用起来非常方便。在web.xml中配置: &lt;context-param&gt; &lt;param-name&gt;logbackConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/conf/logback.xml&lt;/param-value&gt; &lt;/context-param&gt; ...

    基于MyEclipse搭建maven+springmvc整合图文教程(含源码0

    &lt;context:component-scan base-package="Controller" /&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/" /&gt; ...

    开发Spring的过程中出现Eclipse不能识别标签

    开发Spring的过程中出现Eclipse不能识别&lt;tx:advice /&gt;标签,需要引用 spring-tx标签,spring-tx-4.0.xsd 可以解决标签不识别的问题

    spring applicationContext 配置文件

    &lt;context:component-scan base-package="com.ccc"/&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0" /&gt; &lt;!-- 配置事务管理器 針對MES數據庫-...

    Spring注释 注入方式源码示例,Annotation

    &lt;context:component-scan base-package="Mode"&gt;&lt;/context:component-scan&gt; //表示在包mode下面的类将扫描带有@Component,@Controller,@Service,@Repository标识符的类并为之注入对象。 据说是因为XML配置太烦锁而...

    xfire的使用详解

    &lt;param-value&gt;classpath:spring/app.xml,classpath:org/codehaus/xfire/spring/xfire.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;!--配置xfire ws--&gt; &lt;servlet&gt; &lt;servlet-name&gt;XFireServlet&lt;/servlet-name&gt; ...

    springmvc-ibatis

    &lt;context:component-scan base-package="com.org"/&gt; &lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org....

    spring3.2+strut2+hibernate4

    &lt;context:component-scan base-package="*" /&gt; &lt;!-- &lt;aop:config&gt;--&gt; &lt;!-- execution第一个星号代表任何返回类型,第二个星号代表com.sbz.service下的所有包,第三个星号代表所有方法,括号中的两个点代表任何...

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    &lt;context:component-scan base-package="com.mvc" /&gt; &lt;mvc:annotation-driven /&gt; &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt; &lt;mvc:default-servlet-handler /&gt; &lt;aop:config proxy-...

    struts2.3+hibernate3.6+spring3.1整合的纯xml配置的小项目

    &lt;context:component-scan base-package="org.whvcse"&gt;&lt;/context:component-scan&gt; &lt;tx:annotation-driven transaction-manager="txManager" /&gt; &lt;!-- &lt;aop:config&gt; &lt;aop:pointcut id="defaultServiceOperation" ...

    SpringMVC+Hibernate实例

    &lt;context:component-scan base-package="com.bbs"/&gt; &lt;!--注解支持--&gt; &lt;mvc:annotation-driven/&gt; &lt;!--视图解析--&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view....

    spring和hibernate整合

    在web.Xml中&lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; ... &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt; &lt;/listener&gt;

    Maven拆分代码.zip

    &lt;context:component-scan base-package="com.itheima.service"/&gt; &lt;!--aop面向切面编程,切面就是切入点和通知的组合--&gt; &lt;!--配置事务管理器--&gt; &lt;bean id="transactionManager" class="org.springframework.jdbc...

Global site tag (gtag.js) - Google Analytics