zoukankan      html  css  js  c++  java
  • springboot使用ImportResource注解加载spring配置文件(尚硅谷)

    接上篇:springboot使用PropertyResource注解读取指定配置文件的属性(传智播客代码)
    @ImportResource可以加载多个配置文件


    DemoApplication.java
    package com.atguigu;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ImportResource;
    
    @ImportResource(locations = "classpath:beans.xml")
    @SpringBootApplication
    public class DemoApplication {
    
        public static void main(String[] args) {
    
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }

    beans.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">
    
        <bean id="helloService" class="com.atguigu.service.HelloService"/>
    </beans>

    HelloService.java

    package com.atguigu.service;
    
    public class HelloService {
    }

    测试用例

    package com.atguigu;
    
    
    import lombok.extern.slf4j.Slf4j;
    import org.junit.jupiter.api.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.context.ApplicationContext;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.util.List;
    import java.util.Map;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @Slf4j
    class DemoApplicationTests {
    
      
        @Autowired
        ApplicationContext context;
    
    
        @Test
        void containsBeanTest()
        {
            boolean contains = context.containsBean("helloService");
            log.info("是否存在HelloService的实例:{}",contains);
        }
    
    }

    测试结果:

  • 相关阅读:
    c#,winform,progressbar+labe,联动显示进度 Virus
    答某刘
    Expert .NET 2.0 IL Assembler·一校日记 (2)
    IL 二校汇总
    历史的思考(1)
    酝酿中,写一本《你不知道的IL》
    Expert .NET 2.0 IL Assembler·一校日记
    一校 疑难汇总
    强签名
    [转] 《解剖PetShop》系列之三
  • 原文地址:https://www.cnblogs.com/passedbylove/p/12639332.html
Copyright © 2011-2022 走看看