东莞建网站平台郴州必去三个景点
2026/1/22 6:29:08 网站建设 项目流程
东莞建网站平台,郴州必去三个景点,青岛模板建站多少钱,学院网站建设通知假设在某个包下有很多Bean#xff0c;有的Bean上标注了Component#xff0c;有的标注了Controller#xff0c;有的标注了Service#xff0c;有的标注了Repository#xff0c;现在由于某种特殊业务的需要#xff0c;只允许其中所有的Controller参与Bean管理#xff0c;其…假设在某个包下有很多Bean有的Bean上标注了Component有的标注了Controller有的标注了Service有的标注了Repository现在由于某种特殊业务的需要只允许其中所有的Controller参与Bean管理其他的都不实例化。这应该怎么办呢packagecom.powernode.spring6.bean3;importorg.springframework.stereotype.Component;importorg.springframework.stereotype.Controller;importorg.springframework.stereotype.Repository;importorg.springframework.stereotype.Service;ComponentpublicclassA{publicA(){System.out.println(A的无参数构造方法执行);}}ControllerclassB{publicB(){System.out.println(B的无参数构造方法执行);}}ServiceclassC{publicC(){System.out.println(C的无参数构造方法执行);}}RepositoryclassD{publicD(){System.out.println(D的无参数构造方法执行);}}ControllerclassE{publicE(){System.out.println(E的无参数构造方法执行);}}ControllerclassF{publicF(){System.out.println(F的无参数构造方法执行);}}我只想实例化bean3包下的Controller。配置文件这样写?xml version1.0 encodingUTF-8?beansxmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdcontext:component-scanbase-packagecom.powernode.spring6.bean3use-default-filtersfalsecontext:include-filtertypeannotationexpressionorg.springframework.stereotype.Controller//context:component-scan/beansuse-default-filters“true” 表示使用spring默认的规则只要有Component、Controller、Service、Repository中的任意一个注解标注则进行实例化。use-default-filters“false”表示不再spring默认实例化规则即使有Component、Controller、Service、Repository这些注解标注也不再实例化。context:include-filter type“annotation” expression“org.springframework.stereotype.Controller”/ 表示只有Controller进行实例化。TestpublicvoidtestChoose(){ApplicationContextapplicationContextnewClassPathXmlApplicationContext(spring-choose.xml);}执行结也可以将use-default-filters设置为true不写就是true并且采用exclude-filter方式排出哪些注解标注的Bean不参与实例化context:component-scanbase-packagecom.powernode.spring6.bean3context:exclude-filtertypeannotationexpressionorg.springframework.stereotype.Repository/context:exclude-filtertypeannotationexpressionorg.springframework.stereotype.Service/context:exclude-filtertypeannotationexpressionorg.springframework.stereotype.Controller//context:component-scan

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询