-
Notifications
You must be signed in to change notification settings - Fork 328
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
Showing
11 changed files
with
183 additions
and
1 deletion.
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
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,7 @@ | ||
.* | ||
target/* | ||
*.iml | ||
!.gitignore | ||
|
||
# 避免泄露邮箱信息,忽略邮箱认证信息 | ||
/src/main/resources/application-mail.yml |
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,20 @@ | ||
<?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"> | ||
<parent> | ||
<artifactId>spring-extention</artifactId> | ||
<groupId>com.git.hui.boot</groupId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>105-bean-factory-aware</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
</project> |
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,4 @@ | ||
## 1045-bean-factory-aware | ||
|
||
bean的实例化之后,属性注入之前触发,通常用于缓存 BeanFactory, 或者在这里对bean进行特殊处理 | ||
|
31 changes: 31 additions & 0 deletions
31
...-bean-factory-aware/src/main/java/com/git/hui/extention/beanfactoryaware/Application.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,31 @@ | ||
package com.git.hui.extention.beanfactoryaware; | ||
|
||
import com.git.hui.extention.beanfactoryaware.service.BeanCacheService; | ||
import com.git.hui.extention.beanfactoryaware.service.DemoService; | ||
import com.git.hui.extention.beanfactoryaware.service.TestService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* @author YiHui | ||
* @date 2023/2/13 | ||
*/ | ||
@SpringBootApplication | ||
public class Application implements ApplicationRunner { | ||
@Autowired | ||
private BeanCacheService beanCacheService; | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class); | ||
} | ||
|
||
@Override | ||
public void run(ApplicationArguments args) throws Exception { | ||
System.out.println(beanCacheService.findBean(DemoService.class).showDemo()); | ||
System.out.println(beanCacheService.findBean(TestService.class).showTest());; | ||
System.out.println("---------- over -------------"); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...ctory-aware/src/main/java/com/git/hui/extention/beanfactoryaware/config/MyAutoConfig.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,18 @@ | ||
package com.git.hui.extention.beanfactoryaware.config; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author YiHui | ||
* @date 2023/2/13 | ||
*/ | ||
@Component | ||
@ConfigurationProperties(prefix = "user") | ||
public class MyAutoConfig { | ||
@Getter | ||
@Setter | ||
private String data; | ||
} |
31 changes: 31 additions & 0 deletions
31
...-aware/src/main/java/com/git/hui/extention/beanfactoryaware/service/BeanCacheService.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,31 @@ | ||
package com.git.hui.extention.beanfactoryaware.service; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.beans.factory.BeanFactoryAware; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* @author YiHui | ||
* @date 2023/2/13 | ||
*/ | ||
@Service | ||
public class BeanCacheService implements BeanFactoryAware { | ||
private BeanFactory beanFactory; | ||
|
||
@Override | ||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { | ||
this.beanFactory = beanFactory; | ||
System.out.println("bean factory 初始化!"); | ||
|
||
DemoService demoService = this.beanFactory.getBean(DemoService.class); | ||
demoService.setPrefix("cache"); | ||
|
||
TestService testService = this.beanFactory.getBean(TestService.class); | ||
testService.setPrefix("cache"); | ||
} | ||
|
||
public <T> T findBean(Class<T> bean) { | ||
return beanFactory.getBean(bean); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...ctory-aware/src/main/java/com/git/hui/extention/beanfactoryaware/service/DemoService.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,34 @@ | ||
package com.git.hui.extention.beanfactoryaware.service; | ||
|
||
import com.git.hui.extention.beanfactoryaware.config.MyAutoConfig; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
/** | ||
* @author YiHui | ||
* @date 2023/2/13 | ||
*/ | ||
@Service | ||
public class DemoService { | ||
@Setter | ||
@Getter | ||
private String prefix; | ||
|
||
private MyAutoConfig config; | ||
|
||
@Autowired | ||
public void setConfig(MyAutoConfig config) { | ||
this.config = config; | ||
System.out.println("demo service 初始化data!"); | ||
} | ||
|
||
public String showDemo() { | ||
System.out.println("show demo!"); | ||
return prefix + "_demo:" + config.getData() + " at: " + LocalDateTime.now().toString(); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...ctory-aware/src/main/java/com/git/hui/extention/beanfactoryaware/service/TestService.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,34 @@ | ||
package com.git.hui.extention.beanfactoryaware.service; | ||
|
||
import com.git.hui.extention.beanfactoryaware.config.MyAutoConfig; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
/** | ||
* @author YiHui | ||
* @date 2023/2/13 | ||
*/ | ||
@Service | ||
public class TestService { | ||
@Setter | ||
@Getter | ||
private String prefix; | ||
|
||
private MyAutoConfig config; | ||
|
||
@Autowired | ||
public void setConfig(MyAutoConfig config) { | ||
this.config = config; | ||
System.out.println("demo service 初始化data!"); | ||
} | ||
|
||
|
||
public String showTest() { | ||
System.out.println("show test"); | ||
return prefix + "_test:" + config.getData() + " at: " + LocalDateTime.now(); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
spring-extention/105-bean-factory-aware/src/main/resources/application.yml
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,2 @@ | ||
user: | ||
data: propertyInjectData |
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