码迷,mamicode.com
首页 > 编程语言 > 详细

springboot使用ImportResource注解加载spring配置文件(传智播客代码)

时间:2020-04-05 22:13:05      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:rgs   spring配置   ott   pat   autowired   code   http   gui   com   

接上篇: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);
    }

}

测试结果:

技术图片

 

springboot使用ImportResource注解加载spring配置文件(传智播客代码)

标签:rgs   spring配置   ott   pat   autowired   code   http   gui   com   

原文地址:https://www.cnblogs.com/passedbylove/p/12639332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!