Return-Path: X-Original-To: apmail-cxf-issues-archive@www.apache.org Delivered-To: apmail-cxf-issues-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 00B17D443 for ; Fri, 27 Jul 2012 15:20:37 +0000 (UTC) Received: (qmail 2863 invoked by uid 500); 27 Jul 2012 15:20:36 -0000 Delivered-To: apmail-cxf-issues-archive@cxf.apache.org Received: (qmail 2812 invoked by uid 500); 27 Jul 2012 15:20:36 -0000 Mailing-List: contact issues-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list issues@cxf.apache.org Received: (qmail 2736 invoked by uid 99); 27 Jul 2012 15:20:36 -0000 Received: from issues-vm.apache.org (HELO issues-vm) (140.211.11.160) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 27 Jul 2012 15:20:36 +0000 Received: from isssues-vm.apache.org (localhost [127.0.0.1]) by issues-vm (Postfix) with ESMTP id 990D4142866 for ; Fri, 27 Jul 2012 15:20:35 +0000 (UTC) Date: Fri, 27 Jul 2012 15:20:35 +0000 (UTC) From: "Matas Veitas (JIRA)" To: issues@cxf.apache.org Message-ID: <175285325.110723.1343402435630.JavaMail.jiratomcat@issues-vm> In-Reply-To: <2027070382.109296.1343361514527.JavaMail.jiratomcat@issues-vm> Subject: [jira] [Commented] (CXF-4444) Injecting object with @Resource with no specified name attribute is not working MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/CXF-4444?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13423923#comment-13423923 ] Matas Veitas commented on CXF-4444: ----------------------------------- A work around for this is to ensure that all injected items have a name specified > 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 resolve(String resourceName, Class resourceType) { > if (resourceName == null) { > return null; > } > try { > return resourceType.cast(context.getBean(resourceName, resourceType)); > } catch (NoSuchBeanDefinitionException def) { > //ignore > } > .... > .... > {code} > {code:xml} > > > > > {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 resolve(String resourceName, Class 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