Reputation: 4877
Is it possible to inject a value to a wicket page using spring?
With @Value
it's possible to inject values to a spring bean.
I know the @SpringBean
annotation, but this is only for beans.
My workaround is to wrap the value with a spring bean and then inject this with @SpringBean
to my wicket page. Is there a better way to do this?
Upvotes: 6
Views: 940
Reputation: 1269
We have solved this problem using getter & setter in our custom child of WebApplication
. This child is standard Spring bean and is configured in spring's configuration.
Otherwise you have to create some "config" bean.
Upvotes: 1
Reputation: 1373
You can write a Wicket resource loader to load spring values, and then those values will be resolved like regular wicket messages. If instead you need it within the body of the wicket class to do some business logic, that may be an opportunity to refactor that logic outside of the view layer.
Here's what the resource loader looks like:
public class SpringPropertiesResourceLoader
implements IStringResourceLoader
{
public SpringPropertiesResourceLoader()
{
}
@Override
public String loadStringResource(Class<?> clazz, String key, Locale locale, String style, String variation)
{
return loadStringResource(key);
}
@Override
public String loadStringResource(Component component, String key, Locale locale, String style, String variation)
{
return loadStringResource(key);
}
private String loadStringResource(String key)
{
try
{
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(WebPortalApplication.get().getServletContext());
ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory)applicationContext.getAutowireCapableBeanFactory();
String rv = beanFactory.resolveEmbeddedValue("${" + key + "}");
return rv;
}
catch (IllegalArgumentException iae)
{
// no property with the name - move along
return null;
}
}
}
Then add that to your application in init()
:
getResourceSettings().getStringResourceLoaders().add(new SpringPropertiesResourceLoader());
Upvotes: 0