View Javadoc
1   /*
2    * Copyright 2002-2011 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.context.annotation.AnnotationConfigApplicationContext;
22  import org.springframework.context.annotation.Bean;
23  import org.springframework.context.annotation.Configuration;
24  
25  import static org.hamcrest.CoreMatchers.*;
26  import static org.junit.Assert.*;
27  
28  /**
29   * Reproduces SPR-8756, which has been marked as "won't fix" for reasons
30   * described in the JIRA issue. Also demonstrates the suggested workaround.
31   *
32   * @author Chris Beams
33   */
34  public class PackagePrivateBeanMethodInheritanceTests {
35  
36  	@Test
37  	public void repro() {
38  		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
39  		ctx.register(ReproConfig.class);
40  		ctx.refresh();
41  		Foo foo1 = ctx.getBean("foo1", Foo.class);
42  		Foo foo2 = ctx.getBean("foo2", Foo.class);
43  		ctx.getBean("packagePrivateBar", Bar.class); // <-- i.e. @Bean was registered
44  		assertThat(foo1.bar, not(is(foo2.bar)));     // <-- i.e. @Bean *not* enhanced
45  	}
46  
47  	@Test
48  	public void workaround() {
49  		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
50  		ctx.register(WorkaroundConfig.class);
51  		ctx.refresh();
52  		Foo foo1 = ctx.getBean("foo1", Foo.class);
53  		Foo foo2 = ctx.getBean("foo2", Foo.class);
54  		ctx.getBean("protectedBar", Bar.class); // <-- i.e. @Bean was registered
55  		assertThat(foo1.bar, is(foo2.bar));     // <-- i.e. @Bean *was* enhanced
56  	}
57  
58  	public static class Foo {
59  		final Bar bar;
60  		public Foo(Bar bar) {
61  			this.bar = bar;
62  		}
63  	}
64  
65  	public static class Bar {
66  	}
67  
68  	@Configuration
69  	public static class ReproConfig extends org.springframework.context.annotation.configuration.a.BaseConfig {
70  		@Bean
71  		public Foo foo1() {
72  			return new Foo(reproBar());
73  		}
74  
75  		@Bean
76  		public Foo foo2() {
77  			return new Foo(reproBar());
78  		}
79  	}
80  
81  	@Configuration
82  	public static class WorkaroundConfig extends org.springframework.context.annotation.configuration.a.BaseConfig {
83  		@Bean
84  		public Foo foo1() {
85  			return new Foo(workaroundBar());
86  		}
87  
88  		@Bean
89  		public Foo foo2() {
90  			return new Foo(workaroundBar());
91  		}
92  	}
93  }
94