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

spring:使用注解@Configuration、@ComponentScan创建配置类(未完待续)

创建配置类

如题,前例使用xml文件加载bean类,然后通过每个bean的id获取bean实例。注解@ComponentScan注解默认就会装配标识了@Controller,@Service,@Repository,@Component注解的类到spring容器中,功能同xml问价加载bean类。

注解@Configuration标注为配置类。

注解@ComponentScan指定扫描范围

配置类

package com.annotation.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;/* ** @copyright 2003-2025* @author    qiao wei* @date      2025-06-13* @version   1.0* @brief     * @history   name*            date*            brief*/
@Configuration
@ComponentScan(basePackages = {"com.annotation.thirdjar","com.annotation.dao.impl"}
)
public class ConfigForDateForamt02 {
}

扫描包com.annotation.thirdjar和com.annotation.dao.impl中所有类。

加载配置类:

public void test04() {ApplicationContext context =new AnnotationConfigApplicationContext(ConfigForDateForamt02.class);InterfaceUserService userService = (InterfaceUserService) context.getBean("getUserService0100");userService.show();}

详解注解@CompScan

注解@ComponentScan源码如下

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {   /**    * 对应的包扫描路径 可以是单个路径,也可以是扫描的路径数组    * @return    */   @AliasFor("basePackages")   String[] value() default {};   /**    * 和value一样是对应的包扫描路径 可以是单个路径,也可以是扫描的路径数组    * @return    */   @AliasFor("value")   String[] basePackages() default {};   /**    * 指定具体的扫描的类    * @return    */   Class<?>[] basePackageClasses() default {};   /**    * 对应的bean名称的生成器 默认的是BeanNameGenerator
http://www.xdnf.cn/news/14206.html

相关文章:

  • mysql8数据库本地能连上但是远程连不上
  • AI作画提示词:Prompts工程技巧与最佳实践
  • GEO指南之内容创业者:AI时代的“品牌大模型种草”与IP推荐力打造
  • OSPF基础实验案例
  • Java登录验证后台实现详解
  • 【QSoundEffect QT 音频文件的播放】
  • 岛屿周长问题的三种解法:直接计数法、数学计算法与深度优先搜索
  • 精益数据分析(100/126):SaaS行业付费注册率优化与商业模式选择策略
  • Vue3本地存储实现方案
  • java通过hutool工具生成二维码实现扫码跳转功能
  • 【C/C++ 为什么 unique_ptr 不支持拷贝构造、赋值构造等操作】
  • SpringBoot项目接入DeepSeek指南:从零开始实现AI能力整合
  • PyTorch优化器总结
  • JS进阶 Day01
  • 前端面经整理【1】
  • 人工智能嵌入公共服务治理的风险挑战(一)
  • meshgpt 笔记2
  • 企业AI深水区突围:从星辰大海到脚下泥泞的进化论
  • 第六天 界面操作及美化(6.2 控件属性节点)
  • 组件化 websocket
  • VSCode - VSCode 转换英文字母的大小写
  • go语言快速入门
  • Stored XSS(存储型跨站脚本攻击)
  • 日常运维问题汇总_41
  • RAG数据集综述
  • PHP设计模式实战:构建高性能API服务
  • java agent技术(二)
  • ByteTrack:通过关联每个检测框实现多目标跟踪
  • NY197NY205美光闪存固态NY218NY226
  • 阳台光伏配套电表ADL200N-CT/D16-Wf-1