|
| 1 | +/* |
| 2 | + * Copyright 2002-2014 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.context.annotation.configuration; |
| 18 | + |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +import org.springframework.beans.factory.ObjectFactory; |
| 22 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 23 | +import org.springframework.context.annotation.Bean; |
| 24 | +import org.springframework.context.annotation.Configuration; |
| 25 | +import org.springframework.context.annotation.Scope; |
| 26 | +import org.springframework.context.annotation.ScopedProxyMode; |
| 27 | + |
| 28 | +import static org.hamcrest.Matchers.*; |
| 29 | +import static org.junit.Assert.*; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Phillip Webb |
| 33 | + */ |
| 34 | +public class Spr10744Tests { |
| 35 | + |
| 36 | + private static int createCount = 0; |
| 37 | + |
| 38 | + private static int scopeCount = 0; |
| 39 | + |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testSpr10744() throws Exception { |
| 43 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| 44 | + MyTestScope scope = new MyTestScope(); |
| 45 | + context.getBeanFactory().registerScope("myTestScope", scope); |
| 46 | + context.register(MyTestConfiguration.class); |
| 47 | + context.refresh(); |
| 48 | + Foo bean1 = context.getBean("foo", Foo.class); |
| 49 | + Foo bean2 = context.getBean("foo", Foo.class); |
| 50 | + assertThat(bean1, sameInstance(bean2)); |
| 51 | + // Should have created a single instance for the proxy |
| 52 | + assertThat(createCount, equalTo(0)); |
| 53 | + assertThat(scopeCount, equalTo(0)); |
| 54 | + |
| 55 | + // Proxy mode should create new scoped object on each method call |
| 56 | + bean1.getMessage(); |
| 57 | + assertThat(createCount, equalTo(1)); |
| 58 | + assertThat(scopeCount, equalTo(1)); |
| 59 | + bean1.getMessage(); |
| 60 | + assertThat(createCount, equalTo(2)); |
| 61 | + assertThat(scopeCount, equalTo(2)); |
| 62 | + |
| 63 | + context.close(); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + private static class MyTestScope implements org.springframework.beans.factory.config.Scope { |
| 68 | + |
| 69 | + @Override |
| 70 | + public Object get(String name, ObjectFactory<?> objectFactory) { |
| 71 | + scopeCount++; |
| 72 | + return objectFactory.getObject(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Object remove(String name) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void registerDestructionCallback(String name, Runnable callback) { |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public Object resolveContextualObject(String key) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String getConversationId() { |
| 91 | + return null; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + static class Foo { |
| 97 | + |
| 98 | + public Foo() { |
| 99 | + createCount++; |
| 100 | + } |
| 101 | + |
| 102 | + public String getMessage() { |
| 103 | + return "Hello"; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + @Configuration |
| 109 | + static class MyConfiguration { |
| 110 | + |
| 111 | + @Bean |
| 112 | + public Foo foo() { |
| 113 | + return new Foo(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + @Configuration |
| 119 | + static class MyTestConfiguration extends MyConfiguration { |
| 120 | + |
| 121 | + @Override |
| 122 | + @Scope(value = "myTestScope", proxyMode = ScopedProxyMode.TARGET_CLASS) |
| 123 | + @Bean |
| 124 | + public Foo foo() { |
| 125 | + return new Foo(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments