org.springframework.context.annotation.ScopedProxyMode Java Examples

The following examples show how to use org.springframework.context.annotation.ScopedProxyMode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSessionScopeWithNoProxy() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	ApplicationContext context = createContext(ScopedProxyMode.NO);
	ScopedTestBean bean = (ScopedTestBean) context.getBean("session");

	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributesWithSession);
	// not a proxy so this should not have changed
	assertEquals(MODIFIED_NAME, bean.getName());

	// but a newly retrieved bean should have the default name
	ScopedTestBean bean2 = (ScopedTestBean) context.getBean("session");
	assertEquals(DEFAULT_NAME, bean2.getName());
}
 
Example #2
Source File: ClassPathBeanDefinitionScannerScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSessionScopeWithProxiedInterfaces() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	ApplicationContext context = createContext(ScopedProxyMode.INTERFACES);
	IScopedTestBean bean = (IScopedTestBean) context.getBean("session");

	// should be dynamic proxy, implementing both interfaces
	assertTrue(AopUtils.isJdkDynamicProxy(bean));
	assertTrue(bean instanceof AnotherScopeTestInterface);

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributesWithSession);
	// this is a proxy so it should be reset to default
	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	IScopedTestBean bean2 = (IScopedTestBean) context.getBean("session");
	assertEquals(MODIFIED_NAME, bean2.getName());
	bean2.setName(DEFAULT_NAME);
	assertEquals(DEFAULT_NAME, bean.getName());

	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	assertEquals(MODIFIED_NAME, bean.getName());
}
 
Example #3
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSingletonScopeWithNoProxy() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.NO);
	ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton");
	assertTrue(context.isSingleton("singleton"));
	assertFalse(context.isPrototype("singleton"));

	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// not a proxy so this should not have changed
	assertEquals(MODIFIED_NAME, bean.getName());

	// singleton bean, so name should be modified even after lookup
	ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton");
	assertEquals(MODIFIED_NAME, bean2.getName());
}
 
Example #4
Source File: ClassPathBeanDefinitionScannerScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequestScopeWithNoProxy() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.NO);
	ScopedTestBean bean = (ScopedTestBean) context.getBean("request");

	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// not a proxy so this should not have changed
	assertEquals(MODIFIED_NAME, bean.getName());

	// but a newly retrieved bean should have the default name
	ScopedTestBean bean2 = (ScopedTestBean) context.getBean("request");
	assertEquals(DEFAULT_NAME, bean2.getName());
}
 
Example #5
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSingletonScopeIgnoresProxyTargetClass() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.TARGET_CLASS);
	ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton");

	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// not a proxy so this should not have changed
	assertEquals(MODIFIED_NAME, bean.getName());

	// singleton bean, so name should be modified even after lookup
	ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton");
	assertEquals(MODIFIED_NAME, bean2.getName());
}
 
Example #6
Source File: ClassPathBeanDefinitionScannerScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private ApplicationContext createContext(ScopedProxyMode scopedProxyMode) {
	GenericWebApplicationContext context = new GenericWebApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
	scanner.setIncludeAnnotationConfig(false);
	scanner.setBeanNameGenerator(new BeanNameGenerator() {
		@Override
		public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
			return definition.getScope();
		}
	});
	scanner.setScopedProxyMode(scopedProxyMode);

	// Scan twice in order to find errors in the bean definition compatibility check.
	scanner.scan(getClass().getPackage().getName());
	scanner.scan(getClass().getPackage().getName());

	context.refresh();
	return context;
}
 
Example #7
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSessionScopeWithProxiedInterfaces() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	ApplicationContext context = createContext(ScopedProxyMode.INTERFACES);
	IScopedTestBean bean = (IScopedTestBean) context.getBean("session");

	// should be dynamic proxy, implementing both interfaces
	assertTrue(AopUtils.isJdkDynamicProxy(bean));
	assertTrue(bean instanceof AnotherScopeTestInterface);

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributesWithSession);
	// this is a proxy so it should be reset to default
	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	IScopedTestBean bean2 = (IScopedTestBean) context.getBean("session");
	assertEquals(MODIFIED_NAME, bean2.getName());
	bean2.setName(DEFAULT_NAME);
	assertEquals(DEFAULT_NAME, bean.getName());

	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	assertEquals(MODIFIED_NAME, bean.getName());
}
 
Example #8
Source File: ClassPathBeanDefinitionScannerScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequestScopeWithProxiedInterfaces() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.INTERFACES);
	IScopedTestBean bean = (IScopedTestBean) context.getBean("request");

	// should be dynamic proxy, implementing both interfaces
	assertTrue(AopUtils.isJdkDynamicProxy(bean));
	assertTrue(bean instanceof AnotherScopeTestInterface);

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// this is a proxy so it should be reset to default
	assertEquals(DEFAULT_NAME, bean.getName());

	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	assertEquals(MODIFIED_NAME, bean.getName());
}
 
Example #9
Source File: ClassPathBeanDefinitionScannerScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingletonScopeWithNoProxy() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.NO);
	ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton");

	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// not a proxy so this should not have changed
	assertEquals(MODIFIED_NAME, bean.getName());

	// singleton bean, so name should be modified even after lookup
	ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton");
	assertEquals(MODIFIED_NAME, bean2.getName());
}
 
Example #10
Source File: ClassPathBeanDefinitionScannerScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingletonScopeIgnoresProxyInterfaces() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.INTERFACES);
	ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton");

	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// not a proxy so this should not have changed
	assertEquals(MODIFIED_NAME, bean.getName());

	// singleton bean, so name should be modified even after lookup
	ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton");
	assertEquals(MODIFIED_NAME, bean2.getName());
}
 
Example #11
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequestScopeWithProxiedTargetClass() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.TARGET_CLASS);
	IScopedTestBean bean = (IScopedTestBean) context.getBean("request");

	// should be a class-based proxy
	assertTrue(AopUtils.isCglibProxy(bean));
	assertTrue(bean instanceof RequestScopedTestBean);

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// this is a proxy so it should be reset to default
	assertEquals(DEFAULT_NAME, bean.getName());

	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	assertEquals(MODIFIED_NAME, bean.getName());
}
 
Example #12
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingletonScopeIgnoresProxyInterfaces() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.INTERFACES);
	ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton");

	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// not a proxy so this should not have changed
	assertEquals(MODIFIED_NAME, bean.getName());

	// singleton bean, so name should be modified even after lookup
	ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton");
	assertEquals(MODIFIED_NAME, bean2.getName());
}
 
Example #13
Source File: ClassPathBeanDefinitionScannerScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequestScopeWithProxiedTargetClass() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.TARGET_CLASS);
	IScopedTestBean bean = (IScopedTestBean) context.getBean("request");

	// should be a class-based proxy
	assertTrue(AopUtils.isCglibProxy(bean));
	assertTrue(bean instanceof RequestScopedTestBean);

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// this is a proxy so it should be reset to default
	assertEquals(DEFAULT_NAME, bean.getName());

	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	assertEquals(MODIFIED_NAME, bean.getName());
}
 
Example #14
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testRequestScopeWithProxiedTargetClass() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.TARGET_CLASS);
	IScopedTestBean bean = (IScopedTestBean) context.getBean("request");

	// should be a class-based proxy
	assertTrue(AopUtils.isCglibProxy(bean));
	assertTrue(bean instanceof RequestScopedTestBean);

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// this is a proxy so it should be reset to default
	assertEquals(DEFAULT_NAME, bean.getName());

	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	assertEquals(MODIFIED_NAME, bean.getName());
}
 
Example #15
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testRequestScopeWithProxiedInterfaces() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.INTERFACES);
	IScopedTestBean bean = (IScopedTestBean) context.getBean("request");

	// should be dynamic proxy, implementing both interfaces
	assertTrue(AopUtils.isJdkDynamicProxy(bean));
	assertTrue(bean instanceof AnotherScopeTestInterface);

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// this is a proxy so it should be reset to default
	assertEquals(DEFAULT_NAME, bean.getName());

	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	assertEquals(MODIFIED_NAME, bean.getName());
}
 
Example #16
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSessionScopeWithProxiedInterfaces() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	ApplicationContext context = createContext(ScopedProxyMode.INTERFACES);
	IScopedTestBean bean = (IScopedTestBean) context.getBean("session");

	// should be dynamic proxy, implementing both interfaces
	assertTrue(AopUtils.isJdkDynamicProxy(bean));
	assertTrue(bean instanceof AnotherScopeTestInterface);

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributesWithSession);
	// this is a proxy so it should be reset to default
	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	IScopedTestBean bean2 = (IScopedTestBean) context.getBean("session");
	assertEquals(MODIFIED_NAME, bean2.getName());
	bean2.setName(DEFAULT_NAME);
	assertEquals(DEFAULT_NAME, bean.getName());

	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	assertEquals(MODIFIED_NAME, bean.getName());
}
 
Example #17
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSingletonScopeIgnoresProxyTargetClass() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.TARGET_CLASS);
	ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton");

	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// not a proxy so this should not have changed
	assertEquals(MODIFIED_NAME, bean.getName());

	// singleton bean, so name should be modified even after lookup
	ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton");
	assertEquals(MODIFIED_NAME, bean2.getName());
}
 
Example #18
Source File: ClassPathBeanDefinitionScannerScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingletonScopeIgnoresProxyTargetClass() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.TARGET_CLASS);
	ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton");

	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// not a proxy so this should not have changed
	assertEquals(MODIFIED_NAME, bean.getName());

	// singleton bean, so name should be modified even after lookup
	ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton");
	assertEquals(MODIFIED_NAME, bean2.getName());
}
 
Example #19
Source File: ServiceConfiguration.java    From prebid-server-java with Apache License 2.0 6 votes vote down vote up
@Bean
@Scope(scopeName = VertxContextScope.NAME, proxyMode = ScopedProxyMode.INTERFACES)
@ConditionalOnProperty(prefix = "http-client.circuit-breaker", name = "enabled", havingValue = "true")
CircuitBreakerSecuredHttpClient circuitBreakerSecuredHttpClient(
        Vertx vertx,
        Metrics metrics,
        HttpClientProperties httpClientProperties,
        @Qualifier("httpClientCircuitBreakerProperties") CircuitBreakerProperties circuitBreakerProperties,
        Clock clock) {

    final HttpClient httpClient = createBasicHttpClient(vertx, httpClientProperties.getMaxPoolSize(),
            httpClientProperties.getConnectTimeoutMs(), httpClientProperties.getUseCompression(),
            httpClientProperties.getMaxRedirects(), httpClientProperties.getSsl(),
            httpClientProperties.getJksPath(), httpClientProperties.getJksPassword());
    return new CircuitBreakerSecuredHttpClient(vertx, httpClient, metrics,
            circuitBreakerProperties.getOpeningThreshold(), circuitBreakerProperties.getOpeningIntervalMs(),
            circuitBreakerProperties.getClosingIntervalMs(), clock);
}
 
Example #20
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSessionScopeWithProxiedInterfaces() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	ApplicationContext context = createContext(ScopedProxyMode.INTERFACES);
	IScopedTestBean bean = (IScopedTestBean) context.getBean("session");

	// should be dynamic proxy, implementing both interfaces
	assertTrue(AopUtils.isJdkDynamicProxy(bean));
	assertTrue(bean instanceof AnotherScopeTestInterface);

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributesWithSession);
	// this is a proxy so it should be reset to default
	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	IScopedTestBean bean2 = (IScopedTestBean) context.getBean("session");
	assertEquals(MODIFIED_NAME, bean2.getName());
	bean2.setName(DEFAULT_NAME);
	assertEquals(DEFAULT_NAME, bean.getName());

	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	assertEquals(MODIFIED_NAME, bean.getName());
}
 
Example #21
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingletonScopeWithNoProxy() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.NO);
	ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton");
	assertTrue(context.isSingleton("singleton"));
	assertFalse(context.isPrototype("singleton"));

	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// not a proxy so this should not have changed
	assertEquals(MODIFIED_NAME, bean.getName());

	// singleton bean, so name should be modified even after lookup
	ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton");
	assertEquals(MODIFIED_NAME, bean2.getName());
}
 
Example #22
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testRequestScopeWithProxiedInterfaces() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.INTERFACES);
	IScopedTestBean bean = (IScopedTestBean) context.getBean("request");

	// should be dynamic proxy, implementing both interfaces
	assertTrue(AopUtils.isJdkDynamicProxy(bean));
	assertTrue(bean instanceof AnotherScopeTestInterface);

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// this is a proxy so it should be reset to default
	assertEquals(DEFAULT_NAME, bean.getName());

	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	assertEquals(MODIFIED_NAME, bean.getName());
}
 
Example #23
Source File: Config.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
@Scope(
    value = WebApplicationContext.SCOPE_SESSION, 
    proxyMode = ScopedProxyMode.TARGET_CLASS)
public TodoList todos() {
    return new TodoList();
}
 
Example #24
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private ApplicationContext createContext(final ScopedProxyMode scopedProxyMode) {
	GenericWebApplicationContext context = new GenericWebApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
	scanner.setIncludeAnnotationConfig(false);
	scanner.setScopeMetadataResolver(new ScopeMetadataResolver() {
		@Override
		public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
			ScopeMetadata metadata = new ScopeMetadata();
			if (definition instanceof AnnotatedBeanDefinition) {
				AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;
				for (String type : annDef.getMetadata().getAnnotationTypes()) {
					if (type.equals(javax.inject.Singleton.class.getName())) {
						metadata.setScopeName(BeanDefinition.SCOPE_SINGLETON);
						break;
					}
					else if (annDef.getMetadata().getMetaAnnotationTypes(type).contains(javax.inject.Scope.class.getName())) {
						metadata.setScopeName(type.substring(type.length() - 13, type.length() - 6).toLowerCase());
						metadata.setScopedProxyMode(scopedProxyMode);
						break;
					}
					else if (type.startsWith("javax.inject")) {
						metadata.setScopeName(BeanDefinition.SCOPE_PROTOTYPE);
					}
				}
			}
			return metadata;
		}
	});

	// Scan twice in order to find errors in the bean definition compatibility check.
	scanner.scan(getClass().getPackage().getName());
	scanner.scan(getClass().getPackage().getName());

	context.registerAlias("classPathBeanDefinitionScannerJsr330ScopeIntegrationTests.SessionScopedTestBean", "session");
	context.refresh();
	return context;
}
 
Example #25
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testSessionScopeWithProxiedTargetClass() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	ApplicationContext context = createContext(ScopedProxyMode.TARGET_CLASS);
	IScopedTestBean bean = (IScopedTestBean) context.getBean("session");

	// should be a class-based proxy
	assertTrue(AopUtils.isCglibProxy(bean));
	assertTrue(bean instanceof ScopedTestBean);
	assertTrue(bean instanceof SessionScopedTestBean);

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributesWithSession);
	// this is a proxy so it should be reset to default
	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	IScopedTestBean bean2 = (IScopedTestBean) context.getBean("session");
	assertEquals(MODIFIED_NAME, bean2.getName());
	bean2.setName(DEFAULT_NAME);
	assertEquals(DEFAULT_NAME, bean.getName());

	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	assertEquals(MODIFIED_NAME, bean.getName());
}
 
Example #26
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testSessionScopeWithProxiedTargetClass() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	ApplicationContext context = createContext(ScopedProxyMode.TARGET_CLASS);
	IScopedTestBean bean = (IScopedTestBean) context.getBean("session");

	// should be a class-based proxy
	assertTrue(AopUtils.isCglibProxy(bean));
	assertTrue(bean instanceof ScopedTestBean);
	assertTrue(bean instanceof SessionScopedTestBean);

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributesWithSession);
	// this is a proxy so it should be reset to default
	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	IScopedTestBean bean2 = (IScopedTestBean) context.getBean("session");
	assertEquals(MODIFIED_NAME, bean2.getName());
	bean2.setName(DEFAULT_NAME);
	assertEquals(DEFAULT_NAME, bean.getName());

	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	assertEquals(MODIFIED_NAME, bean.getName());
}
 
Example #27
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private ApplicationContext createContext(final ScopedProxyMode scopedProxyMode) {
	GenericWebApplicationContext context = new GenericWebApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
	scanner.setIncludeAnnotationConfig(false);
	scanner.setScopeMetadataResolver(new ScopeMetadataResolver() {
		@Override
		public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
			ScopeMetadata metadata = new ScopeMetadata();
			if (definition instanceof AnnotatedBeanDefinition) {
				AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;
				for (String type : annDef.getMetadata().getAnnotationTypes()) {
					if (type.equals(javax.inject.Singleton.class.getName())) {
						metadata.setScopeName(BeanDefinition.SCOPE_SINGLETON);
						break;
					}
					else if (annDef.getMetadata().getMetaAnnotationTypes(type).contains(javax.inject.Scope.class.getName())) {
						metadata.setScopeName(type.substring(type.length() - 13, type.length() - 6).toLowerCase());
						metadata.setScopedProxyMode(scopedProxyMode);
						break;
					}
					else if (type.startsWith("javax.inject")) {
						metadata.setScopeName(BeanDefinition.SCOPE_PROTOTYPE);
					}
				}
			}
			return metadata;
		}
	});

	// Scan twice in order to find errors in the bean definition compatibility check.
	scanner.scan(getClass().getPackage().getName());
	scanner.scan(getClass().getPackage().getName());

	context.registerAlias("classPathBeanDefinitionScannerJsr330ScopeIntegrationTests.SessionScopedTestBean", "session");
	context.refresh();
	return context;
}
 
Example #28
Source File: GitHubConfiguration.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public GitHub gitHub(ConnectionRepository repository) {
	Connection<GitHub> connection = repository
			.findPrimaryConnection(GitHub.class);
	return connection != null ? connection.getApi() : null;
}
 
Example #29
Source File: GitHubConfiguration.java    From OAuth-2.0-Cookbook with MIT License 5 votes vote down vote up
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public GitHub gitHub(ConnectionRepository repository) {
	Connection<GitHub> connection = repository
			.findPrimaryConnection(GitHub.class);
	return connection != null ? connection.getApi() : null;
}
 
Example #30
Source File: DataSourceDecoratorAutoConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Bean
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
public DataSource dataSource() {
    BasicDataSource pool = new BasicDataSource();
    pool.setDriverClassName("org.hsqldb.jdbcDriver");
    pool.setUrl("jdbc:hsqldb:target/overridedb");
    pool.setUsername("sa");
    return pool;
}