天天看點

springboot幹貨——(八)springboot引入外部xml配置檔案前言正文

前言

在使用springboot的時候一般是極少需要添加配置檔案的(application.properties除外),但是在實際應用中也會存在不得不添加配置檔案的情況,例如內建其他架構或者需要配置一些中間件等,在這種情況下,我們就需要引入我們自定義的xml配置檔案了。

正文

1.建立springboot項目,項目結構如下:

springboot幹貨——(八)springboot引入外部xml配置檔案前言正文

2.建立一個不在springboot掃描範圍内的service

package com.gwd;
/** 
* @FileName TestService.java
* @Description:TODO
* @author JackHisen(gu.weidong)
* @version V1.0
* @createtime 2018年1月21日 下午7:47:10 
* 修改曆史:
* 時間           作者          版本        描述
*====================================================  
*
*/
public class TestService {
	public TestService() {
		System.out.println("這是一個不在springboot掃描範圍内的測試類");
	}
}
           

3.編寫application-bean.xml(resources目錄下)注入TestService

<?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="testService" class="com.gwd.TestService"></bean>
</beans>
           

4.編寫Config注入配置檔案application-bean.xml

locations下多檔案用逗号隔開,如

locations= {"classpath:a.xml,b.xml,c.xml..."}

package com.gwd.config;

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

/** 
* @FileName Config.java
* @Description:TODO
* @author JackHisen(gu.weidong)
* @version V1.0
* @createtime 2018年1月21日 下午7:53:22 
* 修改曆史:
* 時間           作者          版本        描述
*====================================================  
*
*/
@Configuration
@ImportResource(locations= {"classpath:application-bean.xml"})
public class Config {

}
           

@Configuration可了解為用spring的時候xml裡面的<beans>标簽

@Bean可了解為用spring的時候xml裡面的<bean>标簽

Spring Boot不是spring的加強版,是以@Configuration和@Bean同樣可以用在普通的spring項目中,而不是Spring Boot特有的,隻是在spring用的時候,注意加上掃包配置

<context:component-scan base-package="com.xxx.xxx" />,普通的spring項目好多注解都需要掃包,才有用,有時候自己注解用的挺6,但不起效果,就要注意這點。

Spring Boot則不需要,主要你保證你的啟動Spring Boot main入口,在這些類的上層包就行

5.運作啟動類

springboot幹貨——(八)springboot引入外部xml配置檔案前言正文

繼續閱讀