Browse Source

Avoid proxy error if bean implements ScopedObject

fixes gh-277
pull/339/merge
Spencer Gibb 7 years ago
parent
commit
cd436c7d15
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 10
      spring-cloud-context/pom.xml
  2. 10
      spring-cloud-context/src/main/java/org/springframework/cloud/context/scope/GenericScope.java
  3. 15
      spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeIntegrationTests.java
  4. 1
      spring-cloud-context/src/test/resources/application.properties

10
spring-cloud-context/pom.xml

@ -53,6 +53,16 @@ @@ -53,6 +53,16 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>

10
spring-cloud-context/src/main/java/org/springframework/cloud/context/scope/GenericScope.java

@ -33,6 +33,7 @@ import org.aopalliance.intercept.MethodInvocation; @@ -33,6 +33,7 @@ import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.scope.ScopedObject;
import org.springframework.aop.scope.ScopedProxyFactoryBean;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
@ -457,7 +458,8 @@ public class GenericScope implements Scope, BeanFactoryPostProcessor, @@ -457,7 +458,8 @@ public class GenericScope implements Scope, BeanFactoryPostProcessor,
Method method = invocation.getMethod();
if (AopUtils.isEqualsMethod(method)
|| AopUtils.isToStringMethod(method)
|| AopUtils.isHashCodeMethod(method)) {
|| AopUtils.isHashCodeMethod(method)
|| isScopedObjectGetTargetObject(method)) {
return invocation.proceed();
}
Object proxy = getObject();
@ -477,6 +479,12 @@ public class GenericScope implements Scope, BeanFactoryPostProcessor, @@ -477,6 +479,12 @@ public class GenericScope implements Scope, BeanFactoryPostProcessor,
lock.unlock();
}
}
private boolean isScopedObjectGetTargetObject(Method method) {
return method.getDeclaringClass().equals(ScopedObject.class)
&& method.getName().equals("getTargetObject")
&& method.getParameterTypes().length == 0;
}
}
}

15
spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeIntegrationTests.java

@ -25,23 +25,24 @@ import org.springframework.aop.framework.Advised; @@ -25,23 +25,24 @@ import org.springframework.aop.framework.Advised;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.context.scope.GenericScope;
import org.springframework.cloud.context.scope.refresh.RefreshScopeIntegrationTests.TestConfiguration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import javax.sql.DataSource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
@ -198,8 +199,7 @@ public class RefreshScopeIntegrationTests { @@ -198,8 +199,7 @@ public class RefreshScopeIntegrationTests {
@Configuration
@EnableConfigurationProperties(TestProperties.class)
@Import({ RefreshAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class })
@EnableAutoConfiguration
protected static class TestConfiguration {
@Autowired
@ -214,6 +214,11 @@ public class RefreshScopeIntegrationTests { @@ -214,6 +214,11 @@ public class RefreshScopeIntegrationTests {
return service;
}
@Bean
@RefreshScope
public DataSource dataSource(DataSourceProperties dataSourceProperties) {
return dataSourceProperties.initializeDataSourceBuilder().build();
}
}
@ConfigurationProperties

1
spring-cloud-context/src/test/resources/application.properties

@ -3,4 +3,5 @@ delay: 0 @@ -3,4 +3,5 @@ delay: 0
debug: true
#logging.level.org.springframework.web: DEBUG
#logging.level.org.springframework.context.annotation: DEBUG
logging.level.org.hibernate=ERROR
management.security.enabled=false

Loading…
Cancel
Save