1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.context.annotation4;
18
19 import org.springframework.beans.factory.annotation.Qualifier;
20 import org.springframework.beans.factory.annotation.Value;
21 import org.springframework.context.annotation.Bean;
22 import org.springframework.context.annotation.BeanAge;
23 import org.springframework.context.annotation.Scope;
24 import org.springframework.context.annotation.ScopedProxyMode;
25 import org.springframework.stereotype.Component;
26 import org.springframework.tests.sample.beans.TestBean;
27
28
29
30
31
32
33
34
35 @Component
36 public final class FactoryMethodComponent {
37
38 private int i;
39
40 public static TestBean nullInstance() {
41 return null;
42 }
43
44 @Bean @Qualifier("public")
45 public TestBean publicInstance() {
46 return new TestBean("publicInstance");
47 }
48
49
50 public TestBean publicInstance(boolean doIt) {
51 return new TestBean("publicInstance");
52 }
53
54 @Bean @BeanAge(1)
55 protected TestBean protectedInstance(@Qualifier("public") TestBean spouse, @Value("#{privateInstance.age}") String country) {
56 TestBean tb = new TestBean("protectedInstance", 1);
57 tb.setSpouse(tb);
58 tb.setCountry(country);
59 return tb;
60 }
61
62 @Bean @Scope("prototype")
63 private TestBean privateInstance() {
64 return new TestBean("privateInstance", i++);
65 }
66
67 @Bean @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
68 public TestBean requestScopedInstance() {
69 return new TestBean("requestScopedInstance", 3);
70 }
71
72 @Bean
73 public DependencyBean secondInstance() {
74 return new DependencyBean();
75 }
76
77 }