8000 move Spring tutorials into a separate repo · fishercoder1534/SpringTutorials@2c8bae8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2c8bae8

Browse files
move Spring tutorials into a separate repo
1 parent bd006fd commit 2c8bae8

40 files changed

+934
-0
lines changed

src/common/Gospel1.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package common;
2+
3+
import lombok.Getter;
4+
import lombok.NoArgsConstructor;
5+
import lombok.Setter;
6+
7+
@NoArgsConstructor
8+
public class Gospel1 {
9+
// public Gospel1() {
10+
// }
11+
12+
// public String getReflection() {
13+
// return Reflection;
14+
// }
15+
//
16+
// public void setReflection(String reflection) {
17+
// Reflection = reflection;
18+
// }
19+
20+
@Setter @Getter
21+
private String Reflection;
22+
23+
}

src/common/HelloChina.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package common;
2+
3+
import lombok.Getter;
4+
import lombok.NoArgsConstructor;
5+
import lombok.Setter;
6+
7+
@NoArgsConstructor
8+
public class HelloChina {
9+
10+
// public HelloChina() {
11+
// }
12+
13+
@Setter @Getter//these Lombok annotations eventually work in my own Eclipse and project now, I can safely comment out/remove those boilerplate code! Cool!
14+
private String message;
15+
16+
// public void getMessage() {
17+
// System.out.println(message);
18+
// }
19+
20+
public void setMessage(String message) {
21+
this.message = message;
22+
}
23+
24+
// this init() method is required b/c I'm using HelloWorld as this
25+
// HelloChina's parent bean
26+
public void init() {
27+
System.out.println("Bean HelloChina is going through init.");
28+
}
29+
30+
// this destroy() method is required b/c I'm using HelloWorld as this
31+
// HelloChina's parent bean
32+
public void destroy() {
33+
System.out.println("Bean HelloChina will destroy now.");
34+
}
35+
36+
}

src/common/HelloWorld.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package common;
2+
3+
import lombok.Setter;
4+
5+
public class HelloWorld {
6+
@Setter
7+
private String message;
8+
9+
// public void setMessage(String message) {
10+
// this.message = message;
11+
// }
12+
13+
public void getMessage() {
14+
System.out.println("Your Message : " + message);
15+
}
16+
17+
public void init() {
18+
System.out.println("Bean HelloWorld is going through init.");
19+
}
20+
21+
public void destroy() {
22+
System.out.println("Bean HelloWorld will destroy now.");
23+
}
24+
}

src/common/SpellChecker.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}

src/common/TextEditor.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package pureXMLDependencyInjecttionExample;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
6+
7+
@ToString
8+
public class Career {
9+
@Getter
10+
@Setter
11+
private String jobType;
12+
13+
@Getter
14+
@Setter
15+
private int jobLevel;
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package pureXMLDependencyInjecttionExample;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
6+
7+
@ToString
8+
public class City {
9+
@Getter
10+
@Setter
11+
private String cityName;
12+
13+
@Getter
14+
@Setter
15+
private String area;
16+
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package pureXMLDependencyInjecttionExample;
2+
3+
import org.springframework.context.support.AbstractApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
6+
/**This app is running fine!
7+
* Remember to add apache.commson.logging and spring jars onto your classpath, otherwise the program won't run.
8+
* It helps me understand how @Resource annotation works! Cool!
9+
* Look at this project along with other two:
10+
* SpringPureXMLDependencyInjectionExample
11+
* Spring@RessourceAnnotationExample
12+
* to help me better understand the three possible ways to do DI using Spring.*/
13+
public class MainApp {
14+
15+
public static void main(String... args) {
16+
System.out.println("It started!");
17+
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
18+
Person p =(Person) context.getBean("person");
19+
20+
System.out.println(p.getCity().toString());
21+
System.out.println(p.getWife().toString());
22+
System.out.println(p.getCareer().toString());
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package pureXMLDependencyInjecttionExample;
2+
3+
import lombok.Setter;
4+
5+
public class Person {
6+
@Setter
7+
private City city;
8+
9+
@Setter
10+
private Wife wife;
11+
12+
@Setter
13+
private Career career;
14+
15+
public City getCity() {
16+
return city;
17+
}
18+
19+
public Wife getWife() {
20+
return wife;
21+
}
22+
23+
public Career getCareer() {
24+
return career;
25+
}
26+
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package pureXMLDependencyInjecttionExample;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
6+
7+
@ToString
8+
public class Wife {
9+
10+
@Getter
11+
@Setter
12+
private String name;
13+
14+
@Getter
15+
@Setter
16+
private String faith;
17+
18+
}

src/spring.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7+
http://www.springframework.org/schema/context
8+
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
9+
10+
<bean id="person" class="pureXMLDependencyInjecttionExample.Person">
11+
<property name="city" ref="home123"/>
12+
<property name="wife" ref="wife"/>
13+
<property name="career" ref="careerFuture"/>
14+
</bean>
15+
16+
<bean id="home123" class="pureXMLDependencyInjecttionExample.City">
17+
<property name="cityName" value="San Jose"/>
18+
<property name="area" value="NorCal"/>
19+
</bean>
20+
21+
<bean id="wife" class="pureXMLDependencyInjecttionExample.Wife">
22+
<property name="name" value="Sophie Yan"/>
23+
<property name="faith" value="Christian"/>
24+
</bean>
25+
26+
<bean id = "careerCurrent" class="pureXMLDependencyInjecttionExample.Career">
27+
<property name="jobType" value="SDE"/>
28+
<property name="jobLevel" value="4"/>
29+
</bean>
30+
<bean id="careerFuture" class="pureXMLDependencyInjecttionExample.Career">
31+
<property name="jobType" value="SDE"/>
32+
<property name="jobLevel" value="5"/>
33+
</bean>
34+
35+
</beans>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<beans xmlns="http://www.springframework.org/schema/beans"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:context="http://www.springframework.org/schema/context"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
8+
http://www.springframework.org/schema/context
9+
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
10+
11+
<context:annotation-config/>
12+
13+
<bean id="helloWorld"
14+
class="spring_JSR_250_annotations.HelloWorld"
15+
init-method="init" destroy-method="destroy">
16+
<property name="message" value="Hello World from 2015/07/30!"/>
17+
</bean>
18+
19+
</beans>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package spring_JSR_250_annotations;
2+
3+
import javax.annotation.PostConstruct;
4+
import javax.annotation.PreDestroy;
5+
6+
import lombok.Getter;
7+
import lombok.Setter;
8+
9+
public class HelloWorld {
10+
@Setter @Getter
11+
private String message;
12+
13+
// public void setMessage(String message) {
14+
// this.message = message;
15+
// }
16+
//
17+
// public String getMessage() {
18+
// System.out.println("Your Message : " + message);
19+
// return message;
20+
// }
21+
22+
@PostConstruct
23+
public void init() {
24+
System.out.println("Bean is going through init.");
25+
}
26+
27+
@PreDestroy
28+
public void destroy() {
29+
System.out.println("Bean will destroy now.");
30+
}
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package spring_JSR_250_annotations;
2+
3+
/**Following is the content of the MainApp.java file. Here you
4+
* need to register a shutdown hook registerShutdownHook() method that
5+
* is declared on the AbstractApplicationContext class.
6+
* This will ensures a graceful shutdown and calls the relevant destroy methods.*/
7+
8+
import org.springframework.context.support.AbstractApplicationContext;
9+
import org.springframework.context.support.ClassPathXmlApplicationContext;
10+
11+
public class MainApp {
12+
public static void main(String[] args) {
13+
14+
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
15+
"spring_JSR_250_annotations/Beans.xml");
16+
17+
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
18+
obj.getMessage();
19+
context.registerShutdownHook();
20+
21+
System.out.print("\nThe end!\n");
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package spring_bean_post_processor;
2+
3+
/**This MainApp is working fine.*/
4+
import org.springframework.context.support.AbstractApplicationContext;
5+
import org.springframework.context.support.ClassPathXmlApplicationContext;
6+
7+
public class MainAppDemoBeanPostProcessor {
8+
public static void main(String[] args) {
9+
10+
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
11+
"spring_bean_post_processor/beansForDemoBeanPostProcessor.xml");
12+
13+
common.HelloWorld obj = (common.HelloWorld) context.getBean("helloWorld");
14+
obj.getMessage();
15+
16+
common.HelloChina obj2 = (common.HelloChina) context.getBean("helloChina");
17+
obj2.setMessage("China is saying hello to the rest of the world!");
18+
// obj2.getMessage();
19+
20+
context.registerShutdownHook();
21+
22+
System.out.println("The program ended! Cool!");
23+
}
24+
}

0 commit comments

Comments
 (0)
0