Skip to content

Commit 0a1deb0

Browse files
create a maven project for my SpringTutorials
0 parents  commit 0a1deb0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+983
-0
lines changed

pom.xml

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.fishercoder</groupId>
8+
<artifactId>SpringTutorials</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
13+
<dependency>
14+
<groupId>org.projectlombok</groupId>
15+
<artifactId>lombok</artifactId>
16+
<version>1.16.12</version>
17+
<scope>provided</scope>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
<version>3.8.1</version>
24+
<scope>test</scope>
25+
</dependency>
26+
27+
<!-- spring-context which provides core functionality -->
28+
<dependency>
29+
<groupId>org.springframework</groupId>
30+
<artifactId>spring-context</artifactId>
31+
<version>4.1.6.RELEASE</version>
32+
</dependency>
33+
34+
<!-- The spring-aop module provides an AOP Alliance-compliant aspect-oriented
35+
programming implementation allowing you to define -->
36+
<dependency>
37+
<groupId>org.springframework</groupId>
38+
<artifactId>spring-aop</artifactId>
39+
<version>4.1.6.RELEASE</version>
40+
</dependency>
41+
42+
<!-- The spring-webmvc module (also known as the Web-Servlet module) contains
43+
Spring’s model-view-controller (MVC) and REST Web Services implementation
44+
for web applications -->
45+
<dependency>
46+
<groupId>org.springframework</groupId>
47+
<artifactId>spring-webmvc</artifactId>
48+
<version>4.1.6.RELEASE</version>
49+
</dependency>
50+
51+
<!-- The spring-web module provides basic web-oriented integration features
52+
such as multipart file upload functionality and the initialization of the
53+
IoC container using Servlet listeners and a web-oriented application context -->
54+
<dependency>
55+
<groupId>org.springframework</groupId>
56+
<artifactId>spring-web</artifactId>
57+
<version>4.1.6.RELEASE</version>
58+
</dependency>
59+
60+
</dependencies>
61+
62+
</project>

src/main/java/common/Gospel1.java

+23
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/main/java/common/HelloChina.java

+36
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/main/java/common/HelloWorld.java

+24
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+
}
+11
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/main/java/common/TextEditor.java

+40
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+
}
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+
}
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+
}
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+
}
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+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package spring_JSR_250_annotations;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
import javax.annotation.PostConstruct;
7+
import javax.annotation.PreDestroy;
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+
}
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+
"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+
}
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+
"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)