Uploaded image for project: 'CXF'
  1. CXF
  2. CXF-4444

Injecting object with @Resource with no specified name attribute is not working



    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.6.1
    • 2.5.5, 2.6.2, 2.7
    • Core, JAX-RS
    • None
    • Novice


      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) {
      <jaxrs:server id="jaxrsRestService" address="/">
            <bean class="LoginResourceImpl" />
      public class LoginResource {
         public String retrieveCaptchaKey();
      public class LoginResourceImpl implements LoginResource {
         // This is the resource we are attempting to inject
         ApplicationSettings applicationSettings; 
         public String retrieveCaptchaKey() {
            return applicationSettings.getSettting("captchakey");
      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) {




            sergey_beryozkin Sergey Beryozkin
            mveitas Matas Veitas
            0 Vote for this issue
            2 Start watching this issue