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

Commit0876516

Browse files
Spring Java annotation based DI
1 parentcdda6c2 commit0876516

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
packagespring_java_annotation_based_di;
2+
3+
importorg.springframework.beans.factory.annotation.Configurable;
4+
importorg.springframework.context.annotation.Bean;
5+
6+
importcommon.Gospel1;
7+
8+
@Configurable
9+
publicclassGospel1Config {
10+
11+
@Bean
12+
publicGospel1gospel1() {
13+
System.out.println("Gospel1Config.java is getting called now.");
14+
returnnewGospel1();
15+
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
packagespring_java_annotation_based_di;
2+
3+
importorg.springframework.context.annotation.Bean;
4+
importorg.springframework.context.annotation.Configuration;
5+
6+
importcommon.HelloWorld;
7+
8+
/**
9+
* This is to demo java-annotation-based Spring configuration/dependency
10+
* Injection.
11+
*/
12+
13+
@Configuration
14+
publicclassHelloWorldConfig {
15+
16+
@Bean
17+
publicHelloWorldhelloWorld() {
18+
System.out.println("HelloWorldConfig.java is getting called now.");
19+
returnnewHelloWorld();
20+
}
21+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
packagespring_java_annotation_based_di;
2+
3+
importorg.springframework.context.annotation.AnnotationConfigApplicationContext;
4+
5+
importcommon.Gospel1;
6+
importcommon.HelloWorld;
7+
importcommon.TextEditor;
8+
9+
/**
10+
* This is to demo java-annotation-based Spring configuration/dependency
11+
* Injection.
12+
*/
13+
publicclassMainAppForDemoJavaAnnotationBasedDI {
14+
15+
publicstaticvoidmain(String[]args) {
16+
AnnotationConfigApplicationContextctx =newAnnotationConfigApplicationContext();
17+
18+
ctx.register(Gospel1Config.class,TextEditorConfig.class,
19+
HelloWorldConfig.class);
20+
ctx.refresh();
21+
22+
Gospel1luke =ctx.getBean(Gospel1.class);
23+
luke.setReflection("Gospel 1 is the book of Luke!");
24+
System.out.println(luke.getReflection());
25+
26+
TextEditorte =ctx.getBean(TextEditor.class);
27+
te.spellCheck();
28+
29+
HelloWorldhw =ctx.getBean(HelloWorld.class);
30+
31+
System.out.println("The program ends!");
32+
}
33+
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
packagespring_java_annotation_based_di;
2+
3+
importorg.springframework.beans.factory.annotation.Configurable;
4+
importorg.springframework.context.annotation.Bean;
5+
6+
importcommon.SpellChecker;
7+
importcommon.TextEditor;
8+
9+
@Configurable
10+
publicclassTextEditorConfig {
11+
12+
// @Bean
13+
// public TextEditor textEditor() {
14+
// return new TextEditor();
15+
// }
16+
17+
@Bean
18+
publicTextEditortextEditor() {
19+
System.out
20+
.println("TextEditorConfig.java is getting called now and it's inside textEditor() method now.");
21+
returnnewTextEditor(spellChecker());
22+
}
23+
24+
@Bean
25+
publicSpellCheckerspellChecker() {
26+
System.out
27+
.println("TextEditorConfig.java is getting called now and it's inside spellChecker() method now.");
28+
returnnewSpellChecker();
29+
}
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp