1. 程式人生 > >spring boot匯入xml配置檔案

spring boot匯入xml配置檔案

【來也匆匆,去也匆匆,在此留下您的腳印吧,轉發點贊評論;

您的認可是我最大的動力,感謝您的支援】

SpringBoot理念就是零配置程式設計,但是如果絕對需要使用XML的配置,我們建議您仍舊從一個@Configuration類開始,你可以使用@ImportResouce註解載入XML配置檔案,我拿一個例子來進行講解:

這個例子的大體步驟如下:

(1)新建一個工程;

(2)在App.Java類編寫HelloService2;

(3)在App.java類無法掃描的包下編寫HelloService;

(4)編寫application-bean.xml注入HelloService;

(5)編寫ConfigClass注入配置檔案application-bean.xml;

(6)編寫App.java啟動類進行測試;

(7)其它說明

(1)新建一個工程;

我們在前幾節的例子已經寫到hello2了,我們取一個新的名稱為spring-boot-hello3,這裡沒有什麼難點,不過多介紹,還有難處的可以檢視之前的例子,當然這裡加入spring-boot相應的web支援;

不懂的參考:

spring boot起步之Hello World【從零開始學Spring Boot】:

(2)在App.java類編寫HelloService2;

首先我們這裡有幾個包:com.kfit,org.kfit,我們這裡打算把App.java啟動類放到com.kfit中,根據Spring Boot掃描(根包到子包的原則),我們把HelloService2寫在Spring Boot可以掃描的位置,HelloService寫在Spring Boot無法掃描到的位置,那麼我們使用配置檔案bean的方式進行引入,具體程式碼如下:

com.kfit.service.HelloService2:

package com.kfit.service;

importorg.springframework.stereotype.Service;

@Service

public class HelloService2 {

       /**

        * 啟動的時候觀察控制檯是否列印此資訊;

        */

       public HelloService2() {

              System.out.println("HelloService2.HelloService2()"

);

              System.out.println("HelloService2.HelloService2()");

              System.out.println("HelloService2.HelloService2()");

       }

}

(3)在App.java類無法掃描的包下編寫HelloService;

注意這個類是寫在Spring Boot無法自動掃描的位置,正常啟動之後,如果引入HelloService的話肯定會報異常的,因為它根本沒有被注入成功,具體程式碼如下:

org.kfit.service.HelloService:

package org.kfit.service;

importorg.springframework.stereotype.Service;

@Service

public class HelloService {

       /**

        * 啟動的時候觀察控制檯是否列印此資訊;

        */

       public HelloService(){

              System.out.println("HelloService.HelloService()");

              System.out.println("org.kfit.service.HelloService.HelloService()");

              System.out.println("HelloService.HelloService()");

       }

}

(4)編寫application-bean.xml注入HelloService;

在src/main/resouces下編寫配置檔案application-bean.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

       <!-- 注入spring boot無法掃描到的bean. -->

       <bean id="helloService"class="org.kfit.service.HelloService"></bean>

</beans>

(5)編寫ConfigClass注入配置檔案application-bean.xml;

在com.kfit.config包下編寫類ConfigClass,這個確保能被Spring Boot可以掃描到,不然一切都付之東流了,具體程式碼如下:

com.kfit.config.ConfigClass:

package com.kfit.config;

importorg.springframework.context.annotation.Configuration;

importorg.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"})

public class ConfigClass {

}

(6)編寫App.java啟動類進行測試;

這個類Spring Boot正常的啟動程式碼:

com.kfit.App:

package com.kfit;

import org.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

/**

 *

 *

 * 大家也許會看到有些demo使用了3個註解:@Configuration;

 *

 *@EnableAutoConfiguration

 * @ComponentScan

 *

 *   其實:@SpringBootApplication申明讓spring boot自動給程式進行必要的配置,

 *

 * 等價於以預設屬性使用@Configuration

 *  @EnableAutoConfiguration和@ComponentScan

 *

 * 所以大家不要被一些文件誤導了,讓自己很迷茫了,希望本文章對您有所啟發;

 *

 * @authorAngel(QQ:412887952)

 * @version v.0.1

 */

@SpringBootApplication

public class App {

       public static voidmain(String[] args) {

              SpringApplication.run(App.class,args);

       }

}

在App.java 右鍵 Run As  Java Application觀察控制檯輸出可以看到:

HelloService2.HelloService2()

HelloService2.HelloService2()

HelloService2.HelloService2()

HelloService.HelloService()

org.kfit.service.HelloService.HelloService()

HelloService.HelloService()

說明我們引入編寫的程式碼生效了,如果你不相信的話,可以把ConfigClass的註解去掉,測試下,是不是列印資訊就少了HelloService的部分,是的話就對了。

(7)其它說明

       ImportResouce有兩種常用的引入方式:classpath和file,具體檢視如下的例子:

 classpath路徑:locations={"classpath:application-bean1.xml",

"classpath:application-bean2.xml"

}

 file路徑:

locations= {"file:d:/test/application-bean1.xml"};