news 2026/4/29 9:18:32

(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/24 23:48:09

D3.js标签布局重构:从数据拥挤到视觉优雅的技术革新

D3.js标签布局重构&#xff1a;从数据拥挤到视觉优雅的技术革新 【免费下载链接】d3 Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada: 项目地址: https://gitcode.com/gh_mirrors/d3/d3 在数据可视化领域&#xff0c;标签重…

作者头像 李华
网站建设 2026/4/23 17:02:26

28、利用OpenVPN构建安全的跨平台虚拟专用网络

利用OpenVPN构建安全的跨平台虚拟专用网络 1. 静态密钥与PKI的对比 使用静态密钥存在一个问题,即会失去完美前向保密性,因为静态密钥从不改变。如果攻击者设法嗅探并捕获网络流量,然后获取并破解了加密密钥,那么攻击者就可以解密过去和未来的所有数据。而OpenVPN支持使用…

作者头像 李华
网站建设 2026/4/26 23:11:52

55、网络术语与Linux内核构建全解析

网络术语与Linux内核构建全解析 1. 重要网络术语介绍 在网络技术领域,有许多关键的术语和概念,它们构成了网络通信和数据处理的基础。下面将对一些重要的网络术语进行详细介绍。 1.1 TCAM - 三元内容可寻址存储器 与普通计算机中的RAM不同,普通RAM将数据存储在多个地址中…

作者头像 李华
网站建设 2026/4/27 0:08:41

如何高效配置Flutter CanvasKit渲染方案:5个关键优化策略

如何高效配置Flutter CanvasKit渲染方案&#xff1a;5个关键优化策略 【免费下载链接】engine The Flutter engine 项目地址: https://gitcode.com/gh_mirrors/eng/engine 还在为Flutter Web应用在复杂场景下的渲染卡顿和内存占用过高而困扰吗&#xff1f;作为基于WebAs…

作者头像 李华
网站建设 2026/4/26 22:20:57

光纤耦合装置的公差分析

摘要在现代光学中&#xff0c;光纤存在于各种光学系统中&#xff0c;能够将多少光耦合到光纤中一直是人们关注的问题。耦合效率对系统的对准十分敏感&#xff0c;特别是对于芯径相对较小的单模光纤。在本例中&#xff0c;我们选择了一个设计良好的光纤耦合透镜&#xff0c;并根…

作者头像 李华