I have a ServletContextListener in my web application. The following works...
public class AppServletContextListener
implements ServletContextListener, HttpSessionAttributeListener, HttpSessionListener {
public void contextInitialized(ServletContextEvent arg0) {
String repManInitScript = System.getProperty("REP_MAN_INIT_SCRIPT");
System.err.println(repManInitScript );
}
}
But usign the @Value annotation doesn't....
public class AppServletContextListener
implements ServletContextListener, HttpSessionAttributeListener, HttpSessionListener {
@Value("${REP_MAN_INIT_SCRIPT}")
private String repManInitScript;
public void contextInitialized(ServletContextEvent arg0) {
repManInitScript = System.getProperty("REP_MAN_INIT_SCRIPT");
System.err.println(repManInitScript );
}
}
Is there a way to grab the system property using the @Value annotation?
Thanks
Yes you can but you have to grab it from the system properties like this:
@Value("#{systemProperties['REP_MAN_INIT_SCRIPT']}")
private String repManInitScript;
I assume you already have this property in your system properties.
And of course @Value annotation only works in spring beans. If you try to use it directly in ServletContextListener it won't work.
This ${REP_MAN_INIT_SCRIPT} isn't working because spring looks for the property in your configured properties if you have any of course.
So if you want this to work on initialization you can implement InitializingBean in your spring bean and override afterPropertiesSet method. Then the value will be available.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With