Skip to content

Commit

Permalink
Merge pull request #37 from mercyblitz/1.0.10
Browse files Browse the repository at this point in the history
1.0.10
  • Loading branch information
mercyblitz authored Sep 17, 2020
2 parents 63f59ee + 9ffa3ad commit 709808c
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Current project that extends `spring-context` is based on Spring Framework 3.2.x
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.9</version>
<version>1.0.10</version>
</dependency>

......
Expand Down
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.9</version>
<version>1.0.10</version>

<properties>
<maven.compiler.source>1.6</maven.compiler.source>
Expand Down Expand Up @@ -125,28 +125,28 @@
<profile>
<id>spring-4.3</id>
<properties>
<spring.framework.version>4.3.28.RELEASE</spring.framework.version>
<spring.framework.version>4.3.29.RELEASE</spring.framework.version>
</properties>
</profile>

<profile>
<id>spring-5.0</id>
<properties>
<spring.framework.version>5.0.18.RELEASE</spring.framework.version>
<spring.framework.version>5.0.19.RELEASE</spring.framework.version>
</properties>
</profile>

<profile>
<id>spring-5.1</id>
<properties>
<spring.framework.version>5.1.17.RELEASE</spring.framework.version>
<spring.framework.version>5.1.18.RELEASE</spring.framework.version>
</properties>
</profile>

<profile>
<id>spring-5.2</id>
<properties>
<spring.framework.version>5.2.8.RELEASE</spring.framework.version>
<spring.framework.version>5.2.9.RELEASE</spring.framework.version>
</properties>
</profile>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -59,8 +60,10 @@
import java.util.concurrent.ConcurrentMap;

import static com.alibaba.spring.util.AnnotationUtils.getAnnotationAttributes;
import static org.springframework.aop.support.AopUtils.getTargetClass;
import static org.springframework.core.BridgeMethodResolver.findBridgedMethod;
import static org.springframework.core.BridgeMethodResolver.isVisibilityBridgeMethodPair;
import static org.springframework.core.GenericTypeResolver.resolveTypeArgument;

/**
* Abstract common {@link BeanPostProcessor} implementation for customized annotation that annotated injected-object.
Expand Down Expand Up @@ -534,7 +537,7 @@ protected AnnotatedFieldElement(Field field, AnnotationAttributes attributes) {
@Override
protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable {

Class<?> injectedType = field.getType();
Class<?> injectedType = resolveInjectedType(bean, field);

Object injectedObject = getInjectedObject(attributes, bean, beanName, injectedType, this);

Expand All @@ -544,5 +547,13 @@ protected void inject(Object bean, String beanName, PropertyValues pvs) throws T

}

private Class<?> resolveInjectedType(Object bean, Field field) {
Type genericType = field.getGenericType();
if (genericType instanceof Class) { // Just a normal Class
return field.getType();
} else { // GenericType
return resolveTypeArgument(getTargetClass(bean), field.getDeclaringClass());
}
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/alibaba/spring/util/ObjectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
@SuppressWarnings("unchecked")
public abstract class ObjectUtils {

/**
* Empty String array
*
* @since 1.0.10
*/
public static final String[] EMPTY_STRING_ARRAY = {};


/**
* Convert from variable arguments to array
*
Expand Down
39 changes: 28 additions & 11 deletions src/main/java/com/alibaba/spring/util/PropertySourcesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Map;
import java.util.Properties;

import static com.alibaba.spring.util.ObjectUtils.EMPTY_STRING_ARRAY;
import static java.util.Collections.unmodifiableMap;

/**
Expand Down Expand Up @@ -99,23 +100,39 @@ public static Map<String, Object> getSubProperties(PropertySources propertySourc

while (iterator.hasNext()) {
PropertySource<?> source = iterator.next();
if (source instanceof EnumerablePropertySource) {
for (String name : ((EnumerablePropertySource<?>) source).getPropertyNames()) {
if (!subProperties.containsKey(name) && name.startsWith(normalizedPrefix)) {
String subName = name.substring(normalizedPrefix.length());
if (!subProperties.containsKey(subName)) { // take first one
Object value = source.getProperty(name);
if (value instanceof String) {
// Resolve placeholder
value = propertyResolver.resolvePlaceholders((String) value);
}
subProperties.put(subName, value);
for (String name : getPropertyNames(source)) {
if (!subProperties.containsKey(name) && name.startsWith(normalizedPrefix)) {
String subName = name.substring(normalizedPrefix.length());
if (!subProperties.containsKey(subName)) { // take first one
Object value = source.getProperty(name);
if (value instanceof String) {
// Resolve placeholder
value = propertyResolver.resolvePlaceholders((String) value);
}
subProperties.put(subName, value);
}
}
}
}

return unmodifiableMap(subProperties);
}

/**
* Get the property names as the array from the specified {@link PropertySource} instance.
*
* @param propertySource {@link PropertySource} instance
* @return non-null
* @since 1.0.10
*/
public static String[] getPropertyNames(PropertySource propertySource) {
String[] propertyNames = propertySource instanceof EnumerablePropertySource ?
((EnumerablePropertySource) propertySource).getPropertyNames() : null;

if (propertyNames == null) {
propertyNames = EMPTY_STRING_ARRAY;
}

return propertyNames;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
*/
package com.alibaba.spring.beans.factory.annotation;

import com.alibaba.spring.util.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.InjectionMetadata;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
Expand All @@ -37,7 +39,8 @@
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {
AnnotationInjectedBeanPostProcessorTest.TestConfiguration.class,
AbstractAnnotationBeanPostProcessorTest.ReferencedAnnotationInjectedBeanPostProcessor.class
AbstractAnnotationBeanPostProcessorTest.ReferencedAnnotationInjectedBeanPostProcessor.class,
AbstractAnnotationBeanPostProcessorTest.GenericConfiguration.class,
})
@SuppressWarnings({"deprecation", "unchecked"})
public class AbstractAnnotationBeanPostProcessorTest {
Expand All @@ -62,6 +65,9 @@ public class AbstractAnnotationBeanPostProcessorTest {
@Autowired
private ConfigurableListableBeanFactory beanFactory;

@Autowired
private GenericConfiguration.GenericChild genericChild;

@Test
public void testCustomizedAnnotationBeanPostProcessor() {

Expand All @@ -72,7 +78,7 @@ public void testCustomizedAnnotationBeanPostProcessor() {
Assert.assertEquals(AnnotationInjectedBeanPostProcessorTest.Referenced.class, processor.getAnnotationType());
Assert.assertEquals(1, processor.getInjectedObjects().size());
Assert.assertTrue(processor.getInjectedObjects().contains(parent.parentUser));
Assert.assertEquals(2, processor.getInjectedFieldObjectsMap().size());
Assert.assertEquals(3, processor.getInjectedFieldObjectsMap().size());
Assert.assertEquals(1, processor.getInjectedMethodObjectsMap().size());
Assert.assertEquals(Ordered.LOWEST_PRECEDENCE - 3, processor.getOrder());
}
Expand All @@ -84,6 +90,7 @@ public void testReferencedUser() {
Assert.assertEquals(parent.user, parent.parentUser);
Assert.assertEquals(parent.user, child.childUser);
Assert.assertEquals(parent.user, userHolder.user);
Assert.assertEquals(parent.user, genericChild.getS());
}

public static class ReferencedAnnotationInjectedBeanPostProcessor extends AbstractAnnotationBeanPostProcessor {
Expand All @@ -105,4 +112,27 @@ protected String buildInjectedObjectCacheKey(AnnotationAttributes attributes, Ob
}
}

public static class GenericConfiguration {


static abstract class GenericParent<S> {

@AnnotationInjectedBeanPostProcessorTest.Referenced
S s;

public S getS() {
return s;
}
}

static class GenericChild extends GenericParent<User> {
}

@Bean
public GenericChild genericChild() {
return new GenericChild();
}

}

}

0 comments on commit 709808c

Please sign in to comment.