本文介绍了Spring Boot-从外部属性文件设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在application.properties
上设置了以下外部位置
I have an external location set on my application.properties
as below
spring.config.location=file:${catalina.home}/conf/app.properties
app.properties
具有作为timeOut=10000
的属性.还有许多其他属性.
app.properties
has a property as timeOut=10000
. There are many other properties as well.
我需要在会话中设置此属性,如下所示:
I need to set this property on my session something like this:
session.setMaxInactiveInterval(timeOut_Property);
如何实现?
添加控制器:
@Controller
public class StartController {
@Value("${spring.config.location.defaultTimeout}")
private int defaultTimeout;
@RequestMapping("login.do")
public String login(HttpServletRequest request, HttpSession session, Model model) {
session.setMaxInactiveInterval(defaultTimeout);
return null;
}
推荐答案
您的主应用程序类应如下所示:
Your Main Application class should look like this:
@SpringBootApplication
@PropertySource(name = "general-properties", value = { "classpath:path to your app.properties"})
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(NayapayApplication.class, args);
}
}
并将您的控制器更改为:
And change your controller to:
@Controller
public class StartController {
@Value("${timeOut}")
private int defaultTimeout;
@RequestMapping("login.do")
public String login(HttpServletRequest request, HttpSession session, Model model) {
session.setMaxInactiveInterval(defaultTimeout);
return null;
}
}
这篇关于Spring Boot-从外部属性文件设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!