-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSpringBeanScopeTest.java
More file actions
228 lines (190 loc) · 6.64 KB
/
SpringBeanScopeTest.java
File metadata and controls
228 lines (190 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import javax.inject.Singleton;
/**
*
* Created by vvedenin on 11/14/2015.
*/
public class SpringBeanScopeTest {
@Configuration
public static class TestConfiguration {
@Bean
public BeanForTestScope1 getBeanForTestScope1(){
return new BeanForTestScope1();
}
@Bean
public BeanForTestScope2 getBeanForTestScope2(){
return new BeanForTestScope2();
}
@Bean
@Scope("singleton")
public SingletonClass1 getSingletonClass1(){
return new SingletonClass1();
}
@Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public SingletonClass2 getSingletonClass2(){
return new SingletonClass2();
}
@Bean
@Singleton
public SingletonClass3 getSingletonClass3(){
return new SingletonClass3();
}
@Bean
public SingletonClass4 getSingletonClass4(){
return new SingletonClass4();
}
@Bean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public PrototypeClass1 getPrototypeClass1(){
return new PrototypeClass1();
}
@Bean
@Scope("prototype")
public PrototypeClass2 getPrototypeClass2(){
return new PrototypeClass2();
}
}
@Service
public static class SingletonClass1 {
private int value = 0;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
@Service
public static class SingletonClass2 {
private int value = 0;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
@Service
public static class SingletonClass3 {
private int value = 0;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
@Service
public static class SingletonClass4 {
private int value = 0;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
@Service
public static class PrototypeClass1 {
private int value = 0;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
@Service
public static class PrototypeClass2 {
private int value = 0;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
@Service
public static class BeanForTestScope1 {
@Autowired
private SingletonClass1 singletonClass1;
@Autowired
private SingletonClass2 singletonClass2;
@Autowired
private SingletonClass3 singletonClass3;
@Autowired
private SingletonClass4 singletonClass4;
@Autowired
private PrototypeClass1 prototypeClass1;
@Autowired
private PrototypeClass2 prototypeClass2;
public void setValue(int value) {
singletonClass1.setValue(value);
singletonClass2.setValue(value);
singletonClass3.setValue(value);
singletonClass4.setValue(value);
prototypeClass1.setValue(value);
prototypeClass2.setValue(value);
}
public void printValue() {
System.out.println();
System.out.println("BeanForTestScope1");
System.out.println("Singleton1:" + singletonClass1.getValue());
System.out.println("Singleton2:" + singletonClass2.getValue());
System.out.println("Singleton3:" + singletonClass3.getValue());
System.out.println("Singleton4:" + singletonClass4.getValue());
System.out.println("Prototype1:" + prototypeClass1.getValue());
System.out.println("Prototype2:" + prototypeClass2.getValue());
}
}
@Service
public static class BeanForTestScope2 {
@Autowired
private SingletonClass1 singletonClass1;
@Autowired
private SingletonClass2 singletonClass2;
@Autowired
private SingletonClass3 singletonClass3;
@Autowired
private SingletonClass4 singletonClass4;
@Autowired
private PrototypeClass1 prototypeClass1;
@Autowired
private PrototypeClass2 prototypeClass2;
public void setValue(int value) {
singletonClass1.setValue(value);
singletonClass2.setValue(value);
singletonClass3.setValue(value);
singletonClass4.setValue(value);
prototypeClass1.setValue(value);
prototypeClass2.setValue(value);
}
public void printValue() {
System.out.println();
System.out.println("BeanForTestScope2");
System.out.println("Singleton1:" + singletonClass1.getValue());
System.out.println("Singleton2:" + singletonClass2.getValue());
System.out.println("Singleton3:" + singletonClass3.getValue());
System.out.println("Singleton4:" + singletonClass4.getValue());
System.out.println("Prototype1:" + prototypeClass1.getValue());
System.out.println("Prototype2:" + prototypeClass2.getValue());
}
}
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
BeanForTestScope1 beanForTestScope1 = context.getBean(BeanForTestScope1.class);
BeanForTestScope2 beanForTestScope2 = context.getBean(BeanForTestScope2.class);
beanForTestScope1.setValue(1);
beanForTestScope2.setValue(2);
beanForTestScope1.printValue();
beanForTestScope1.setValue(4);
beanForTestScope2.printValue();
}
}