news 2026/6/26 7:14:15

(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/25 22:58:39

ReClassEx内存分析终极指南:从入门到实战精通

在逆向工程和软件安全分析领域&#xff0c;内存分析是不可或缺的核心技能。本文将带你深度解析开源内存分析工具ReClassEx的实战应用&#xff0c;帮助开发者快速掌握这一强大工具的使用技巧。 【免费下载链接】ReClassEx ReClassEx 项目地址: https://gitcode.com/gh_mirrors…

作者头像 李华
网站建设 2026/6/25 1:00:36

21、网络路由与安全远程管理技术全解析

网络路由与安全远程管理技术全解析 在网络管理和安全领域,有许多实用的工具和技术可以帮助我们更好地管理网络和保障远程访问的安全。下面将为大家详细介绍 Quagga 路由守护进程的相关操作、RIPD 和 OSPFD 的监控方法、路由黑洞设置、OSPF 动态路由的使用、RIP 和 OSPF 的安全…

作者头像 李华
网站建设 2026/6/19 11:32:16

52、网络故障排除、参考资料与术语详解

网络故障排除、参考资料与术语详解 1. 网络配置与故障排除 在网络配置中,源、目标和过滤语句可以使用任意名称。例如,源 s_local 可以是 source local 或 source fred 等。虽然有使用 s_ 表示源语句、 d_ 表示目标语句的约定,但这并非强制要求。 不同 Linux 发…

作者头像 李华
网站建设 2026/6/25 19:18:59

56、Linux内核构建与网络配置全解析

Linux内核构建与网络配置全解析 1. 新增可加载内核模块 在Linux系统中,添加新的可加载内核模块是一项常见且重要的操作。以下是详细的步骤: 1. 切换到包含构建树的目录,例如: cd ~/kernel/linux-2.6.20.1准备一个最新的配置文件,并将其复制到构建树的顶级目录,然后运…

作者头像 李华
网站建设 2026/6/25 5:31:26

将 Graph RAG 投入生产——逐步指南

原文&#xff1a;towardsdatascience.com/graph-rag-into-production-step-by-step-3fe71fb4a98e?sourcecollection_archive---------0-----------------------#2024-09-23 https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/203dbf1037dad1…

作者头像 李华
网站建设 2026/6/24 23:05:01

GLAD:谐振腔的优化设计

概述利用GLAD的优化功能&#xff0c;谐振腔命令可以对谐振腔的各个参数进行优化设计。整个设计分为两个过程&#xff1a;首先&#xff0c;利用“resonator/test”和“resonator/set”命令对给定腔镜参数的谐振腔确定腔内本征模的尺寸。然后利用GLAD的优化功能针对特殊的谐振腔参…

作者头像 李华