(六)、SpringBoot 自定义配置属性,可以在application.properties中修改

可以前往第一篇博客查看目录结构 --> 这里

一、自定义配置 (一些动态的数据:登录页面地址 、接收数据格式等等)

二、在core模块下创建properties目录 : com.zeke.core.properties 。在properties目录下创建SecurityProperties主配置文件,BrowserProperties浏览器配置文件,LoginType枚举类

LoginType : 

public enum LoginType {

    REDIRECT,

    JSON
}

BrowserProperties :

public class BrowserProperties {

    private String loginPage = "/zeke-login.html";

    private LoginType loginType = LoginType.JSON;

    public String getLoginPage() {
        return loginPage;
    }

    public void setLoginPage(String loginPage) {
        this.loginPage = loginPage;
    }

    public LoginType getLoginType() {
        return loginType;
    }

    public void setLoginType(LoginType loginType) {
        this.loginType = loginType;
    }
}

SecurityProperties : 

@ConfigurationProperties(prefix = "zeke.security")

public class SecurityProperties { private BrowserProperties browser = new BrowserProperties(); public BrowserProperties getBrowser() { return browser; } public void setBrowser(BrowserProperties browser) { this.browser = browser; }}

三、在core模块 com.zeke.core 目录下添加SecurityCoreConfig类,使得SecurityProperties为配置类

@Configuration
@EnableConfigurationProperties(SecurityProperties.class)
public class SecurityCoreConfig {


}

四、这时候我们就可以在demo模块下的application.properties中配置我们需要的属性,例:(覆盖java文件中的loginPage属性)

zeke.security.browser.loginPage = /zeke-login.html

五、使用的时候@Autowired SecurityProperties securityProperties即可,示例:

    @Autowired
    private SecurityProperties securityProperties;
//使用
securityProperties.getBrowser().getLoginType()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值