Skip to content

Commit

Permalink
feat: 添加 beanfactoryaware 扩展点
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyueyi committed Feb 13, 2023
1 parent 2bab5d0 commit cbb39f3
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public BasicService() {

public BasicService(DemoService2 demoService2) {
this.demoService2 = demoService2;
System.out.println("BasicServiec 使用demoService传参构建");
System.out.println("BasicService 使用demoService传参构建");
}

@PostConstruct
Expand Down
7 changes: 7 additions & 0 deletions spring-extention/105-bean-factory-aware/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.*
target/*
*.iml
!.gitignore

# 避免泄露邮箱信息,忽略邮箱认证信息
/src/main/resources/application-mail.yml
20 changes: 20 additions & 0 deletions spring-extention/105-bean-factory-aware/pom.xml
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>
4 changes: 4 additions & 0 deletions spring-extention/105-bean-factory-aware/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 1045-bean-factory-aware

bean的实例化之后,属性注入之前触发,通常用于缓存 BeanFactory, 或者在这里对bean进行特殊处理

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 -------------");
}
}
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;
}
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);
}
}
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();
}

}
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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
user:
data: propertyInjectData
1 change: 1 addition & 0 deletions spring-extention/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<module>102-bean-factory-post-processor</module>
<module>103-instantiation-bean-post-processor</module>
<module>104-smart-instantiation-bean-post-processor</module>
<module>105-bean-factory-aware</module>
</modules>

<properties>
Expand Down

0 comments on commit cbb39f3

Please sign in to comment.