Skip to content

Commit 0876516

Browse files
Spring Java annotation based DI
1 parent cdda6c2 commit 0876516

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+
package spring_java_annotation_based_di;
2+
3+
import org.springframework.beans.factory.annotation.Configurable;
4+
import org.springframework.context.annotation.Bean;
5+
6+
import common.Gospel1;
7+
8+
@Configurable
9+
public class Gospel1Config {
10+
11+
@Bean
12+
public Gospel1 gospel1() {
13+
System.out.println("Gospel1Config.java is getting called now.");
14+
return new Gospel1();
15+
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package spring_java_annotation_based_di;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
import common.HelloWorld;
7+
8+
/**
9+
* This is to demo java-annotation-based Spring configuration/dependency
10+
* Injection.
11+
*/
12+
13+
@Configuration
14+
public class HelloWorldConfig {
15+
16+
@Bean
17+
public HelloWorld helloWorld() {
18+
System.out.println("HelloWorldConfig.java is getting called now.");
19+
return new HelloWorld();
20+
}
21+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package spring_java_annotation_based_di;
2+
3+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4+
5+
import common.Gospel1;
6+
import common.HelloWorld;
7+
import common.TextEditor;
8+
9+
/**
10+
* This is to demo java-annotation-based Spring configuration/dependency
11+
* Injection.
12+
*/
13+
public class MainAppForDemoJavaAnnotationBasedDI {
14+
15+
public static void main(String[] args) {
16+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
17+
18+
ctx.register(Gospel1Config.class, TextEditorConfig.class,
19+
HelloWorldConfig.class);
20+
ctx.refresh();
21+
22+
Gospel1 luke = ctx.getBean(Gospel1.class);
23+
luke.setReflection("Gospel 1 is the book of Luke!");
24+
System.out.println(luke.getReflection());
25+
26+
TextEditor te = ctx.getBean(TextEditor.class);
27+
te.spellCheck();
28+
29+
HelloWorld hw = 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+
package spring_java_annotation_based_di;
2+
3+
import org.springframework.beans.factory.annotation.Configurable;
4+
import org.springframework.context.annotation.Bean;
5+
6+
import common.SpellChecker;
7+
import common.TextEditor;
8+
9+
@Configurable
10+
public class TextEditorConfig {
11+
12+
// @Bean
13+
// public TextEditor textEditor() {
14+
// return new TextEditor();
15+
// }
16+
17+
@Bean
18+
public TextEditor textEditor() {
19+
System.out
20+
.println("TextEditorConfig.java is getting called now and it's inside textEditor() method now.");
21+
return new TextEditor(spellChecker());
22+
}
23+
24+
@Bean
25+
public SpellChecker spellChecker() {
26+
System.out
27+
.println("TextEditorConfig.java is getting called now and it's inside spellChecker() method now.");
28+
return new SpellChecker();
29+
}
30+
}

0 commit comments

Comments
 (0)