SpringBoot第二篇:利用application.properties配置一些项目中固定的自定义参数

        有时候,我们项目中需要一些指定的参数,这种参数并不会随着项目开发和运行而改变,也避免存放在数据库因遗失而导致项目无法正常运行,我们就可以把这样的数据写在application.properties文件中,这样就不怕参数丢失或者被后台人员所篡改。

1. 首先在application.properties中定义我们的参数。参数格式形式用xxx.xx来表示,如下图:

2.然后在你使用的地方用@Value("xxx.xx")注解的形式读取配置文件中的值,代码所示如下:

package com.ask.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 负责处理前端的请求
 */
@Controller
public class IndexController {

    @Value("${csnd.name}")
    private String nameOfCSDN;

    @Value("${csnd.url}")
    private String urlOfCSDN;

    //定义请求路径为:localhost:8888/hello,下面是对应这个路径的处理方法。
    //RequestParam用于接收页面传递过来的参数,例如localhost:8888/hello?name=小明
    //model是Spring内置的对象,用来处理视图的
    @GetMapping("/hello")
    public String hello(@RequestParam(name="name") String name, Model model){
        //将浏览器的请求参数中的name加入到model中,这样就能在模版的html中获取到
        model.addAttribute("name",name);
        model.addAttribute("csName",nameOfCSDN);
        model.addAttribute("url",urlOfCSDN);
        //返回的index是html模版的名字
        return "index";
    }
}

3.就可以用在方法中,这里我用来展示了,效果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值