最近有时间 回顾一下2年前用这个Springboot的点点滴滴 Springboot 读取资源文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32048567/article/details/85697398

在实际的项目开发之中资源文件一定不可或缺失,因为所有的提示文字信息都要求在资源文件之中进行定义,而且资源文件是实现国际化技术的主要手段。如果要想在Springboot 里面进行资源文件的配置只需要做一些简单的application.yml 配置即可 而且所有注入的资源文件都可以像最初的Spring处理那样直接使用MessageSource 进行读取

   1.0 为了统一管理资源文件 在src/main/resources 目录之中建立有一个il8n的存储目录 

   2.0 在 src/main/resources/il8n  目录之中建立有两个资源文件 

           建立 Messages.properties

welcome.url=www.fabu0746.com

welcome.msg=zw

          建立 Pages.properties

member.add.page=/pages/back/admin/member/member_add.jsp

member.add.action=/pages/back/admin/member/member_add.action

把默认配置文件 .properties 改为.yml 格式的配置 

  1.   YAML 文件 

        这是一种结构化的数据文件,其在很多的地方上都使用过,列如 Apache Storm 开发组件上进行配置的时候使用的就是yml 配置文件 该配置文件的全称 Yet Another Markup Languange 然是一种标记语言 

修改 application.yml 

spring:   #表示该配置直接为Spring 容器负责处理

  messages:   #表示进行资源配置

    basename: il8n/Messages,il8n/Pages #资源文件的名称

server:     #修改访问端口

  port: 80
  1. 当执行完以上的配置之后会自动为用户创建MessageSource 对象,那么用户在使用的时候直接注入此对象即可 。考虑真实开发的标准 建议创建一个父类的控制器的抽象类 而后在此抽象类之中进行资源读取读取类的配置  AbstractBaseController 

package com.hema.demo2.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.MessageSource;

import java.util.Locale;

/**

* 描述:

* 抽象类 进行资源的读取

*

* @author zw

* @create 2018-11-28 9:12 PM

*/

public abstract class AbstractBaseController {


    @Autowired

    private MessageSource  messageSource;


    public String getMessage(String key,String ... args){


        System.out.println(Locale.getDefault());


        return this.messageSource.getMessage(key,args, Locale.getDefault());

    }

}


在控制器的子类之中读取以上的配置信息

package com.hema.demo2.controller;


import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;


/**

* 描述:

* 子类 读取抽象类配置信息

*

* @author zw

* @create 2018-11-28 9:16 PM

*/

@RestController

public class MessageController  extends AbstractBaseController {


    /****

     * 进行资源的读取

     * @return

     */

    @RequestMapping(value = "/msg",method = RequestMethod.GET)


    public String msg(String mid){


        System.out.println("[****访问地址*********]"+super.getMessage("member.add.action"));

        return  super.getMessage("welcome.msg",mid);

    }

}

编写一个junit 测试类  来测试以上的控制器程序是否正确
 

package com.hema.demo2;


import com.hema.demo2.controller.MessageController;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

import org.springframework.test.context.web.WebAppConfiguration;


/**

* 描述:

* 测试类

*

* @author zw

* @create 2018-11-29 9:16 PM

*/

@SpringBootTest

@RunWith(SpringRunner.class)


public class TestMessageController {


    @Autowired

    private MessageController messageController;


    @Test

    public  void  testMsg(){

       System.out.println(this.messageController.msg("zwjava"));

   }

}

通过springboot 执行可以清楚的发现 在Springboot 里面所有的对象信息注入配置操作,都直接通过一行简单的字符串实现了,而且最终也能保持与之前同样的运行效果。

喜欢的麻烦在  githua 上点个小星星

githua  代码如下   https://github.com/zhouwei520/Springboot/tree/master/demo2

猜你喜欢

转载自blog.csdn.net/qq_32048567/article/details/85697398