springboot导入xml

在App.java类无法扫描的包下编写HelloService;

package org.kfit.service;

import org.springframework.stereotype.Service;

@Service
publicclass HelloService {

    /**
     * 启动的时候观察控制台是否打印此信息;
     */
    public HelloService() {
       System.out.println("HelloService.HelloService()");
       System.out.println("org.kfit.service.HelloService.HelloService()");
       System.out.println("HelloService.HelloService()");
    }

}
<?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">

    <!-- 注入spring boot无法扫描到的bean. -->
    <bean id="helloService" class="org.kfit.service.HelloService"></bean>

</beans>

编写ConfigClass注入配置文件application-bean.xml

package com.kfit.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

/**
 * classpath路径:locations={"classpath:application-bean1.xml","classpath:application-bean2.xml"}
 * file路径: locations = {"file:d:/test/application-bean1.xml"};
 */
@Configuration
@ImportResource(locations={"classpath:application-bean.xml"})
//@ImportResource(locations={"file:d:/test/application-bean1.xml"})
publicclass ConfigClass {

}

app.java


package com.kfit;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 *
 *
 * 大家也许会看到有些demo使用了3个注解: @Configuration;
 *
 * @EnableAutoConfiguration
 * @ComponentScan
 *
 *   其实:@SpringBootApplication申明让spring boot自动给程序进行必要的配置,
 *
 * 等价于以默认属性使用@Configuration,
 *  @EnableAutoConfiguration和@ComponentScan
 *
 * 所以大家不要被一些文档误导了,让自己很迷茫了,希望本文章对您有所启发;
 *
 * @author Angel(QQ:412887952)
 * @version v.0.1
 */
@SpringBootApplication
public class App {
       public static void main(String[] args) {
              SpringApplication.run(App.class, args);
       }
}

ImportResouce有两种常用的引入方式:classpath和file,具体查看如下的例子
classpath路径:locations={“classpath:application-bean1.xml”,
“classpath:application-bean2.xml”
}
file路径:
locations = {“file:d:/test/application-bean1.xml”};

原文:http://412887952-qq-com.iteye.com/blog/2293846

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值