Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4a500dc

Browse files
spring import annotation
1 parent4978f0c commit4a500dc

File tree

7 files changed

+176
-0
lines changed

7 files changed

+176
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
packagecommon;
2+
3+
publicclassSpellChecker {
4+
publicSpellChecker() {
5+
System.out.println("Inside SpellChecker constructor.");
6+
}
7+
8+
publicvoidcheckSpelling() {
9+
System.out.println("Inside checkSpelling.");
10+
}
11+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
packagecommon;
2+
3+
importlombok.Getter;
4+
5+
publicclassTextEditor {
6+
@Getter
7+
privateSpellCheckerspellChecker;
8+
9+
// I have to comment this out when using inner bean method to inject
10+
// dependency
11+
publicTextEditor(SpellCheckerspellChecker) {
12+
System.out.println("Inside TextEditor constructor.");
13+
this.spellChecker =spellChecker;
14+
}
15+
16+
publicvoidspellCheck() {
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+
publicvoidinit() {
34+
System.out.println("Inside init() method.");
35+
}
36+
37+
publicvoidcleanup() {
38+
System.out.println("Inside cleanup() method.");
39+
}
40+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packagespring_import_annotation;
2+
3+
/** This is a class used for MainApp to demo @Import annotation. */
4+
publicclassApp1 {
5+
publicApp1() {
6+
setup();
7+
work();
8+
cleanup();
9+
}
10+
11+
privatevoidcleanup() {
12+
System.out.println("This is App1: I'm cleaning myself up!");
13+
}
14+
15+
privatevoidwork() {
16+
System.out.println("This is App1: I'm working now!");
17+
}
18+
19+
privatevoidsetup() {
20+
System.out.println("This is App1: I'm setting myself up!");
21+
}
22+
23+
publicvoiddoCooler() {
24+
System.out.println("This is App1: doing something cool!");
25+
}
26+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
packagespring_import_annotation;
2+
3+
/** This is a class used for MainApp to demo @Import annotation.*/
4+
importorg.springframework.context.annotation.Bean;
5+
importorg.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
publicclassApp1Config {
9+
10+
@Bean
11+
publicApp1app1() {
12+
returnnewApp1();
13+
}
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packagespring_import_annotation;
2+
3+
/** This is a class used for MainApp to demo @Import annotation. */
4+
publicclassApp2 {
5+
publicApp2() {
6+
setup();
7+
work();
8+
cleanup();
9+
}
10+
11+
privatevoidcleanup() {
12+
System.out.println("This is App2: I'm cleaning myself up!");
13+
}
14+
15+
privatevoidwork() {
16+
System.out.println("This is App2: I'm working now!");
17+
}
18+
19+
privatevoidsetup() {
20+
System.out.println("This is App2: I'm setting myself up!");
21+
}
22+
23+
publicvoiddoCool() {
24+
System.out.println("This is App2: doing something cool!");
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
packagespring_import_annotation;
2+
3+
/** This is a class used for MainApp to demo @Import annotation.*/
4+
importorg.springframework.context.annotation.Bean;
5+
importorg.springframework.context.annotation.Configuration;
6+
importorg.springframework.context.annotation.Import;
7+
8+
importcommon.TextEditor;
9+
10+
11+
@Configuration
12+
@Import(App1Config.class)
13+
publicclassApp2Config {
14+
15+
@Bean
16+
publicApp1app1() {
17+
returnnewApp1();
18+
}
19+
20+
@Bean
21+
publicApp2app2() {
22+
returnnewApp2();
23+
}
24+
25+
@Bean(initMethod ="init",destroyMethod ="cleanup")
26+
publicTextEditortextEditor() {
27+
returnnewTextEditor(null);
28+
}
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
packagespring_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+
importorg.springframework.context.ApplicationContext;
11+
importorg.springframework.context.annotation.AnnotationConfigApplicationContext;
12+
13+
importcommon.TextEditor;
14+
15+
publicclassMainAppForDemoImportAnnotation {
16+
17+
publicstaticvoidmain(String[]args) {
18+
ApplicationContextctx =newAnnotationConfigApplicationContext(App2Config.class);
19+
// now both beans A and B will be available...
20+
App1app1 =ctx.getBean(App1.class);
21+
App2app2 =ctx.getBean(App2.class);
22+
app2.doCool();
23+
app1.doCooler();
24+
25+
common.TextEditorte =ctx.getBean(TextEditor.class);
26+
27+
System.out.println("That's the end of the program.");
28+
}
29+
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp