File tree Expand file tree Collapse file tree 6 files changed +98
-3
lines changed
main/java/com/ftm/vcp/beanmode
test/java/com/ftm/vcp/beanmode Expand file tree Collapse file tree 6 files changed +98
-3
lines changed Original file line number Diff line number Diff line change @@ -27,4 +27,5 @@ configuration updates.
27
27
[ This video] ( https://www.youtube.com/watch?time_continue=367&v=GlV5sXdXPu4&feature=emb_logo&themeRefresh=1 ) helped me discover this great library.
28
28
29
29
### Other useful notes
30
- * [ Bean post processor] ( src/main/java/com/ftm/vcp/beanpostprocessor/notes_bean_post_processor.md )
30
+ * [ Bean post processor] ( src/main/java/com/ftm/vcp/beanpostprocessor/notes_bean_post_processor.md )
31
+ * [ Bean configuration full vs lite mode] ( src/main/java/com/ftm/vcp/beanmode/notes_full_vs_line_mode.md )
Original file line number Diff line number Diff line change 5
5
import org .springframework .context .annotation .Bean ;
6
6
import org .springframework .context .annotation .Configuration ;
7
7
8
+ /**
9
+ * @see org.springframework.context.annotation.ConfigurationClassUtils
10
+ */
8
11
@ Configuration
9
12
public class BeanFullModeConfig {
10
13
Original file line number Diff line number Diff line change 4
4
import com .ftm .vcp .beanmode .model .Person ;
5
5
import org .springframework .context .annotation .Bean ;
6
6
7
+ /**
8
+ * @see org.springframework.context.annotation.ConfigurationClassUtils
9
+ */
7
10
public class BeanLiteModeConfig {
8
11
9
12
@ Bean ("john" )
Original file line number Diff line number Diff line change
1
+ package com .ftm .vcp .beanmode .config ;
2
+
3
+ import com .ftm .vcp .beanmode .model .Name ;
4
+ import com .ftm .vcp .beanmode .model .Person ;
5
+ import org .springframework .context .annotation .Bean ;
6
+ import org .springframework .context .annotation .Configuration ;
7
+ import org .springframework .stereotype .Component ;
8
+
9
+ /**
10
+ * @see org.springframework.context.annotation.ConfigurationClassUtils
11
+ */
12
+ @ Configuration (proxyBeanMethods = false ) // This could easily be replaced by @Component
13
+ public final class BeanWithoutProxyConfig {
14
+
15
+ @ Bean ("john" )
16
+ Person getPerson (Name name ) {
17
+ return new Person (name );
18
+ }
19
+
20
+ @ Bean
21
+ Name getName () {
22
+ return new Name ("John" );
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ ## Bean configuration's lite vs full mode
2
+
3
+ ` @Configuration ` leverages cglib's proxy to ensure that the underlying beans declared in the annotated class are unique
4
+ (if theis scope is defaulted to singleton). That is the reason why classes stereotyped with ` @Configuration ` aren't allowed
5
+ to be ` final ` .
6
+
7
+ That said, ** full mode** is when ` @Configuration ` along with ` cglib proxy ` are used.
8
+
9
+ Conversely, beans in ** lite mode** can be defined in any other stereotype (including none and ` @Configuration(proxyBeanMethods = false) ` )
10
+ that can incidentally be a final class.
11
+
12
+ ⚠️ Mind that bean method calls inside the declaring class do will create as many new instances as calls.
13
+
14
+ ``` java
15
+ import org.springframework.context.annotation.Bean ;
16
+
17
+ class LiteModeConfig {
18
+
19
+ @Bean
20
+ Foo foo () {
21
+ return new Foo (foo1()); // This will create a new instance of foo1
22
+ }
23
+
24
+ @Bean
25
+ Foo foo1 () {
26
+ return new Foo1 ();
27
+ }
28
+ }
29
+ ```
30
+
31
+ thus, the correct way yo handle with this would be to leverage parameter injection:
32
+
33
+ ``` java
34
+ import org.springframework.context.annotation.Configuration ;
35
+
36
+ @Configuration (proxyBeanMethods = false )
37
+ final class LiteModeConfig {
38
+
39
+ @Bean
40
+ Foo foo (Foo1 foo1 ) {
41
+ return new Foo (foo1); // using unique instance of foo1
42
+ }
43
+
44
+ @Bean
45
+ Foo foo1 () {
46
+ return new Foo1 ();
47
+ }
48
+ }
49
+ ```
Original file line number Diff line number Diff line change 1
1
package com .ftm .vcp .beanmode ;
2
2
3
3
import com .ftm .vcp .beanmode .config .BeanFullModeConfig ;
4
+ import com .ftm .vcp .beanmode .config .BeanWithoutProxyConfig ;
4
5
import com .ftm .vcp .beanmode .config .BeanLiteModeConfig ;
5
6
import com .ftm .vcp .beanmode .model .Name ;
6
7
import com .ftm .vcp .beanmode .model .Person ;
15
16
class BeanModeTest {
16
17
17
18
@ Test
18
- void should_load_new_instances_when_not_using_proper_configuration_annotation () {
19
+ void should_load_new_instances_when_using_lite_mode () {
19
20
// Given
20
21
final var applicationContext = new AnnotationConfigApplicationContext (BeanLiteModeConfig .class );
21
22
@@ -28,7 +29,7 @@ void should_load_new_instances_when_not_using_proper_configuration_annotation()
28
29
}
29
30
30
31
@ Test
31
- void should_let_spring_create_singleton_beans () {
32
+ void should_let_spring_create_singleton_beans_when_using_full_mode () {
32
33
// Given
33
34
final var applicationContext = new AnnotationConfigApplicationContext (BeanFullModeConfig .class );
34
35
@@ -39,4 +40,18 @@ void should_let_spring_create_singleton_beans() {
39
40
// Then
40
41
then (person .name ()).isSameAs (name );
41
42
}
43
+
44
+ @ Test
45
+ void should_still_let_spring_create_singleton_beans_when_using_full_mode_without_cglib_proxy () {
46
+ // Given
47
+ final var applicationContext = new AnnotationConfigApplicationContext (BeanWithoutProxyConfig .class );
48
+
49
+ // When
50
+ final var person = applicationContext .getBean (Person .class );
51
+ final var name = applicationContext .getBean (Name .class );
52
+
53
+ // Then
54
+ then (person .name ()).isSameAs (name );
55
+ }
56
+
42
57
}
You can’t perform that action at this time.
0 commit comments