news 2026/5/16 4:48:19

(25)选择性实例化Bean

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
(25)选择性实例化Bean

假设在某个包下有很多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 version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://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.xsd"><context:component-scanbase-package="com.powernode.spring6.bean3"use-default-filters="false"><context:include-filtertype="annotation"expression="org.springframework.stereotype.Controller"/></context:component-scan></beans>

use-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(){ApplicationContextapplicationContext=newClassPathXmlApplicationContext("spring-choose.xml");}

执行结
也可以将use-default-filters设置为true(不写就是true),并且采用exclude-filter方式排出哪些注解标注的Bean不参与实例化:

<context:component-scanbase-package="com.powernode.spring6.bean3"><context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Repository"/><context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Service"/><context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Controller"/></context:component-scan>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/10 20:31:27

Bootstrap 5.3 轻量化实战:从臃肿框架到精准组件的性能跃升

Bootstrap 5.3 轻量化实战&#xff1a;从臃肿框架到精准组件的性能跃升 【免费下载链接】bootstrap 项目地址: https://gitcode.com/gh_mirrors/boo/bootstrap 你是否曾经面对加载缓慢的网页感到无奈&#xff1f;数据显示&#xff0c;完整版Bootstrap 5.3的CSS和JS文件…

作者头像 李华
网站建设 2026/5/14 1:32:05

选对工具,提升成交率:新能源汽车客户跟进管理软件评测与指南

在新能源汽车零售领域&#xff0c;门店运营的核心挑战之一是如何系统化地管理客户跟进流程。跟进环节直接决定线索转化效率与客户体验&#xff0c;但传统依赖个人记录与分散沟通的方式&#xff0c;往往导致信息不透明、客户流失和团队协同效率低下。一、现有跟进模式的主要局限…

作者头像 李华
网站建设 2026/5/9 20:29:30

告别繁琐复制!clipboard.js让你的网页复制体验飞起来

告别繁琐复制&#xff01;clipboard.js让你的网页复制体验飞起来 【免费下载链接】clipboard.js :scissors: Modern copy to clipboard. No Flash. Just 3kb gzipped :clipboard: 项目地址: https://gitcode.com/gh_mirrors/cl/clipboard.js 还在为网页上的复制功能烦恼…

作者头像 李华
网站建设 2026/5/11 23:46:07

昆旺精密越南工厂实力出圈!TCL深度审厂后给予高度认可

近日&#xff0c;TCL集团一行代表莅临昆旺精密电子有限公司越南工厂进行实地审厂。本次来访的团队包括TCL的品质与采购负责人&#xff0c;旨在深入了解我司的制造实力与技术研发能力&#xff0c;为未来深入合作夯实基础。昆旺精密作为集研发、制造、贸易于一体的科技技术企业&a…

作者头像 李华