Resolving resources

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
Report Content as Inappropriate

Resolving resources

Hello everyone,

Another piece of the puzzle is to resolve and inject resources. We've already seen `@InjectResources` but we were missing `ResourcesInjector`, a simple interface that can take an Object and perform resource injection on it.

But in order to inject resources one must first resolve such resources, thus the `ResourceResolver` interface is born. this interface is very similar to `MessageSource`, so I expect any discussion on one interface to be tied to the other. While `MessageSource` is used to resolve just messages (or literal only resources if you look for commonalities), `ResourceResolver` may return non-literal values such as Color, Font, etc. The most basic method is

   <T> T resolveResource(String key)

However there are additional signatures that can be used to get the right type, not just Object, for example

   <T> T resolveResource(String, Key, Class<T> type)

As you can appreciate, the first method blindly casts the result to T, while the second can use an explicit type argument to find a suitable Converter<?,T>.