Injector should support auto-wiring so eg Spring can create bean instances that uses auto-wiring on bean constructors.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/184e07a3 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/184e07a3 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/184e07a3 Branch: refs/heads/master Commit: 184e07a310cf002ede2efd49131fcea97bc3d227 Parents: f5296eb Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Jun 21 14:49:43 2017 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Jun 22 10:27:34 2017 +0200 ---------------------------------------------------------------------- .../org/apache/camel/impl/DefaultInjector.java | 5 +++ .../org/apache/camel/model/BeanDefinition.java | 13 +++++- .../java/org/apache/camel/spi/Injector.java | 7 ++++ .../apache/camel/util/ReflectionInjector.java | 5 +++ .../org/apache/camel/cdi/CdiCamelInjector.java | 6 +++ .../apache/camel/guice/impl/GuiceInjector.java | 5 +++ .../apache/camel/spring/spi/SpringInjector.java | 5 +++ .../component/bean/AutowireConstructorBean.java | 32 +++++++++++++++ .../bean/AutowireConstructorRouteTest.java | 43 ++++++++++++++++++++ .../bean/AutowireConstructorRouteTwoTest.java | 43 ++++++++++++++++++++ .../bean/AutowireConstructorTwoBean.java | 39 ++++++++++++++++++ .../bean/AutowireConstructorRouteTest.xml | 37 +++++++++++++++++ .../bean/AutowireConstructorRouteTwoTest.xml | 37 +++++++++++++++++ 13 files changed, 276 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/184e07a3/camel-core/src/main/java/org/apache/camel/impl/DefaultInjector.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultInjector.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultInjector.java index 7c6ef3c..9e72b9e 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/DefaultInjector.java +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultInjector.java @@ -67,4 +67,9 @@ public class DefaultInjector implements Injector { } return answer; } + + @Override + public boolean supportsAutoWiring() { + return delegate.supportsAutoWiring(); + } } http://git-wip-us.apache.org/repos/asf/camel/blob/184e07a3/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java b/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java index 222d9f6..92daa39 100644 --- a/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java @@ -274,8 +274,19 @@ public class BeanDefinition extends NoOutputDefinition<BeanDefinition> { clazz = beanClass; } + // attempt to create bean using injector which supports auto-wiring + if (isCacheBean() && routeContext.getCamelContext().getInjector().supportsAutoWiring()) { + try { + log.debug("Attempting to create new bean instance from class: {} via auto-wiring enabled", clazz); + bean = CamelContextHelper.newInstance(routeContext.getCamelContext(), clazz); + } catch (Throwable e) { + log.debug("Error creating new bean instance from class: " + clazz + ". This exception is ignored", e); + } + } + // create a bean if there is a default public no-arg constructor - if (isCacheBean() && ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) { + if (bean == null && isCacheBean() && ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) { + log.debug("Class has default no-arg constructor so creating a new bean instance: {}", clazz); bean = CamelContextHelper.newInstance(routeContext.getCamelContext(), clazz); ObjectHelper.notNull(bean, "bean", this); } http://git-wip-us.apache.org/repos/asf/camel/blob/184e07a3/camel-core/src/main/java/org/apache/camel/spi/Injector.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/spi/Injector.java b/camel-core/src/main/java/org/apache/camel/spi/Injector.java index ec39e2b..9feb41f 100644 --- a/camel-core/src/main/java/org/apache/camel/spi/Injector.java +++ b/camel-core/src/main/java/org/apache/camel/spi/Injector.java @@ -44,4 +44,11 @@ public interface Injector { */ <T> T newInstance(Class<T> type, Object instance); + /** + * Whether the injector supports creating new instances using auto-wiring. + * If this is possible then bean instances is attempt first to be created this way + * and if not, then the bean can only be created if there is a public no-arg constructor. + */ + boolean supportsAutoWiring(); + } http://git-wip-us.apache.org/repos/asf/camel/blob/184e07a3/camel-core/src/main/java/org/apache/camel/util/ReflectionInjector.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/ReflectionInjector.java b/camel-core/src/main/java/org/apache/camel/util/ReflectionInjector.java index a82dc6a..79142da 100644 --- a/camel-core/src/main/java/org/apache/camel/util/ReflectionInjector.java +++ b/camel-core/src/main/java/org/apache/camel/util/ReflectionInjector.java @@ -41,4 +41,9 @@ public class ReflectionInjector implements Injector { } return newInstance(type); } + + @Override + public boolean supportsAutoWiring() { + return true; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/184e07a3/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelInjector.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelInjector.java b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelInjector.java index a658051..da58eec 100755 --- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelInjector.java +++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelInjector.java @@ -43,4 +43,10 @@ final class CdiCamelInjector implements Injector { public <T> T newInstance(Class<T> type, Object instance) { return injector.newInstance(type, instance); } + + @Override + public boolean supportsAutoWiring() { + // TODO: cdi to support some kind of @Inject on constructors? + return false; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/184e07a3/components/camel-guice/src/main/java/org/apache/camel/guice/impl/GuiceInjector.java ---------------------------------------------------------------------- diff --git a/components/camel-guice/src/main/java/org/apache/camel/guice/impl/GuiceInjector.java b/components/camel-guice/src/main/java/org/apache/camel/guice/impl/GuiceInjector.java index 1544400..c73f6db 100644 --- a/components/camel-guice/src/main/java/org/apache/camel/guice/impl/GuiceInjector.java +++ b/components/camel-guice/src/main/java/org/apache/camel/guice/impl/GuiceInjector.java @@ -47,4 +47,9 @@ public class GuiceInjector implements Injector { return newInstance(type); } + @Override + public boolean supportsAutoWiring() { + return false; + } + } http://git-wip-us.apache.org/repos/asf/camel/blob/184e07a3/components/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringInjector.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringInjector.java b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringInjector.java index 157c8da..77549d4 100644 --- a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringInjector.java +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringInjector.java @@ -50,6 +50,11 @@ public class SpringInjector implements Injector { return newInstance(type); } + @Override + public boolean supportsAutoWiring() { + return true; + } + public int getAutowireMode() { return autowireMode; } http://git-wip-us.apache.org/repos/asf/camel/blob/184e07a3/components/camel-spring/src/test/java/org/apache/camel/component/bean/AutowireConstructorBean.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/bean/AutowireConstructorBean.java b/components/camel-spring/src/test/java/org/apache/camel/component/bean/AutowireConstructorBean.java new file mode 100644 index 0000000..a7dcd27 --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/component/bean/AutowireConstructorBean.java @@ -0,0 +1,32 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.bean; + +import org.springframework.beans.factory.annotation.Autowired; + +public class AutowireConstructorBean { + + private final MyBeanCounter counter; + + public AutowireConstructorBean(@Autowired MyBeanCounter counter) { + this.counter = counter; + } + + public String hello(String name) { + return "Hello " + name + " at " + counter.count(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/184e07a3/components/camel-spring/src/test/java/org/apache/camel/component/bean/AutowireConstructorRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/bean/AutowireConstructorRouteTest.java b/components/camel-spring/src/test/java/org/apache/camel/component/bean/AutowireConstructorRouteTest.java new file mode 100644 index 0000000..88c9136 --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/component/bean/AutowireConstructorRouteTest.java @@ -0,0 +1,43 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.bean; + +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.SpringTestSupport; +import org.springframework.context.support.AbstractXmlApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @version + */ +public class AutowireConstructorRouteTest extends SpringTestSupport { + + public void testAutowireConstructor() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("Hello Camel at 1", "Hello World at 2"); + + template.sendBody("direct:start", "Camel"); + template.sendBody("direct:start", "World"); + + assertMockEndpointsSatisfied(); + } + + protected AbstractXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/component/bean/AutowireConstructorRouteTest.xml"); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/184e07a3/components/camel-spring/src/test/java/org/apache/camel/component/bean/AutowireConstructorRouteTwoTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/bean/AutowireConstructorRouteTwoTest.java b/components/camel-spring/src/test/java/org/apache/camel/component/bean/AutowireConstructorRouteTwoTest.java new file mode 100644 index 0000000..dcf1a96 --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/component/bean/AutowireConstructorRouteTwoTest.java @@ -0,0 +1,43 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.bean; + +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.SpringTestSupport; +import org.springframework.context.support.AbstractXmlApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @version + */ +public class AutowireConstructorRouteTwoTest extends SpringTestSupport { + + public void testAutowireConstructor() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("Hello Camel at 1", "Hello World at 2"); + + template.sendBody("direct:start", "Camel"); + template.sendBody("direct:start", "World"); + + assertMockEndpointsSatisfied(); + } + + protected AbstractXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/component/bean/AutowireConstructorRouteTwoTest.xml"); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/184e07a3/components/camel-spring/src/test/java/org/apache/camel/component/bean/AutowireConstructorTwoBean.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/bean/AutowireConstructorTwoBean.java b/components/camel-spring/src/test/java/org/apache/camel/component/bean/AutowireConstructorTwoBean.java new file mode 100644 index 0000000..4412e3a --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/component/bean/AutowireConstructorTwoBean.java @@ -0,0 +1,39 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.bean; + +import org.springframework.beans.factory.annotation.Autowired; + +/** + * This bean has both a default no-arg and a autowired constructor + */ +public class AutowireConstructorTwoBean { + + private final MyBeanCounter counter; + + public AutowireConstructorTwoBean() { + this.counter = null; + } + + public AutowireConstructorTwoBean(@Autowired MyBeanCounter counter) { + this.counter = counter; + } + + public String hello(String name) { + return "Hello " + name + " at " + counter.count(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/184e07a3/components/camel-spring/src/test/resources/org/apache/camel/component/bean/AutowireConstructorRouteTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/resources/org/apache/camel/component/bean/AutowireConstructorRouteTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/component/bean/AutowireConstructorRouteTest.xml new file mode 100644 index 0000000..e891a03 --- /dev/null +++ b/components/camel-spring/src/test/resources/org/apache/camel/component/bean/AutowireConstructorRouteTest.xml @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:start"/> + <bean beanType="org.apache.camel.component.bean.AutowireConstructorBean"/> + <to uri="mock:result"/> + </route> + </camelContext> + + <bean id="myCounter" class="org.apache.camel.component.bean.MyBeanCounter"/> + +</beans> http://git-wip-us.apache.org/repos/asf/camel/blob/184e07a3/components/camel-spring/src/test/resources/org/apache/camel/component/bean/AutowireConstructorRouteTwoTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/resources/org/apache/camel/component/bean/AutowireConstructorRouteTwoTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/component/bean/AutowireConstructorRouteTwoTest.xml new file mode 100644 index 0000000..708b4ac --- /dev/null +++ b/components/camel-spring/src/test/resources/org/apache/camel/component/bean/AutowireConstructorRouteTwoTest.xml @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:start"/> + <bean beanType="org.apache.camel.component.bean.AutowireConstructorTwoBean"/> + <to uri="mock:result"/> + </route> + </camelContext> + + <bean id="myCounter" class="org.apache.camel.component.bean.MyBeanCounter"/> + +</beans>