java - Spring Property Injection with Play2 Framework -


I like many features of Play Framework 2 (I'm using it with Java), but the dependency injection fan As a matter of fact, I especially love the spring and in particular, injecting the configuration into objects using @Value annotations.

Therefore, I would love to know how the value of the property, using the built-in property resolution mechanism of a frequency variable play something like this: @Component public class SpringBeanWithinAPlay2Application { @Value ("$ {application.timeout: 10}") Private int timeout; }

Anyone have any clues? Many thanks in advance.

I had the same problem even a while back and this was the way to do this:

First of all, when you promote your spring application context (I use annotation based configuration, but it should work for XML based), you have to add a custom , Which enables the way in which other than the new way to solve spring properties. Received some:

  public static void start () {ctx = new Antateshn Config Aplifikeshn Kansteks (); Ctx.getEnvironment (). GetPropertySources (). AddFirst (new PlayFrameworkPropertySource ()); Ctx.scan ("somepackage"); Ctx.refresh (); }   

Custom class PlayFrameworkPropertySource does such magic:

  public class PlayFrameworkPropertySource PropertySource & lt; Object & gt; {Public PlayFrameworkPropertySource () {Super ("Resolution Mechanism of Play Framework Properties"); } @OverWide public object getProperty (string proprietary) {// or ConfigFactory.load (). GetString (propertyName), as you like ... Configuration Configuration.root (). GetString (propertyName); }}   

To do all this work you only need to do one more thing: Clearly declare any kind of bean which you are using Be:

  @ Bean Public Static Property Source Placeholder Configurator Properties Placeholder Configurator () {New Property Site Placeholder Configurator (); }  Important note: this bean should be  static  , because it is one and it is before any other regular @ bean Should be loaded.  

It works like a charm for me, hope it is useful for someone else!

Cheers, Jonathan

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -