Skip to content

Commit 3302266

Browse files
committed
https://jstobigdata.com/spring/spring-beanpostprocessor-to-customize-beans/
1 parent 370f77d commit 3302266

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<module>spring-tutorial-bom</module>
1414
<module>spring-core-ioc</module>
1515
<module>spring-bean-lifecycle</module>
16+
<module>spring-core-beanpostprocessor</module>
1617
</modules>
1718

1819
<properties>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
<parent>
6+
<artifactId>spring-framework-tutorial-parent</artifactId>
7+
<groupId>com.jstobigdata</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>spring-core-beanpostprocessor</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework</groupId>
17+
<artifactId>spring-core</artifactId>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>org.springframework</groupId>
22+
<artifactId>spring-context</artifactId>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>javax.annotation</groupId>
27+
<artifactId>javax.annotation-api</artifactId>
28+
</dependency>
29+
</dependencies>
30+
31+
<dependencyManagement>
32+
<dependencies>
33+
<dependency>
34+
<groupId>com.jstobigdata</groupId>
35+
<artifactId>spring-tutorial-boms</artifactId>
36+
<type>pom</type>
37+
<scope>import</scope>
38+
<version>1.0-SNAPSHOT</version>
39+
</dependency>
40+
</dependencies>
41+
</dependencyManagement>
42+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package core.bean.postprocessor;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
import javax.annotation.PostConstruct;
6+
import javax.annotation.PreDestroy;
7+
8+
@Component
9+
public class ExampleBean {
10+
11+
@PostConstruct
12+
public void initData() {
13+
System.out.println("Your custom initialization code goes here...");
14+
}
15+
16+
@PreDestroy
17+
public void clearData() {
18+
System.out.println("Your custom destroy code goes here...");
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package core.bean.postprocessor;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ComponentScan("core.bean.postprocessor")
8+
public class ExampleConfig {
9+
//Extra config goes
10+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package core.bean.postprocessor;
2+
3+
import org.springframework.context.ConfigurableApplicationContext;
4+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5+
6+
public class TestExampleBean {
7+
public static void main(String[] args) {
8+
9+
ConfigurableApplicationContext context
10+
= new AnnotationConfigApplicationContext(ExampleConfig.class);
11+
context.getBean(ExampleBean.class);
12+
13+
//This is important
14+
context.registerShutdownHook();
15+
}
16+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package core.bean.postprocessor;
2+
3+
import org.springframework.beans.BeansException;
4+
import org.springframework.beans.factory.config.BeanPostProcessor;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class TraceBeanPostProcessor implements BeanPostProcessor {
9+
10+
@Override
11+
public Object postProcessBeforeInitialization(Object bean, String beanName)
12+
throws BeansException {
13+
System.out.println("Inside postProcessBeforeInitialization - " + beanName);
14+
return bean;
15+
}
16+
17+
@Override
18+
public Object postProcessAfterInitialization(Object bean, String beanName)
19+
throws BeansException {
20+
System.out.println("Inside postProcessAfterInitialization - " + beanName);
21+
return bean;
22+
}
23+
}

0 commit comments

Comments
 (0)