news 2026/6/10 16:40:01

(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/6/10 11:12:10

2025年降ai工具大盘点:三大类10款热门降ai率神器全面评测

今年AIGC检测这块儿真是翻车重灾区。现在用AI写初稿不是秘密&#xff0c;但问题是&#xff0c;论文aigc的“AI味”实在太重&#xff0c;学校的检测系统一抓一个准。 我这俩月几乎把市面上能找到的降ai工具都踩了一遍坑&#xff0c;花了不少冤枉钱。怎么说呢&#xff0c;很多工…

作者头像 李华
网站建设 2026/6/10 12:31:18

9个研究生论文写作工具,AI辅助让你高效完成毕业论文

9个研究生论文写作工具&#xff0c;AI辅助让你高效完成毕业论文 论文写作的“三座大山”&#xff1a;时间、质量与重复率 对于研究生来说&#xff0c;毕业论文不仅是一场学术能力的全面检验&#xff0c;更是一次体力与心理的双重挑战。从选题到开题报告&#xff0c;从文献综述到…

作者头像 李华
网站建设 2026/6/10 14:35:16

1、Linux 网络技术全解析:从基础到高级应用

Linux 网络技术全解析:从基础到高级应用 1. 适用人群与基础要求 如果你在网络使用中遇到诸如网速慢却不知如何解决的问题,或是小型企业中负责网络事务的 IT 人员,又或是对网络技术充满兴趣想深入学习的爱好者,甚至是已有一定知识但想填补空白的专业人士,那么接下来的内容…

作者头像 李华
网站建设 2026/6/10 16:02:36

54、网络术语详解:从基础概念到高级协议

网络术语详解:从基础概念到高级协议 在当今数字化的时代,网络已经成为我们生活和工作中不可或缺的一部分。了解网络术语对于理解网络的工作原理、优化网络性能以及保障网络安全至关重要。本文将详细介绍一系列常见的网络术语,帮助您深入了解网络世界。 1. 路由协议相关术语…

作者头像 李华