news 2026/4/21 11:30:09

(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/4/21 10:23:15

Azure API Management 多后端负载均衡与智能重试策略优化

背景 在使用 Azure OpenAI Service 的生产环境中,我们部署了多个区域的 GPT 模型来提供服务。为了实现高可用性和负载均衡,需要在 Azure API Management (APIM) 层面实现智能的请求分发和故障转移机制。 架构概览 后端资源部署 我们在三个 Azure 区域部署了 OpenAI 资源:…

作者头像 李华
网站建设 2026/4/16 12:29:29

迈瑞医疗董事长李西廷提前完成2亿元增持计划 坚定未来发展信心

12月15日&#xff0c;迈瑞医疗发布公告称&#xff0c;2025年11月27日至2025年12月12日期间&#xff0c;实际控制人之一、董事长李西廷通过集中竞价交易的方式累计增持公司股份1005381股&#xff0c;增持股份占公司股份的0.08%&#xff0c;增持金额共计人民币2亿元&#xff08;含…

作者头像 李华
网站建设 2026/4/16 12:40:35

终极指南:使用LSUnusedResources快速清理iOS项目冗余资源

终极指南&#xff1a;使用LSUnusedResources快速清理iOS项目冗余资源 【免费下载链接】LSUnusedResources A Mac App to find unused images and resources in Xcode project. 项目地址: https://gitcode.com/gh_mirrors/ls/LSUnusedResources 在iOS开发过程中&#xff…

作者头像 李华
网站建设 2026/4/20 18:50:19

6、搭建 Linux 防火墙全攻略

搭建 Linux 防火墙全攻略 1. 安全基础:Windows 与防火墙的局限 在网络安全领域,防火墙固然重要,但不能忽视应用层和操作系统的安全。一些管理员建议,在配置服务器时应假设没有防火墙,这是个不错的策略。像 Linux 和 Unix 服务器,经过强化后甚至可以不需要防火墙。然而,…

作者头像 李华
网站建设 2026/4/18 16:09:34

27、跨平台远程图形桌面与OpenVPN安全网络搭建

跨平台远程图形桌面与OpenVPN安全网络搭建 1. VNC与SSH隧道 VNC(Virtual Network Computing)会话可通过SSH隧道运行,测试其是否正常运行很简单,只需从SSH会话中注销,若VNC随之关闭,则说明运行正常。SSH隧道适用于所有支持SSH的操作系统,在Linux系统之间使用效果良好,…

作者头像 李华
网站建设 2026/4/18 0:05:40

【每日一读Day11】2025年度AI十大趋势报告

1️⃣ 趋势一&#xff1a;算力基建化&#xff1a;数据中心需求狂飙&#xff0c;算力经济是智能产业第一大引擎 算力 ≈ 电力 / 高速公路核心不再是“有没有 GPU”&#xff0c;而是&#xff1a; 数据中心选址能耗、散热调度效率 对工程的影响&#xff1a;算力调度、推理性价比 &…

作者头像 李华