[二十一]SpringBoot 之 導入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)新建一個工程;

       參照我前面的例子

(2)在App.java類包路徑下編寫TestService;

      首先我們這里有幾個包:me.shijunjie,com.test,我們這里打算把App.java啟動類放到me.shijunjie中,根據Spring Boot掃描(根包到子包的原則),我們把TestService寫在Spring Boot可以掃描的位置,    TestService2寫在Spring Boot無法掃描到的位置,那么我們使用配置文件bean的方式進行引入,具體代碼如下:

package me.shijunjie.service;

import org.springframework.stereotype.Service;

@Service
public class TestService {

    public TestService() {
        System.out.println("me.shijunjie.service");
        System.out.println("TestService");
    }

}

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

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

package com.test.service;

import org.springframework.stereotype.Service;

@Service
public class TestService2 {

    public TestService2() {
        System.out.println("com.test.service");
        System.out.println("TestService2");
    }
    
}

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

       在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" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
            
       <bean id="testService2" class="com.test.service.TestService2"></bean>
</beans>

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

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

package me.shijunjie.config;

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

@Configuration
@ImportResource(locations={"classpath:application-bean.xml"})
public class ConfigClass {
    
}

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

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

package me.shijunjie;

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

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
       SpringApplication.run(App.class, args);
    }
}

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

(7)其它說明

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

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

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM