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

Commitf9b5ffb

Browse files
Spring Bean post processor
1 parentcb03c35 commitf9b5ffb

File tree

6 files changed

+154
-0
lines changed

6 files changed

+154
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packagecommon;
2+
3+
importlombok.Getter;
4+
importlombok.NoArgsConstructor;
5+
importlombok.Setter;
6+
7+
@NoArgsConstructor
8+
publicclassGospel1 {
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+
privateStringReflection;
22+
23+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
packagecommon;
2+
3+
importlombok.Getter;
4+
importlombok.NoArgsConstructor;
5+
importlombok.Setter;
6+
7+
@NoArgsConstructor
8+
publicclassHelloChina {
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+
privateStringmessage;
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+
publicvoidinit() {
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+
publicvoiddestroy() {
33+
System.out.println("Bean HelloChina will destroy now.");
34+
}
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
packagecommon;
2+
3+
importlombok.Setter;
4+
5+
publicclassHelloWorld {
6+
@Setter
7+
privateStringmessage;
8+
9+
// public void setMessage(String message) {
10+
// this.message = message;
11+
// }
12+
13+
publicvoidgetMessage() {
14+
System.out.println("Your Message : " +message);
15+
}
16+
17+
publicvoidinit() {
18+
System.out.println("Bean HelloWorld is going through init.");
19+
}
20+
21+
publicvoiddestroy() {
22+
System.out.println("Bean HelloWorld will destroy now.");
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
packagespring_bean_post_processor;
2+
3+
/**This MainApp is working fine.*/
4+
importorg.springframework.context.support.AbstractApplicationContext;
5+
importorg.springframework.context.support.ClassPathXmlApplicationContext;
6+
7+
publicclassMainAppDemoBeanPostProcessor {
8+
publicstaticvoidmain(String[]args) {
9+
10+
AbstractApplicationContextcontext =newClassPathXmlApplicationContext(
11+
"spring_bean_post_processor/beansForDemoBeanPostProcessor.xml");
12+
13+
common.HelloWorldobj = (common.HelloWorld)context.getBean("helloWorld");
14+
obj.getMessage();
15+
16+
common.HelloChinaobj2 = (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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<beansxmlns="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+
<beanid="helloWorld"class="common.HelloWorld"
13+
init-method="init"destroy-method="destroy"lazy-init="true">
14+
<propertyname="message"
15+
value="Hello World from Steve Sun's very first own Spring project!" />
16+
</bean>
17+
18+
<beanid="helloChina"class="common.HelloChina"
19+
parent="helloWorld"lazy-init="true">
20+
<propertyname="message"value="Hello China! It is China this time!" />
21+
</bean>
22+
</beans>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packagespring_bean_post_processor;
2+
3+
importorg.springframework.beans.BeansException;
4+
importorg.springframework.beans.factory.config.BeanPostProcessor;
5+
6+
publicclassinitHelloWorldimplementsBeanPostProcessor {
7+
8+
publicinitHelloWorld() {
9+
// TODO Auto-generated constructor stub
10+
}
11+
12+
@Override
13+
publicObjectpostProcessAfterInitialization(Objectbean,StringbeanName)
14+
throwsBeansException {
15+
System.out.println("Before Initialization : " +beanName);
16+
returnbean;// you can return any other object as well
17+
}
18+
19+
@Override
20+
publicObjectpostProcessBeforeInitialization(Objectbean,StringbeanName)
21+
throwsBeansException {
22+
System.out.println("After Initialization : " +beanName);
23+
returnbean;// you can return any other object as well
24+
}
25+
26+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp