SpringBoot如何讀取xml配置bean(@ImportResource)
讀取xml配置bean(@ImportResource)
1、應用場景
舊框架SSM項目移行到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、應用xml中的bean對象
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、測試
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ā)中會經(jīng)常用到的框架,那么在實際項目中項目配置了springBoot框架,應該如何在項目中讀取配置文件中的參數(shù)呢?
1、打開eclipse開發(fā)工具軟件
2、在項目中確保pom.xml文件已引用
【spring-boot-starter-web】jar包。
因為springBoot啟動的時候會自動去獲取項目中在resources文件錄目下的名為application.properties參數(shù)配置文件。
3、在項目中的src/main/resource文件錄目下
創(chuàng)建application.properties參數(shù)配置文件。
4、在application.properties配置文件中添加對應的參數(shù)
5、此時在項目啟動的時候
springBoot容器就會自動的將application.properties配置文件的配置信息自動的加入在spring容器中。
6、在需要使用的配置參數(shù)信息的類中
只要通過spring注解@Value("${xxx}")的方法注入到全局變量中即可讀取配置文件中的參數(shù)。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
java自定義任務類定時執(zhí)行任務示例 callable和future接口使用方法
Callable是類似于Runnable的接口,實現(xiàn)Callable接口的類和實現(xiàn)Runnable的類都是可被其它線程執(zhí)行的任務2014-01-01Java通過PowerMockito和Mokito進行單元測試的實現(xiàn)
PowerMockito和Mockito都是Java語言中的測試框架,用于進行單元測試和集成測試,本文就來詳細的介紹一下通過PowerMockito和Mokito進行單元測試,感興趣的可以了解一下2023-08-08Java abstract class 與 interface對比
這篇文章主要介紹了 Java abstract class 與 interface對比的相關資料,需要的朋友可以參考下2016-12-12SpringBoot自定義Starter實現(xiàn)流程詳解
SpringBoot中的starter是一種非常重要的機制,能夠拋棄以前繁雜的配置,將其統(tǒng)一集成進starter,應用者只需要在maven中引入starter依賴,SpringBoot就能自動掃描到要加載的信息并啟動相應的默認配置。starter讓我們擺脫了各種依賴庫的處理,需要配置各種信息的困擾2022-09-09實戰(zhàn)分布式醫(yī)療掛號系統(tǒng)之設置微服務搭建醫(yī)院模塊
這篇文章主要為大家介紹了實戰(zhàn)分布式醫(yī)療掛號系統(tǒng)之搭建醫(yī)院設置微服務模塊,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04