所以在这里我将从Spring中比较重要的标签<context:component-scan base-package=""/> 入手,讲一讲这个标签的解析类是怎么去解析的。 1. 2)配置了context:component-scan标签后还需要配置context:annotation-config标签吗? " annotation-config="true"> </context:component-scan> 如下是相关部分源代码: // 如有必要,注册注释配置处理器。 这二个配置文件下都需要配置包扫描context:component-scan,所以这里为了避免重复扫描,可能会采用如下方式来配置: my-service.xml <context:component-scan > my-mvc.xml <context:component-scan base-package="cn.edu.his.pay"> <!
本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7717331.html 解决问题 通过component-scan spring容器中,去除XML配置文件bean手动注册过程,降低XML配置文件繁琐性; 内容说明 1、注册扫描bean并使用@Autowired注解自动装配时,需在XML配置文件中引入 <context:component-scan base-package="com.spring.example.scan"/>; 2、通过component-scan自动扫描定义基类包下所有bean,需要在类名前加入注解@Component --自动扫描检测bean以及定义bean,并将bean放入spring容器--> <context:component-scan base-package="com.spring.example.scan 总结 component-scan自动扫描优缺点: 优点: a> component-scan自动扫描注册装配可以有效解决annotation-config的不足,不需要在spring配置文件中需提前指明
<context:component-scan base-package="com.hafiz.www.controller"> <context:include-filter type ="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <context:component-scan base-package="com.hafiz.www"> <context:include-filter type="annotation " expression="org.springframework.stereotype.Controller"/> </context:component-scan> 这是因为什么呢? 解决办法是:在spring-mvc.xml配置文件中的context:component-scan标签中使用use-default-filters=“false”禁用掉默认的行为。
指定扫描的资源 <context:component-scan base-package="com.sangyu.test05.annotation" resource-pattern="repository --context:exclude-filter 子节点指定排除哪些指定表达式的组件--> <context:component-scan base-package="com.sangyu.test05 --context:include-filter 子节点指定包含哪些表达式的组件,该子节点需要use-default-filters配合使用--> <context:component-scan --type="assignable" 所有继承或扩展XxxService的类,该类型采用目标类是否继承或扩展某个特定类进行过滤 --> <context:component-scan base-package > <context:component-scan base-package="com.sangyu.test05.annotation" use-default-filters="false">
什么都不说,先直接上代码 SpringMVC的包扫描 <context:component-scan base-package="com.voidpawm.springmvc"/> Spring 的包扫描 <context:component-scan base-package="com.voidpawm.springmvc"/> package com.voidpawm.springmvc; -- 配置包扫描器 限制只扫描Handle的注解@Controller和异常处理的注解@ControllerAdvice--> <context:component-scan base-package expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan -- 配置包扫描器 限制不扫描Handle的注解@Controller和异常处理的注解@ControllerAdvice--> <context:component-scan base-package
-- 组件扫描--> <context:component-scan base-package="com.spring.bean"></context:component-scan -- <context:component-scan base-package="com.spring.bean,com.spring.web"></context:component-scan -- <context:component-scan base-package="com.spring"></context:component-scan>-->测试类@Test public -- 第一种 use-default-filters="false"如果是false表示这个包下所有带有声明的注解全部失效<context:component-scan base-package= -- 组件扫描--> <context:component-scan base-package="com.spring.bean"></context:component-scan
exclude-filter type="annotation" expression="org.springframework.stereotype.Component"/> </context:component-scan > type=“assignable” type=“assignable” : 指定排除某个具体的类,按照类排除 expression="" :类的全类名 <context:component-scan <context:exclude-filter type="assignable" expression="com.dhy.Factory.main"/> </context:component-scan > type=“assignable” type=“assignable” : 指定排除某个具体的类,按照类排除 expression="" :类的全类名 <context:component-scan > <context:include-filter type="assignable" expression="com.dhy.Factory.main"/> </context:component-scan
<context:component-scan base-package="myproject"> 和<context:component-scan base-package="myproject.controller 我思考和查询很久,话不多说,也实践做起: (1)只在applicationContext.xml中配置如下 <context:component-scan base-package="myproject org.springframework.beans.factory.annotation.Autowired(required=true)} (3)在applicationContext.xml和spring-servlet.xml中都配置过滤<context:component-scan base-package="myproject"> 和<context:component-scan base-package="myproject.controller" />, 不报错,请求正常, 总结:由此可以分析<context:component-scan base-package="myproject">主要过滤@Controller,同时还查资料了解<context:component-scan
在上篇笔记《context:component-scan 配置 —— 分库遇到问题(1)》中解决了 spring中某些实例被初始化了两次的问题, 但是紧接着又来了另一个头疼的问题,dubbo的@Reference <dubbo:annotation /> <context:component-scan base-package="xxx" use-default-filters="false"> <context type="annotation" expression="org.springframework.web.bind.annotation.RestController" /> </context:component-scan 我始终在想,之前dubbo使用没有问题的,就在我昨天加了use-default-filters=”false”才出现的这个问题,所以我围绕着 context:component-scan + dubbo /> 也可以使用:(等价于前面的:<dubbo:annotation package=”com.foo.bar.service” />) <dubbo:annotation /> <context:component-scan
-- use-default-filters="false" 设置取消默认包含规则 --> <context:component-scan base-package="com" use-default-filters <context:exclude-filter type="assignable" expression="com.atguigu.service.BookService"/> </context:component-scan -- context:component-scan 包扫描 base-package 设置你要扫描的包,以子包 base-package="com" 表示Spring 会扫描com包,以及它所有子包都会扫描 --> <context:component-scan base-package="com"> <! -- context:component-scan 包扫描 base-package 设置你要扫描的包,以子包 base-package="com" 表示Spring
-- use-default-filters="false" 设置取消默认包含规则 --> <context:component-scan base-package="com" use-default-filters <context:exclude-filter type="assignable" expression="com.atguigu.service.BookService"/> </context:component-scan -- context:component-scan 包扫描 base-package 设置你要扫描的包,以子包 base-package="com" 表示Spring 会扫描com包,以及它所有子包都会扫描 --> <context:component-scan base-package="com"> <! -- context:component-scan 包扫描 base-package 设置你要扫描的包,以子包 base-package="com" 表示Spring
在Spring配置文件中,使用context:component-scan标签来扫描包。确保扫描的包路径正确,以及包中的类带有适当的注解(如@Component、@Service等)。 示例代码:xmlCopy code<context:component-scan base-package="com.example" />4. 通过使用context:component-scan标签,我们可以告诉Spring框架在哪个包下扫描组件,并将其注册为Spring的bean。 下面是context:component-scan标签的详细介绍: 功能:扫描指定包下的类,并注册为Spring的bean。支持自动检测和注册具有特定注解的类。 在Spring的配置文件中,我们可以使用context:component-scan标签来实现自动扫描和注册:xmlCopy code<context:component-scan base-package
<context:component-scan base-package="com.nnngu" /> 从Spring提供的参考手册中我们得知该配置的功能是扫描配置的base-package包下的所有使用了 在applicationContext.xml中配置了<context:component-scan base-package=“com.nnngu" />,负责所有需要注册的Bean的扫描和注册工作。 在applicationContext-MVC.xml中配置<context:component-scan base-package=“com.nnngu" />,重启后,验证成功,springMVC跳转有效 type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> -- SpringMVC容器中只注册带有@Controller注解的Bean --> <context:component-scan base-package="com.nnngu" use-default-filters
当访问hello时出现如下问题: 解决方案: 之前扫描包时这样: <context:component-scan base-package=”com.itshenjin.controller .*” /> 修改后: <context:component-scan base-package=”com.itshenjin.controller” /> 或者:<context:component-scan base-package=”com.itshenjin.controller.**” /> 如果能解决你的问题,那最好不过了。
<context:component-scan base-package="com.test" /> 些扫描配置默认use-default-filters="true",默认扫描@Component注解及子注解 <context:component-scan base-package="com.test" use-default-filters="false"> <context:include-filter context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan Expression custom org.example.MyTypeFilter 自定义Type,实现接口org.springframework.core.type.TypeFilter 另外,<context:component-scan
--注解的组件扫描--> <context:component-scan base-package="com.xdr630"></context:component-scan> 2. xml 方式配置实现 base-package="com.xdr630"></context:component-scan> 测试: [在这里插入图片描述] [在这里插入图片描述] xml配置和注解配置达到相同的效果 使用 还需要使用注解替代的配置如下: 非自定义的Bean的配置:<bean> 加载properties文件的配置:<context:property-placeholder> 组件扫描的配置:<context:component-scan 文件配置 [在这里插入图片描述] 新建一个核心配置类:SpringConfiguration,相当于总配置 //标志改类是Spring核心配置类 @Configuration //<context:component-scan base-package="com.xdr630"></context:component-scan> @ComponentScan("com.xdr630") //<import resource=
要启用此扫描,我们将需要在applicationContext.xml文件中使用context:component-scan标记。 <context:component-scan base-package="cn.howtodoinjava.demo.service" /> <context:component-scan base-package ="cn.howtodoinjava.demo.dao" /> <context:component-scan base-package="cn.howtodoinjava.demo.controller " /> context:component-scan元素需要一个base-package属性,顾名思义,该属性指定了递归组件搜索的起点。 我们可能不希望将顶层软件包交给spring,所以应该声明三个component-scan元素,每个元素都具有指向不同软件包的base-package属性。
项目中 springMvc的部分配置如下: <dubbo:annotation /> <context:component-scan base-package="xxx"> <context: type="annotation" expression="org.springframework.web.bind.annotation.RestController" /> </context:component-scan Repository.class)) public class AppConfig { ... } and the equivalent using XML <beans> <context:component-scan "annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan
base-package="com.citi"> </context:component-scan> <! base-package="com.citi"> </context:component-scan> </beans> 在java包下新建controller包和service包,增加HalloController include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan > 将Spring 配置文件修改为 <context:component-scan base-package="com.citi"> <context:exclude-filter type=" annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> 保存配置并重新启动应用
创建持久层组件UserDaoImpl @Repository public class UserDaoImpl implements UserDao { } 扫描组件 情况一:最基本的扫描方式 <context:component-scan base-package="com.kailong"> </context:component-scan> 情况二:指定要排除的组件 <context:component-scan base-package context:exclude-filter type="assignable"expression="com.kailong.controller.UserController"/>--> </context:component-scan > 情况三:仅扫描指定组件 <context:component-scan base-package="com.kailong" use-default-filters="false"> <! context:include-filter type="assignable"expression="com.kailong.controller.UserController"/>--> </context:component-scan