@ImportResource 注解的使用

@ImportResource注解:用于导入 Spring 的 xml 配置文件,让该配置文件中定义的 bean 对象加载到Spring容器中。

1.Spring 方式的配置文件 beans.xml

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--将 HelloService 以xml的方式,注入到容器中-->
    <bean id="helloService" class="com.demo.springboot.service.HelloService"/>        
</beans>

2.使用@ImportResource注解,引入 xml 配置

java 复制代码
/**
 * Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
 * 如果想让Spring的配置文件生效,加载到Spring 容器中来;
 * 使用@ImportResource注解,将其标注在一个配置类上(此处配置在启动类)
 */
@SpringBootApplication
@ImportResource(locations = {"classpath:beans.xml"})
public class BootApplication {

    public static void main(String[] args) {
        // Spring应用启动起来         
        SpringApplication.run(BootApplication.class,args);

    }
}

classpath 和 classpath*的 区别

classpath只会到你指定的class路径中查找找文件 ;
classpath*不仅包含class路径,还包括jar文件中(class路径)进行查找.

1:未打包前classpath就是项目结构中的src文件夹。

2:经过maven打包以后你会在idea中看见一份target文件夹,这里边的classes就是classpath。

相关推荐
一线大码8 分钟前
Smart-Doc 的简单使用
java·后端·restful
MacroZheng2 小时前
Claude Code官方桌面端正式发布,夯爆了!
java·人工智能·后端
虚无境2 小时前
如何编写一个SpringBoot项目告警推送的Starter
java·prometheus·webhook
NE_STOP17 小时前
Vide Coding--AI编程工具的选择
java
码云数智-园园17 小时前
C++20 Modules 模块详解
java·开发语言·spring
程序员黑豆17 小时前
JDK 下载安装与配置详细教程
java·前端·ai编程
小宇宙Zz18 小时前
Maven依赖冲突
java·服务器·maven
swordbob18 小时前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
咖啡八杯18 小时前
GoF设计模式——享元模式
java·spring·设计模式·享元模式