SpringBoot如何读取资源文件

2025-10-03 23:48:33

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

为了统一管理资源文件,在“src/main/resources”目录之中建立一个i18n的存储目录.

SpringBoot如何读取资源文件

2、在"src/main/resources"目录之中建立有两个资源文件:Messages.properties。

welcome.url=www.baidu.comwelcome.msg=欢迎{0}光临!

SpringBoot如何读取资源文件

3、建立Pages.propertis配置文件:

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

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

SpringBoot如何读取资源文件

4、修改application.yml配置文件:

server:

  port: 80

spring:

  messages:

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

  

SpringBoot如何读取资源文件

5、当执行完以上的配置之后会自动为用户创建MessageSource对象,那么用户在使用的时候直接注入此对象即可。

考虑到实际开发的标准性,所以现在建议创建一个父的控制器抽象类:AbstractBaseController,而后在此抽象类之中进行资源读取类的配置。

package com.gwolf.util.controller;

import org.springframework.context.MessageSource;

import javax.annotation.Resource;

import java.util.Locale;

public abstract class AbstractBaseController {

    @Resource

    private MessageSource messageSource;//自动注入此资源对象

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

        return this.messageSource.getMessage(key,args, 

                        Locale.getDefault());

    }

}

SpringBoot如何读取资源文件

6、在控制器的子类之中读取配置信息:

package com.gwolf.controller;

import ch.qos.logback.core.net.SyslogOutputStream;

import com.gwolf.util.controller.AbstractBaseController;

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

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

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

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

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@RestController

public class MessageController extends AbstractBaseController{

    

    @RequestMapping(value="/echo",

            method = RequestMethod.GET)

    public String echo(String mid) {

        System.out.println("访问:" + super.getMessage("member.add.action"));

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

    }

}

SpringBoot如何读取资源文件

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

package com.gwolf.test;

import com.gwolf.StartSpringBootMain;

import com.gwolf.controller.HelloController;

import com.gwolf.controller.MessageController;

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 javax.annotation.Resource;

@SpringBootTest(classes = StartSpringBootMain.class)

@RunWith(SpringJUnit4ClassRunner.class)

@WebAppConfiguration

public class TestMessageController {

    @Resource

    private MessageController messageController;

    

    @Test

    public void testEcho() {

        System.out.print(this.messageController.echo("www.baidu.com"));

    }

}

SpringBoot如何读取资源文件

8、通过SpringBoot执行可以清楚的发现,在SpringBoot里面所有对象信息的注入配置操作,都直接通过一行简单的字符串实现了,而且最终也能够保持与之前同样运行效果。这样SpringBoot就完成了读取资源文件的操作。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢