Skip to content

Commit

Permalink
深入学习Spring Bean生命周期
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyouzhuguli committed Mar 13, 2019
1 parent 934ccbf commit 9dc6a75
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 0 deletions.
43 changes: 43 additions & 0 deletions 51.Spring-Bean-Lifecycle/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cc.mrbird</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cc.mrbird;

import cc.mrbird.demo.config.WebConfig;
import cc.mrbird.demo.domain.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);

// 返回 IOC 容器,使用注解配置,传入配置类
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(WebConfig.class);
System.out.println("容器创建完毕");
// User user = context.getBean(User.class);
// 关闭 IOC 容器
context.close();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cc.mrbird.demo.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable;

/**
* @author MrBird
*/
public class MyBeanPostProcessor implements BeanPostProcessor {

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName + " 初始化之前调用");
return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName + " 初始化之后调用");
return bean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cc.mrbird.demo.config;

import cc.mrbird.demo.domain.Bird;
import cc.mrbird.demo.domain.Fish;
import cc.mrbird.demo.domain.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
* @author MrBird
*/
@Configuration
public class WebConfig {

// @Scope("prototype")
// @Bean(initMethod = "init", destroyMethod = "destory")
// public User user() {
// return new User();
// }

// @Bean
// public Bird bird() {
// return new Bird();
// }

@Bean
public Fish fish(){
return new Fish();
}

@Bean
public MyBeanPostProcessor myBeanPostProcessor () {
return new MyBeanPostProcessor();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cc.mrbird.demo.domain;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

/**
* @author MrBird
*/
public class Bird implements InitializingBean, DisposableBean {

public Bird() {
System.out.println("调用无参构造器创建Bird");
}

@Override
public void destroy() {
System.out.println("销毁Bird");
}

@Override
public void afterPropertiesSet() {
System.out.println("初始化Bird");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cc.mrbird.demo.domain;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
* @author MrBird
*/
public class Fish {

public Fish() {
System.out.println("调用无参构造器创建Fish");
}

@PostConstruct
public void init() {
System.out.println("初始化Fish");
}

@PreDestroy
public void destory() {
System.out.println("销毁Fish");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cc.mrbird.demo.domain;

/**
* @author MrBird
*/
public class User {

public User() {
System.out.println("调用无参构造器创建User");
}

public void init() {
System.out.println("初始化User");
}

public void destory() {
System.out.println("销毁User");
}
}
Empty file.

0 comments on commit 9dc6a75

Please sign in to comment.