0

I have an external location set on my application.properties as below

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

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

How can this be achieved?

Adding Controller:

@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;        
}
Reema
  • 587
  • 3
  • 12
  • 37
  • 1
    Possible duplicate of [How to access a value defined in the application.properties file in Spring Boot](https://stackoverflow.com/questions/30528255/how-to-access-a-value-defined-in-the-application-properties-file-in-spring-boot) – Emil Hotkowski May 25 '17 at 11:11
  • @Rjiuk That is probably when there is a single property defined. – Reema May 25 '17 at 11:18
  • Have you tried it ? I am pretty sure it works as well with one as with many properties files – Emil Hotkowski May 25 '17 at 11:19
  • It gives the below exception: nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'timeOut' in value "${timeOut}" Have tried with ${spring.config.location}, ${spring.config.location.timeOut} as well – Reema May 25 '17 at 11:26
  • can you give your main app code? where you import your properties for spring-boot? – Emil Hotkowski May 25 '17 at 11:28
  • 1
    Looks like you haven't added your properties file which has timeOut value. – Emil Hotkowski May 25 '17 at 11:37
  • 1
    Import your custom property file in main class like @PropertySource(name = "general-properties", value = { "classpath:path to your app.properties"}) public class MainApplication { } – Afridi May 25 '17 at 11:42
  • @Rjiuk Added controller code. The properties file is in place – Reema May 25 '17 at 11:51
  • @Rjiuk I was deploying using Spring Boot app and not tomcat - that's why it couldnt find tomcat directory. My bad! Plus the PropertySource as mentioned by Afridi – Reema May 25 '17 at 14:51

2 Answers2

0

You can annotate the variable with this property in the class as:

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

Use this variable to set session inactive interval as:

session.setMaxInactiveInterval(timeOut);
kk.
  • 3,747
  • 12
  • 36
  • 67
0

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;        
    }
}
Afridi
  • 6,753
  • 2
  • 18
  • 27