Matas Veitas created CXF-4444:
---------------------------------
Summary: Injecting object with @Resource with no specified name
attribute is not working
Key: CXF-4444
URL: https://issues.apache.org/jira/browse/CXF-4444
Project: CXF
Issue Type: Bug
Components: Core, JAX-RS
Affects Versions: 2.6.1
Reporter: Matas Veitas
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)".
{code:title=ResourceInjector.java}
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);
}
{code}
{code:title=BusApplicationContextResourceResolver.java}
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
}
....
....
{code}
{code:xml}
<jaxrs:server id="jaxrsRestService" address="/">
<jaxrs:serviceBeans>
<bean class="LoginResourceImpl" />
</jaxrs:serviceBeans>
{code}
{code:title=LoginResource.java}
public class LoginResource {
@GET
@Path("/login/captchakey")
public String retrieveCaptchaKey();
}
{code}
{code:title=LoginResourceImpl.java}
public class LoginResourceImpl implements LoginResource {
// This is the resource we are attempting to inject
@Resource
ApplicationSettings applicationSettings;
public String retrieveCaptchaKey() {
return applicationSettings.getSettting("captchakey");
}
}
{code}
{code:title=Possible fix in BusApplicationContextResourceResolver.java}
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
}
...
...
{code}
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira