Repository: cxf Updated Branches: refs/heads/3.1.x-fixes b673d0d79 -> acbab5dea
[CXF-6694] Getting SpringResourcefactory recognize contructor-autowired beans, patch from Vladimir Kulev applied with thanks Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/acbab5de Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/acbab5de Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/acbab5de Branch: refs/heads/3.1.x-fixes Commit: acbab5dea87bf1747da1085c37c5f13b50944d42 Parents: b673d0d Author: Sergey Beryozkin <[email protected]> Authored: Sun Dec 6 18:13:25 2015 +0000 Committer: Sergey Beryozkin <[email protected]> Committed: Sun Dec 6 18:14:40 2015 +0000 ---------------------------------------------------------------------- .../cxf/jaxrs/spring/SpringResourceFactory.java | 29 ++++++++++++-------- .../jaxrs/spring/SpringResourceFactoryTest.java | 17 ++++++++---- .../org/apache/cxf/jaxrs/spring/servers2.xml | 4 ++- 3 files changed, 31 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/acbab5de/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringResourceFactory.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringResourceFactory.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringResourceFactory.java index 826f619..ba0fee1 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringResourceFactory.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringResourceFactory.java @@ -44,6 +44,7 @@ import org.springframework.context.ApplicationContextAware; public class SpringResourceFactory implements ResourceProvider, ApplicationContextAware { private Constructor<?> c; + private Class<?> type; private ApplicationContext ac; private String beanId; private Method postConstructMethod; @@ -65,26 +66,30 @@ public class SpringResourceFactory implements ResourceProvider, ApplicationConte } private void init() { - Class<?> type = ClassHelper.getRealClassFromClass(ac.getType(beanId)); + type = ClassHelper.getRealClassFromClass(ac.getType(beanId)); if (Proxy.isProxyClass(type)) { type = ClassHelper.getRealClass(ac.getBean(beanId)); } - c = ResourceUtils.findResourceConstructor(type, !isSingleton()); - if (c == null) { - throw new RuntimeException("Resource class " + type - + " has no valid constructor"); - } + isSingleton = ac.isSingleton(beanId); postConstructMethod = ResourceUtils.findPostConstructMethod(type, postConstructMethodName); preDestroyMethod = ResourceUtils.findPreDestroyMethod(type, preDestroyMethodName); - isSingleton = ac.isSingleton(beanId); - if (!isSingleton) { - isPrototype = ac.isPrototype(beanId); - } else { + + if (isSingleton()) { try { singletonInstance = ac.getBean(beanId); } catch (BeansException ex) { - // ignore for now, can be to do with no default constructor available + // ignore for now, try resolving resource constructor later + } + if (singletonInstance != null) { + return; } + } else { + isPrototype = ac.isPrototype(beanId); + } + c = ResourceUtils.findResourceConstructor(type, !isSingleton()); + if (c == null) { + throw new RuntimeException("Resource class " + type + + " has no valid constructor"); } } @@ -156,7 +161,7 @@ public class SpringResourceFactory implements ResourceProvider, ApplicationConte * {@inheritDoc} */ public Class<?> getResourceClass() { - return c.getDeclaringClass(); + return type; } public void setCallPostConstruct(boolean callPostConstruct) { http://git-wip-us.apache.org/repos/asf/cxf/blob/acbab5de/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/spring/SpringResourceFactoryTest.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/spring/SpringResourceFactoryTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/spring/SpringResourceFactoryTest.java index e1119aa..a712e14 100644 --- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/spring/SpringResourceFactoryTest.java +++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/spring/SpringResourceFactoryTest.java @@ -21,10 +21,12 @@ package org.apache.cxf.jaxrs.spring; import java.lang.reflect.Constructor; import java.util.List; + import org.apache.cxf.BusFactory; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; import org.apache.cxf.jaxrs.model.ClassResourceInfo; import org.apache.cxf.jaxrs.resources.BookStore; +import org.apache.cxf.jaxrs.resources.BookStoreConstructor; import org.junit.After; import org.junit.Assert; @@ -56,8 +58,8 @@ public class SpringResourceFactoryTest extends Assert { List<ClassResourceInfo> list = factoryBean.getServiceFactory().getClassResourceInfo(); assertNotNull(list); assertEquals(4, list.size()); - assertSame(BookStore.class, list.get(0).getServiceClass()); - assertSame(BookStore.class, list.get(0).getResourceClass()); + assertSame(BookStoreConstructor.class, list.get(0).getServiceClass()); + assertSame(BookStoreConstructor.class, list.get(0).getResourceClass()); assertSame(BookStore.class, list.get(1).getServiceClass()); assertSame(BookStore.class, list.get(1).getResourceClass()); } @@ -68,10 +70,13 @@ public class SpringResourceFactoryTest extends Assert { assertNotNull(bean); SpringResourceFactory sf = (SpringResourceFactory)bean; assertNotNull(sf.getApplicationContext()); - Constructor<?> c = sf.getBeanConstructor(); - Constructor<BookStore> c2 = BookStore.class.getConstructor(new Class[]{}); - - assertEquals(c.getParameterTypes().length, c2.getParameterTypes().length); assertEquals(isSingleton, sf.isSingleton()); + if (!isSingleton) { + Constructor<?> c = sf.getBeanConstructor(); + Constructor<BookStore> c2 = BookStore.class.getConstructor(new Class[]{}); + + assertEquals(c.getParameterTypes().length, c2.getParameterTypes().length); + } + } } http://git-wip-us.apache.org/repos/asf/cxf/blob/acbab5de/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/spring/servers2.xml ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/spring/servers2.xml b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/spring/servers2.xml index f05954b..54925b4 100644 --- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/spring/servers2.xml +++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/spring/servers2.xml @@ -39,7 +39,9 @@ <bean id="sfactory2" class="org.apache.cxf.jaxrs.spring.SpringResourceFactory"> <property name="beanId" value="bookstore2"/> </bean> - <bean id="bookstore1" class="org.apache.cxf.jaxrs.resources.BookStore"/> + <bean id="bookstore1" class="org.apache.cxf.jaxrs.resources.BookStoreConstructor"> + <constructor-arg value="CXF" /> + </bean> <bean id="bookstore2" class="org.apache.cxf.jaxrs.resources.BookStore" scope="prototype"/> <bean id="bookstore3" class="org.apache.cxf.jaxrs.resources.BookStore"/> <bean id="bookstore4" class="org.apache.cxf.jaxrs.resources.BookStore" scope="prototype"/>
