File tree 7 files changed +176
-0
lines changed
7 files changed +176
-0
lines changed Original file line number Diff line number Diff line change
1
+ package common ;
2
+
3
+ public class SpellChecker {
4
+ public SpellChecker () {
5
+ System .out .println ("Inside SpellChecker constructor." );
6
+ }
7
+
8
+ public void checkSpelling () {
9
+ System .out .println ("Inside checkSpelling." );
10
+ }
11
+ }
Original file line number Diff line number Diff line change
1
+ package common ;
2
+
3
+ import lombok .Getter ;
4
+
5
+ public class TextEditor {
6
+ @ Getter
7
+ private SpellChecker spellChecker ;
8
+
9
+ // I have to comment this out when using inner bean method to inject
10
+ // dependency
11
+ public TextEditor (SpellChecker spellChecker ) {
12
+ System .out .println ("Inside TextEditor constructor." );
13
+ this .spellChecker = spellChecker ;
14
+ }
15
+
16
+ public void spellCheck () {
17
+ spellChecker .checkSpelling ();
18
+ }
19
+
20
+ // I have to comment out the setter method if I'm using constructor based
21
+ // dependency injection
22
+ // a setter method to inject the dependency.
23
+ // public void setSpellChecker(SpellChecker spellChecker) {
24
+ // System.out.println("Inside setSpellChecker.");
25
+ // this.spellChecker = spellChecker;
26
+ // }
27
+
28
+ // a getter method to return spellChecker
29
+ // public SpellChecker getSpellChecker() {
30
+ // return spellChecker;
31
+ // }
32
+
33
+ public void init () {
34
+ System .out .println ("Inside init() method." );
35
+ }
36
+
37
+ public void cleanup () {
38
+ System .out .println ("Inside cleanup() method." );
39
+ }
40
+ }
Original file line number Diff line number Diff line change
1
+ package spring_import_annotation ;
2
+
3
+ /** This is a class used for MainApp to demo @Import annotation. */
4
+ public class App1 {
5
+ public App1 () {
6
+ setup ();
7
+ work ();
8
+ cleanup ();
9
+ }
10
+
11
+ private void cleanup () {
12
+ System .out .println ("This is App1: I'm cleaning myself up!" );
13
+ }
14
+
15
+ private void work () {
16
+ System .out .println ("This is App1: I'm working now!" );
17
+ }
18
+
19
+ private void setup () {
20
+ System .out .println ("This is App1: I'm setting myself up!" );
21
+ }
22
+
23
+ public void doCooler () {
24
+ System .out .println ("This is App1: doing something cool!" );
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ package spring_import_annotation ;
2
+
3
+ /** This is a class used for MainApp to demo @Import annotation.*/
4
+ import org .springframework .context .annotation .Bean ;
5
+ import org .springframework .context .annotation .Configuration ;
6
+
7
+ @ Configuration
8
+ public class App1Config {
9
+
10
+ @ Bean
11
+ public App1 app1 () {
12
+ return new App1 ();
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ package spring_import_annotation ;
2
+
3
+ /** This is a class used for MainApp to demo @Import annotation. */
4
+ public class App2 {
5
+ public App2 () {
6
+ setup ();
7
+ work ();
8
+ cleanup ();
9
+ }
10
+
11
+ private void cleanup () {
12
+ System .out .println ("This is App2: I'm cleaning myself up!" );
13
+ }
14
+
15
+ private void work () {
16
+ System .out .println ("This is App2: I'm working now!" );
17
+ }
18
+
19
+ private void setup () {
20
+ System .out .println ("This is App2: I'm setting myself up!" );
21
+ }
22
+
23
+ public void doCool () {
24
+ System .out .println ("This is App2: doing something cool!" );
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ package spring_import_annotation ;
2
+
3
+ /** This is a class used for MainApp to demo @Import annotation.*/
4
+ import org .springframework .context .annotation .Bean ;
5
+ import org .springframework .context .annotation .Configuration ;
6
+ import org .springframework .context .annotation .Import ;
7
+
8
+ import common .TextEditor ;
9
+
10
+
11
+ @ Configuration
12
+ @ Import (App1Config .class )
13
+ public class App2Config {
14
+
15
+ @ Bean
16
+ public App1 app1 () {
17
+ return new App1 ();
18
+ }
19
+
20
+ @ Bean
21
+ public App2 app2 () {
22
+ return new App2 ();
23
+ }
24
+
25
+ @ Bean (initMethod = "init" , destroyMethod = "cleanup" )
26
+ public TextEditor textEditor () {
27
+ return new TextEditor (null );
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ package spring_import_annotation ;
2
+
3
+ /** This is a class to demo @Import annotation.
4
+ * Pay special attention to Config classes and normal classes, they are used
5
+ * in different places, don't misuse them.
6
+ *
7
+ * This pkg of application didn't use beans.xml at all! Cool!
8
+ * All annotation based configuration to use Spring!*/
9
+
10
+ import org .springframework .context .ApplicationContext ;
11
+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
12
+
13
+ import common .TextEditor ;
14
+
15
+ public class MainAppForDemoImportAnnotation {
16
+
17
+ public static void main (String [] args ) {
18
+ ApplicationContext ctx = new AnnotationConfigApplicationContext (App2Config .class );
19
+ // now both beans A and B will be available...
20
+ App1 app1 = ctx .getBean (App1 .class );
21
+ App2 app2 = ctx .getBean (App2 .class );
22
+ app2 .doCool ();
23
+ app1 .doCooler ();
24
+
25
+ common .TextEditor te = ctx .getBean (TextEditor .class );
26
+
27
+ System .out .println ("That's the end of the program." );
28
+ }
29
+
30
+ }
You can’t perform that action at this time.
0 commit comments