当前位置: 首页 > news >正文

(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>
http://www.cnnetsun.cn/news/82476.html

相关文章:

  • AndroidGen-GLM-4-9B:开启移动智能体新时代的革命性突破
  • 微服务架构下的分布式数据加密:SQLCipher实战指南
  • 国产开源,含20+视频AI算法模型,兼容GB28181、ONVIF、RTMP协议、私有化部署,10天搞定IoT物联网项目交付
  • 16、搭建 Asterisk VoIP 服务器全攻略
  • Apache Mesos集群运维实战:故障恢复与版本管理完全指南
  • 探索式测试的Session管理法:提升软件测试效率与可追溯性
  • 购物省钱参考:爱创猫电商优惠券领取方式
  • 12、Red Hat Enterprise Linux硬件分析与管理指南
  • 35、Linux 内核监控与调试:NUMA、AltSysRq 及 Kdump 全解析
  • DataEase开源BI工具完整指南:从零开始的数据可视化之旅
  • Gutenberg性能优化终极指南:零成本加速WordPress编辑器
  • ag-ui与LangGraph集成终极指南:构建企业级AI工作流的完整教程
  • 2026毕设ssm+vue基于架构的校园二手物品交易论文+程序
  • 手机强制开启USB调试模式终极指南:轻松解决设备连接难题
  • DataEase开源BI工具完整安装配置指南:从零开始快速部署
  • ReClassEx终极指南:免费开源的内存逆向分析神器
  • 物联网平台前端技术重构:从技术困境到用户体验的完美蜕变
  • 动态资源池化技术:让高价值软件许可“流动”起来的关键策略
  • 24、网络服务中的 SSH 与代理服务器使用指南
  • 2025护网蓝队面试题库,参加护网看这个就够了!
  • 3步精通FLAN-T5 XL:从零到多语言AI实战指南
  • Kubernetes环境下Hadoop存储配置的7个关键问题及解决方案
  • Moonlight安卓串流:5分钟实现手机玩PC游戏的终极指南
  • 40、Postfix性能测试、故障排查及相关标准参考
  • 41、邮件服务器配置与优化全解析
  • 如何评价 Nano Banana Pro?
  • 如何快速掌握Gemma模型转换:面向开发者的终极指南
  • 前端UI框架汇总,零基础入门到精通,收藏这篇就够了
  • 前端开发者必知的AI核心概念与技术栈全解析,收藏这篇就够了
  • keyboard-layout-editor 终极指南:从零开始打造你的专属键盘