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> listProp1 = new ArrayList>(); //接收prop1里面的属性值,List中的元素是Map  
    private List listProp2 = new ArrayList(); //接收prop2里面的属性值  
    private Map mapProps = new HashMap(); //接收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> getListProp1() {
		return listProp1;
	}

	public void setListProp1(List> listProp1) {
		this.listProp1 = listProp1;
	}

	public Map getMapProps() {
		return mapProps;
	}

	public void setMapProps(Map mapProps) {
		this.mapProps = mapProps;
	}

	public List getListProp2() {
		return listProp2;
	}

	public void setListProp2(List 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 文件


    4.0.0
    testSpringBoot
    testSpringBoot
    0.0.1-SNAPSHOT
    testSpringBoot
    jar

  
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.8.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.7
	

	
		
			org.springframework.boot
			spring-boot-starter-jdbc
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.1
		
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			mysql
			mysql-connector-java
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



项目结构图:

springboot 将yml文件内容注入javaBean_第1张图片



访问路径: http://localhost:8080/


源码下载地址: https://gitee.com/jason-fu/testSpringBoot

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



你可能感兴趣的:(springboot)