Skip to content

Commit 0b0e8b5

Browse files
chickenljbeiwei30
authored andcommitted
[Dubbo-2678] Add ability to turn off SPI auto injection, special support for Object type. (#2681)
* Add ability to turn off SPI auto injection, special support for generic Object type injection. * Add ability to turn off SPI auto injection, special support for generic Object type injection. * disable() is redundant in DisableInject annotation.
1 parent bcb1899 commit 0b0e8b5

File tree

7 files changed

+143
-3
lines changed

7 files changed

+143
-3
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.common.extension;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
@Documented
26+
@Retention(RetentionPolicy.RUNTIME)
27+
@Target({ElementType.TYPE, ElementType.METHOD})
28+
public @interface DisableInject {
29+
}

dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/ExtensionLoader.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,12 @@ private T injectExtension(T instance) {
515515
if (method.getName().startsWith("set")
516516
&& method.getParameterTypes().length == 1
517517
&& Modifier.isPublic(method.getModifiers())) {
518+
/**
519+
* Check {@link DisableInject} to see if we need auto injection for this property
520+
*/
521+
if (method.getAnnotation(DisableInject.class) != null) {
522+
continue;
523+
}
518524
Class<?> pt = method.getParameterTypes()[0];
519525
try {
520526
String property = method.getName().length() > 3 ? method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4) : "";

dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/ExtensionLoaderTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import com.alibaba.dubbo.common.extensionloader.ext8_add.impl.AddExt4_ManualAdaptive;
4747
import com.alibaba.dubbo.common.extensionloader.ext9_empty.Ext9Empty;
4848
import com.alibaba.dubbo.common.extensionloader.ext9_empty.impl.Ext9EmptyImpl;
49+
import com.alibaba.dubbo.common.extensionloader.injection.InjectExt;
50+
import com.alibaba.dubbo.common.extensionloader.injection.impl.InjectExtImpl;
4951

5052
import junit.framework.Assert;
5153
import org.junit.Test;
@@ -416,4 +418,14 @@ public void testLoadDefaultActivateExtension() throws Exception {
416418
Assert.assertTrue(list.get(1).getClass() == OrderActivateExtImpl1.class);
417419
}
418420

421+
@Test
422+
public void testInjectExtension() {
423+
// test default
424+
InjectExt injectExt = ExtensionLoader.getExtensionLoader(InjectExt.class).getExtension("injection");
425+
InjectExtImpl injectExtImpl = (InjectExtImpl) injectExt;
426+
org.junit.Assert.assertNotNull(injectExtImpl.getSimpleExt());
427+
org.junit.Assert.assertNull(injectExtImpl.getSimpleExt1());
428+
org.junit.Assert.assertNull(injectExtImpl.getGenericType());
429+
}
430+
419431
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.common.extensionloader.injection;
18+
19+
import com.alibaba.dubbo.common.extension.SPI;
20+
21+
/**
22+
*
23+
*/
24+
@SPI("injection")
25+
public interface InjectExt {
26+
String echo(String msg);
27+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.common.extensionloader.injection.impl;
18+
19+
20+
import com.alibaba.dubbo.common.extension.DisableInject;
21+
import com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt;
22+
import com.alibaba.dubbo.common.extensionloader.injection.InjectExt;
23+
24+
public class InjectExtImpl implements InjectExt {
25+
26+
private SimpleExt simpleExt;
27+
28+
private SimpleExt simpleExt1;
29+
30+
private Object genericType;
31+
32+
public void setSimpleExt(SimpleExt simpleExt) {
33+
this.simpleExt = simpleExt;
34+
}
35+
36+
@DisableInject
37+
public void setSimpleExt1(SimpleExt simpleExt1) {
38+
this.simpleExt1 = simpleExt1;
39+
}
40+
41+
public void setGenericType(Object genericType) {
42+
this.genericType = genericType;
43+
}
44+
45+
@Override
46+
public String echo(String msg) {
47+
return null;
48+
}
49+
50+
public SimpleExt getSimpleExt() {
51+
return simpleExt;
52+
}
53+
54+
public SimpleExt getSimpleExt1() {
55+
return simpleExt1;
56+
}
57+
58+
public Object getGenericType() {
59+
return genericType;
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
injection=com.alibaba.dubbo.common.extensionloader.injection.impl.InjectExtImpl

dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/extension/SpringExtensionFactory.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,25 @@ public <T> T getExtension(Class<T> type, String name) {
6060
}
6161
}
6262

63-
logger.warn("No spring extension(bean) named:" + name + ", try to find an extension(bean) of type " + type.getName());
63+
logger.warn("No spring extension (bean) named:" + name + ", try to find an extension (bean) of type " + type.getName());
64+
65+
if (Object.class == type) {
66+
return null;
67+
}
6468

6569
for (ApplicationContext context : contexts) {
6670
try {
6771
return context.getBean(type);
6872
} catch (NoUniqueBeanDefinitionException multiBeanExe) {
69-
throw multiBeanExe;
73+
logger.warn("Find more than 1 spring extensions (beans) of type " + type.getName() + ", will stop auto injection. Please make sure you have specified the concrete parameter type and there's only one extension of that type.");
7074
} catch (NoSuchBeanDefinitionException noBeanExe) {
7175
if (logger.isDebugEnabled()) {
7276
logger.debug("Error when get spring extension(bean) for type:" + type.getName(), noBeanExe);
7377
}
7478
}
7579
}
7680

77-
logger.warn("No spring extension(bean) named:" + name + ", type:" + type.getName() + " found, stop get bean.");
81+
logger.warn("No spring extension (bean) named:" + name + ", type:" + type.getName() + " found, stop get bean.");
7882

7983
return null;
8084
}

0 commit comments

Comments
 (0)