Narada Sage opened SPR-6651 and commented

Hi,

As mentioned on forum post accessing system properties in SpEL programmatically does not work for me. The manual only refers to use of system properties in xml which I haven't tried. However shouldn't this work programmatically as well?

If you need further details please let me know.

Thanks.


Affects: 3.0 GA

Reference URL: http://forum.springsource.org/showthread.php?t=82259

Attachments: - SystemPropertiesAccessor.java (2.10 kB) - SystemPropertiesAccessorUnitTest.java (2.65 kB)

2 votes, 4 watchers

Comment From: spring-projects-issues

Juergen Hoeller commented

"systemProperties" is the name of an implicitly registered bean in a BeanFactory, which is why you may dereference it using the standard map syntax in SpEL. However, the actual "systemProperties" bean is only exposed to the BeanExpressionResolver in an actual BeanFactory. When using SpelExpressionParser directly, you'd have to pass a custom EvaluationContext into the getValue method, with that EvaluationContext configured to know about a "systemProperties" bean.

For an example of how this can be accomplished, have a look at "org.springframework.context.expression.StandardBeanExpressionResolver"; admittedly, it's not quite trivial. I suppose we could provide an easier way to evaluate an expression according to the same rules that the BeanFactory uses, e.g. a public "evaluate" method on a factory API than can be injected into application components.

Juergen

Comment From: spring-projects-issues

Narada Sage commented

Hi,

Thanks for your feedback.

From my point of view my thinking was simply that system properties are a jdk provision and not tied to spring or bean factories in any way. If I wanted to use spel in a standalone fashion completely separately from spring then it would be nice to able to dereference system properties using special variables. Of course in the mean time I can set that on the context as you said.

The implementation I leave to you.

Thanks.

Comment From: spring-projects-issues

Jay Erb commented

This seems to be preventing me from using the Cacheable annotation with a condition based on a system property. I've got the same set of services configured in a web application and a data-load application whose life only exists for a single data load. I want caching to be used in the data-load application but not the web application and I figured one way to do that while keeping the same application context and source code is to use a system property.

Comment From: spring-projects-issues

Giovanni Dall'Oglio Risso commented

I get the same problem, and resolved writing a PropertyAccessor and instructing the StandardEvaluationContext. Please: find attached.

Now, the "systemProperties" are registered in the BeanFactory as a singleton bean (and the evaluation is made via BeanExpressionContextAccessor).

This result in an inhability to read the system properties outside the evaluation in "beanfactory neighborhood".

The SpEl could include this class in the default property accessors (class StandardEvaluationContext, method initializePropertyAccessors).

PS: feel free to include this classes; if you want I can make a pull request

Comment From: jhoeller

Superseded by #27721 with a wider-ranging scope. At this point still left up to custom arrangements, in any case.