(Quick Reference)

11.4 Automatically Injected Resources - Reference Documentation

Authors: Andres Almiray

Version: 1.2.0

11.4 Automatically Injected Resources

Resources may be automatically injected to any instance created using the application's facilities (by calling newInstance() on the application instance or any Griffon artifact instance). Injection points must be annotated with @griffon.core.resources.InjectedResource which can only be set on properties (Groovy) or fields (Java and Groovy). @InjectedResource is a perfect companion to models as the following example shows

resources.properties

sample.SampleModel.griffonLogo = /griffon-logo-48x48.png
logo = /griffon-logo-{0}x{0}.png

SampleModel.groovy

package sample

import griffon.core.resources.InjectedResource import javax.swing.Icon

class SampleModel { @InjectedResource Icon griffonLogo @InjectedResource(key='logo', args=['16']) Icon smallGriffonLogo @InjectedResource(key='logo', args=['64']) Icon largeGriffonLogo }

@InjectedResource assumes a naming convention in order to determine the resource key to use. These are the rules applied by the default ResourcesInjector:

  • If a value is specified for the key argument then use it as is
  • otherwise construct a key based in the field name prefixed with the full qualified class name of the field's owner

You may also specify a default value if the resource definition is not found, however be aware that this value must be set as a String thus guaranteeing a type conversion.