File tree Expand file tree Collapse file tree 6 files changed +112
-0
lines changed
spring-core-beanpostprocessor
src/main/java/core/bean/postprocessor Expand file tree Collapse file tree 6 files changed +112
-0
lines changed Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments