【问题标题】:Spring Boot - set value from an external properties fileSpring Boot - 从外部属性文件设置值
【发布时间】:2017-10-26 00:27:19
【问题描述】:

我在application.properties 上设置了如下外部位置

spring.config.location=file:${catalina.home}/conf/app.properties

app.properties 的属性为timeOut=10000。还有许多其他属性。

我需要在我的会话中设置这个属性,如下所示:

 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;        
}

【问题讨论】:

  • @Rjiuk 那可能是定义了一个属性的时候。
  • 你试过了吗?我很确定它适用于一个和许多属性文件一样好
  • 看起来您还没有添加具有超时值的属性文件。
  • 在 @PropertySource(name = "general-properties", value = { "classpath:path to your app.properties"}) 这样的主类中导入您的自定义属性文件 public class MainApplication { }跨度>

标签: java spring spring-mvc model-view-controller spring-boot


【解决方案1】:

您的主应用程序类应如下所示:

@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);
    }
}

并将您的控制器更改为:

@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;        
    }
}

【讨论】:

    【解决方案2】:

    您可以在类中使用此属性将变量注释为:

    @Value("${timeOut}")
    private String timeOut;
    

    使用此变量将会话不活动间隔设置为:

    session.setMaxInactiveInterval(timeOut);
    

    【讨论】:

      猜你喜欢
      • 2020-07-30
      • 2017-08-18
      • 2018-02-21
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 2015-05-14
      • 1970-01-01
      相关资源
      最近更新 更多