Johann Burkard opened SPR-11745 and commented

Currently

Environment env
...
env['foo']

doesn't work in Groovy. Which is a bit odd since Environment implements a getProperty method. Now I have to write

env.getProperty('foo')

which is not very Groovy.


Affects: 4.0.3

Comment From: spring-projects-issues

Juergen Hoeller commented

This is a good point, and applicable to other Spring interfaces as well...

However, the only way to make Groovy detect a getProperty implementation is to implement the GroovyObject interface - which implies a runtime dependency on the Groovy jar - and that pill we can't swallow for such core Spring API types as Environment.

We'll research what we can do between the Spring and Groovy world there, possibly asking the Groovy guys to give us a convention-based way to expose a getProperty implementation to them.

Juergen

Comment From: spring-projects-issues

Johann Burkard commented

I think you can do many things to integrate Environment better with Groovy. For instance, it could implement java.util.Map and I think that would already enable the env['foo'] syntax. Or you might implement asType(Class) with something like this

public <T> T asType(Class<T> type) {
 return type == Map.class ? new Map<String, String>() {
  public String get(String key) {
   return Environment.this.getProperty(key);
  }
  public void clear() {
   throw new UnsupportedOperationException();
  }
 } : null;
}

People could then write (env as Map)['foo'] in Groovy.

(Unverified code ;) )