1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32
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);
44 assertThat(foo1.bar, not(is(foo2.bar)));
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);
55 assertThat(foo1.bar, is(foo2.bar));
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