springboot 將yml文件內容注入javaBean

這是使用註解的方式分爲兩步:

1、編寫 yml 文件

2、編寫 javaBean 接收類

下面上代碼: 

application.yml

myProps: #自定義的屬性和值  
  simpleProp: simplePropValue  
  arrayProps: 1,2,3,4,5  
  listProp1:   #List中的元素是Map
    - name: abc  
      value: abcValue  
    - name: efg  
      value: efgValue  
  listProp2:  
    - config2Value1  
    - config2Vavlue2  
  mapProps:  
    key1: value1  
    key2: value2 

MyConfig.java

package testSpringBoot;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
// 不加這個註解的話, 使用@Autowired 就不能注入進去了
@ConfigurationProperties(prefix = "myProps")
// 配置文件中的前綴
public class MyConfig {
    private String simpleProp;  
    private String[] arrayProps;  
    private List<Map<String, String>> listProp1 = new ArrayList<Map<String, String>>(); //接收prop1裏面的屬性值,List中的元素是Map  
    private List<String> listProp2 = new ArrayList<String>(); //接收prop2裏面的屬性值  
    private Map<String, String> mapProps = new HashMap<String,String>(); //接收prop1裏面的屬性值

	public String getSimpleProp() {
		return simpleProp;
	}

	public void setSimpleProp(String simpleProp) {
		this.simpleProp = simpleProp;
	}

	public String[] getArrayProps() {
		return arrayProps;
	}

	public void setArrayProps(String[] arrayProps) {
		this.arrayProps = arrayProps;
	}

	public List<Map<String, String>> getListProp1() {
		return listProp1;
	}

	public void setListProp1(List<Map<String, String>> listProp1) {
		this.listProp1 = listProp1;
	}

	public Map<String, String> getMapProps() {
		return mapProps;
	}

	public void setMapProps(Map<String, String> mapProps) {
		this.mapProps = mapProps;
	}

	public List<String> getListProp2() {
		return listProp2;
	}

	public void setListProp2(List<String> listProp2) {
		this.listProp2 = listProp2;
	}

 
}

package testSpringBoot;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
      
}

package testSpringBoot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MainController {

	@Autowired
	  private MyConfig myConfig;
	 
      @RequestMapping("/")
      @ResponseBody
      Object  home() {
        return myConfig.getMapProps();
      }
}
application.properties文件內容

spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#端口號
server.port=8080

pom.xml 文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>testSpringBoot</groupId>
    <artifactId>testSpringBoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>testSpringBoot</name>
    <packaging>jar</packaging>

  
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.8.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.7</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

項目結構圖:




訪問路徑: http://localhost:8080/


源碼下載地址: https://gitee.com/jason-fu/testSpringBoot

參考:  http://makaidong.com/softidea/2229_2750650.html



發佈了70 篇原創文章 · 獲贊 71 · 訪問量 62萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章