Description
Created a JAX-RS service and the implementation is attempting to inject a resource by type as seen in the example below. The ApplicationSettings bean is confirmed to be defined in the root Spring context as I can explicitly retrieve the bean from the ApplicationContext in the LoginResourceImpl. I am aware that the beans defined in the serviceBeans are created for each request and that Spring does not do the DI, but rather CXF code handles this.
The issue stems from how it performs a lookup for the injected resource. If no name attribute is defined on the @Resource, it should perform the lookup for that object by type.
The ResourceInjector class has a visitField method that will retrieve the name of the resource along with the class type. In our case the first resolveResource attempt will fail since there is nothing found with the name/type combination. The second case should attempt to perform a lookup with just the type of the resource to be injected.
When the BusApplicationContextResourceResolver attempts to resolve the value the 2nd try with a null name, the resolve method will ALWAYS return null instead of attempting to do a lookup using "context.getBean(resourceType)".
String name = getFieldNameForResource(res, field); Class<?> type = getResourceType(res, field); Object resource = resolveResource(name, type); if (resource == null && "".equals(res.name())) { resource = resolveResource(null, type); }
public <T> T resolve(String resourceName, Class<T> resourceType) { if (resourceName == null) { return null; } try { return resourceType.cast(context.getBean(resourceName, resourceType)); } catch (NoSuchBeanDefinitionException def) { //ignore } .... ....
<jaxrs:server id="jaxrsRestService" address="/"> <jaxrs:serviceBeans> <bean class="LoginResourceImpl" /> </jaxrs:serviceBeans>
public class LoginResource { @GET @Path("/login/captchakey") public String retrieveCaptchaKey(); }
public class LoginResourceImpl implements LoginResource { // This is the resource we are attempting to inject @Resource ApplicationSettings applicationSettings; public String retrieveCaptchaKey() { return applicationSettings.getSettting("captchakey"); } }
public <T> T resolve(String resourceName, Class<T> resourceType) { try { T resource = null; if (resourceName == null) { // Perofmrm the lookup of the resource using just the type resource = resourceType.cast(context.getBean(resourceType)); } else { resource = resourceType.cast(context.getBean(resourceName, resourceType)); } return resource; } catch (NoSuchBeanDefinitionException def) { //ignore } ... ...