@ImportResource 注解的使用

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

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




    
            

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

/**
 * 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。

你可能感兴趣的:(java,spring)