news 2026/7/7 20:27:26

(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/7/7 8:16:50

如何在3步内完成MLX模型转换?实用指南来了!

如何在3步内完成MLX模型转换&#xff1f;实用指南来了&#xff01; 【免费下载链接】mlx-examples 在 MLX 框架中的示例。 项目地址: https://gitcode.com/GitHub_Trending/ml/mlx-examples 还在为PyTorch模型在Apple芯片上的性能瓶颈而烦恼吗&#xff1f;&#x1f914;…

作者头像 李华
网站建设 2026/7/7 11:28:36

非球面透镜的聚焦点研究

摘要高功率激光二极管通常在两个方向之间显示出不对称的发散和像散。例如&#xff0c;激光二极管首先由物镜准直&#xff0c;然后由非球面聚焦&#xff0c;在VirtualLab中研究了镜头聚焦区域的演化。与没有像散的情况相比&#xff0c;清楚地呈现了像散对聚焦区域的影响。建模任…

作者头像 李华
网站建设 2026/7/6 23:39:12

SCRFD人脸检测算法:从原理到部署的完整实战指南

SCRFD人脸检测算法&#xff1a;从原理到部署的完整实战指南 【免费下载链接】insightface State-of-the-art 2D and 3D Face Analysis Project 项目地址: https://gitcode.com/GitHub_Trending/in/insightface 还在为项目中的人脸检测模块性能不佳而苦恼吗&#xff1f;在…

作者头像 李华
网站建设 2026/7/7 20:06:08

解密GEO生成引擎优化:5个让AI优先推荐你品牌的底层逻辑

在AI搜索流量占比突破43%的2025年&#xff08;数据来源&#xff1a;Gartner&#xff09;&#xff0c;传统SEO策略已无法满足生成式AI环境下的内容分发需求。GEO&#xff08;Generative Engine Optimization&#xff09;作为新一代优化范式&#xff0c;正在重构企业获取AI流量的…

作者头像 李华