news 2025/12/28 11:35:12

(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进行投诉反馈,一经查实,立即删除!
网站建设 2025/12/20 9:53:53

AndroidGen-GLM-4-9B:开启移动智能体新时代的革命性突破

AndroidGen-GLM-4-9B&#xff1a;开启移动智能体新时代的革命性突破 【免费下载链接】androidgen-glm-4-9b 项目地址: https://ai.gitcode.com/zai-org/androidgen-glm-4-9b 2025年4月&#xff0c;智谱AI正式发布基于GLM-4-9B大模型的AndroidGen开源版本&#xff0c;标…

作者头像 李华
网站建设 2025/12/21 11:21:11

微服务架构下的分布式数据加密:SQLCipher实战指南

在微服务架构日益普及的今天&#xff0c;数据安全已成为每个技术团队必须直面的挑战。你是否曾为分布式环境下的数据加密而头疼&#xff1f;多个服务节点间的密钥同步、跨服务数据传输加密、性能损耗控制等问题往往让开发者望而却步。本文将通过SQLCipher加密库&#xff0c;为你…

作者头像 李华
网站建设 2025/12/24 5:11:38

16、搭建 Asterisk VoIP 服务器全攻略

搭建 Asterisk VoIP 服务器全攻略 1. 配置与测试 首先,我们需要进行一系列的配置和测试工作。以下是相关的配置参数: format=wav49 skipms=3000 maxsilence=10 silencethreshold=128 maxlogins=3 [local-vm-users] ;mailbox number, password, username 250 => 1234,E…

作者头像 李华
网站建设 2025/12/21 12:26:10

Apache Mesos集群运维实战:故障恢复与版本管理完全指南

Apache Mesos集群运维实战&#xff1a;故障恢复与版本管理完全指南 【免费下载链接】mesos Apache Mesos 项目地址: https://gitcode.com/gh_mirrors/mesos2/mesos 在现代分布式系统中&#xff0c;Mesos集群维护是确保业务连续性的关键环节。本指南将深入解析Mesos集群运…

作者头像 李华
网站建设 2025/12/21 22:20:50

探索式测试的Session管理法:提升软件测试效率与可追溯性

探索式测试作为一种强调测试者自主性与创造性的软件测试方法&#xff0c;在快速迭代的现代开发环境中日益重要。然而&#xff0c;其非结构化的特性可能导致测试过程难以追踪和评估。Session管理法应运而生&#xff0c;它通过引入时间盒&#xff08;Time-boxed&#xff09;会话和…

作者头像 李华