forked from wuyouzhuguli/SpringAll
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
934ccbf
commit 9dc6a75
Showing
8 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
23 changes: 23 additions & 0 deletions
23
51.Spring-Bean-Lifecycle/src/main/java/cc/mrbird/DemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
23 changes: 23 additions & 0 deletions
23
51.Spring-Bean-Lifecycle/src/main/java/cc/mrbird/demo/config/MyBeanPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
51.Spring-Bean-Lifecycle/src/main/java/cc/mrbird/demo/config/WebConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
51.Spring-Bean-Lifecycle/src/main/java/cc/mrbird/demo/domain/Bird.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
51.Spring-Bean-Lifecycle/src/main/java/cc/mrbird/demo/domain/Fish.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
51.Spring-Bean-Lifecycle/src/main/java/cc/mrbird/demo/domain/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.