View Javadoc
1   /*
2    * Copyright 2002-2013 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.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   * Class used to test the functionality of factory method bean definitions
30   * declared inside a Spring component class.
31   *
32   * @author Mark Pollack
33   * @author Juergen Hoeller
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  	// to be ignored
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  }