SpringBoot如何讀取xml配置bean(@ImportResource)
讀取xml配置bean(@ImportResource)
1、應(yīng)用場(chǎng)景
舊框架SSM項(xiàng)目移行到SpringBoot中,xml配置文件很齊全,就可以省去配置的麻煩,直接讀取舊xml文件
2、spring-common.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:p="http://www.springframework.org/schema/p" ? ? xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">? ?? ? ? <bean id="msgService" class="com.zemel.micorboot.service.impl.MessageServiceImpl"></bean>? ?? ? ? </beans>
3、SpringBoot讀取xml
package com.zemel.micorboot;?
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
?
/**
?* Hello world!
?*
?*/
@SpringBootApplication
@ImportResource(locations={"classpath:xml/spring-common.xml"})
public class App?
{
? ? public static void main( String[] args )
? ? {
? ? ? ? SpringApplication.run(App.class, args);
? ? }
}4、應(yīng)用xml中的bean對(duì)象
package com.zemel.micorboot.controller;?
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;?
import com.zemel.micorboot.base.AbstractBaseController;
import com.zemel.micorboot.service.MessageService;
?
@RestController
public class MessageController extends AbstractBaseController {
?? ?
?? ?@Autowired
?? ?private MessageService messageService;?? ?
?
?? ?@GetMapping("/echo")
?? ?public String echo(String mid){
?? ??? ?System.out.println("[***訪問地址***]"+super.getMessage("member.add.action"));
?? ??? ?return super.getMessage("welcome.msg", mid);
?? ?}
?? ?
?? ?public String msg(){
?? ??? ?return (messageService.getMessage());
?? ?}?? ?
}5、Service類
package com.zemel.micorboot.service;
public interface MessageService {
String getMessage();
}package com.zemel.micorboot.service.impl;
import com.zemel.micorboot.service.MessageService;
public class MessageServiceImpl implements MessageService{
@Override
public String getMessage() {
return "my message...";
}
}6、測(cè)試
package com.zemel.micorboot.controller;?
import javax.annotation.Resource;?
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;?
import com.zemel.micorboot.App;
?
@SpringBootTest(classes=App.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class MessageControllerTest {?
?? ?@Resource
?? ?private MessageController mc;
?? ?
?? ?@Test
?? ?public void testEcho() {?? ??? ?
?? ??? ?System.out.println(this.mc.echo("mldnjava"));?? ??? ?
?? ?}
?? ?
?? ?@Test
?? ?public void testMsg(){
?? ??? ?System.out.println(this.mc.msg());
?? ?}?
}讀取配置文件中的參數(shù)
springBoot是java開發(fā)中會(huì)經(jīng)常用到的框架,那么在實(shí)際項(xiàng)目中項(xiàng)目配置了springBoot框架,應(yīng)該如何在項(xiàng)目中讀取配置文件中的參數(shù)呢?
1、打開eclipse開發(fā)工具軟件

2、在項(xiàng)目中確保pom.xml文件已引用
【spring-boot-starter-web】jar包。
因?yàn)閟pringBoot啟動(dòng)的時(shí)候會(huì)自動(dòng)去獲取項(xiàng)目中在resources文件錄目下的名為application.properties參數(shù)配置文件。

3、在項(xiàng)目中的src/main/resource文件錄目下
創(chuàng)建application.properties參數(shù)配置文件。

4、在application.properties配置文件中添加對(duì)應(yīng)的參數(shù)

5、此時(shí)在項(xiàng)目啟動(dòng)的時(shí)候
springBoot容器就會(huì)自動(dòng)的將application.properties配置文件的配置信息自動(dòng)的加入在spring容器中。

6、在需要使用的配置參數(shù)信息的類中
只要通過spring注解@Value("${xxx}")的方法注入到全局變量中即可讀取配置文件中的參數(shù)。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java自定義任務(wù)類定時(shí)執(zhí)行任務(wù)示例 callable和future接口使用方法
Callable是類似于Runnable的接口,實(shí)現(xiàn)Callable接口的類和實(shí)現(xiàn)Runnable的類都是可被其它線程執(zhí)行的任務(wù)2014-01-01
Java通過PowerMockito和Mokito進(jìn)行單元測(cè)試的實(shí)現(xiàn)
PowerMockito和Mockito都是Java語(yǔ)言中的測(cè)試框架,用于進(jìn)行單元測(cè)試和集成測(cè)試,本文就來詳細(xì)的介紹一下通過PowerMockito和Mokito進(jìn)行單元測(cè)試,感興趣的可以了解一下2023-08-08
Java實(shí)現(xiàn)線性表的順序存儲(chǔ)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)線性表的順序存儲(chǔ),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
詳解Java中LinkedStack鏈棧的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java中LinkedStack鏈棧的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下2022-11-11
Java abstract class 與 interface對(duì)比
這篇文章主要介紹了 Java abstract class 與 interface對(duì)比的相關(guān)資料,需要的朋友可以參考下2016-12-12
SpringBoot自定義Starter實(shí)現(xiàn)流程詳解
SpringBoot中的starter是一種非常重要的機(jī)制,能夠拋棄以前繁雜的配置,將其統(tǒng)一集成進(jìn)starter,應(yīng)用者只需要在maven中引入starter依賴,SpringBoot就能自動(dòng)掃描到要加載的信息并啟動(dòng)相應(yīng)的默認(rèn)配置。starter讓我們擺脫了各種依賴庫(kù)的處理,需要配置各種信息的困擾2022-09-09
實(shí)戰(zhàn)分布式醫(yī)療掛號(hào)系統(tǒng)之設(shè)置微服務(wù)搭建醫(yī)院模塊
這篇文章主要為大家介紹了實(shí)戰(zhàn)分布式醫(yī)療掛號(hào)系統(tǒng)之搭建醫(yī)院設(shè)置微服務(wù)模塊,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04

