亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot如何讀取xml配置bean(@ImportResource)

 更新時間:2022年01月18日 10:18:34   作者:上官天夜  
這篇文章主要介紹了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ā)工具軟件

e6c60ab06e185b7cd79c00459006772d.jpg

2、在項目中確保pom.xml文件已引用

【spring-boot-starter-web】jar包。

因為springBoot啟動的時候會自動去獲取項目中在resources文件錄目下的名為application.properties參數(shù)配置文件。

95071b6e1ffb4ebfd05d237095e0cb2f.jpg

3、在項目中的src/main/resource文件錄目下

創(chuàng)建application.properties參數(shù)配置文件。

2f90dbe28121dfc0652d15f694fa516c.jpg

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

8f2ac55454f4a502aba456493c1a7573.jpg

5、此時在項目啟動的時候

springBoot容器就會自動的將application.properties配置文件的配置信息自動的加入在spring容器中。

3ebd6711897e1b7d570c9bb40099d4ae.jpg

6、在需要使用的配置參數(shù)信息的類中

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

e65ccc51f8261a2678a5a257d1cadd40.jpg

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論