File tree 6 files changed +154
-0
lines changed
spring_bean_post_processor
6 files changed +154
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ xsi:schemaLocation=" http://www.springframework.org/schema/beans
6
+ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" >
7
+
8
+ <!-- I have another class called "initHelloWorld.java" which has init and
9
+ destroy methods, so that I could have these properties set for this helloWolrd
10
+ bean, but since the child bean "helloChina" doesn't have this class and init/destroy
11
+ methods, Spring won't work if I want to init helloChina bean. -->
12
+ <bean id =" helloWorld" class =" common.HelloWorld"
13
+ init-method =" init" destroy-method =" destroy" lazy-init =" true" >
14
+ <property name =" message"
15
+ value=" Hello World from Steve Sun's very first own Spring project!" />
16
+ </bean >
17
+
18
+ <bean id =" helloChina" class =" common.HelloChina"
19
+ parent=" helloWorld" lazy-init =" true" >
20
+ <property name =" message" value =" Hello China! It is China this time!" />
21
+ </bean >
22
+ </beans >
Original file line number Diff line number Diff line change
1
+ package spring_bean_post_processor ;
2
+
3
+ import org .springframework .beans .BeansException ;
4
+ import org .springframework .beans .factory .config .BeanPostProcessor ;
5
+
6
+ public class initHelloWorld implements BeanPostProcessor {
7
+
8
+ public initHelloWorld () {
9
+ // TODO Auto-generated constructor stub
10
+ }
11
+
12
+ @ Override
13
+ public Object postProcessAfterInitialization (Object bean , String beanName )
14
+ throws BeansException {
15
+ System .out .println ("Before Initialization : " + beanName );
16
+ return bean ; // you can return any other object as well
17
+ }
18
+
19
+ @ Override
20
+ public Object postProcessBeforeInitialization (Object bean , String beanName )
21
+ throws BeansException {
22
+ System .out .println ("After Initialization : " + beanName );
23
+ return bean ; // you can return any other object as well
24
+ }
25
+
26
+ }
You can’t perform that action at this time.
0 commit comments