forked from Vip-Augus/spring-analysis-note
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2. 分析 beanFactory 的后处理器 完成,详细搜索 注释 6.
- Loading branch information
Showing
17 changed files
with
589 additions
and
9 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
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
423 changes: 423 additions & 0 deletions
423
spring-analysis-note/note/2019-06-16-spring-analysis-note-3.md
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions
30
spring-analysis-note/src/main/java/context/BeanFactoryPostProcessorBootstrap.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,30 @@ | ||
package context; | ||
|
||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.support.ClassPathXmlApplicationContext; | ||
|
||
/** | ||
* 注释 6.3 BeanFactory 后处理器的 demo | ||
* @author JingQ at 2019-06-14 | ||
*/ | ||
public class BeanFactoryPostProcessorBootstrap { | ||
|
||
public static void main(String[] args) { | ||
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("factory.bean/factory-post-processor.xml"); | ||
BeanFactoryPostProcessor beanFactoryPostProcessor = (BeanFactoryPostProcessor) context.getBean("carPostProcessor"); | ||
beanFactoryPostProcessor.postProcessBeanFactory(context.getBeanFactory()); | ||
// 输出 :Car{maxSpeed=0, brand='*****', price=10000.0},敏感词被替换了 | ||
System.out.println(context.getBean("car")); | ||
|
||
// 硬编码 后处理器执行时间 | ||
BeanFactoryPostProcessor hardCodeBeanFactoryPostProcessor = new HardCodeBeanFactoryPostProcessor(); | ||
context.addBeanFactoryPostProcessor(hardCodeBeanFactoryPostProcessor); | ||
// 更新上下文 | ||
context.refresh(); | ||
// 输出 : | ||
// Hard Code BeanFactory Post Processor execute time | ||
// Car{maxSpeed=0, brand='*****', price=10000.0} | ||
System.out.println(context.getBean("car")); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
spring-analysis-note/src/main/java/context/CarBeanFactoryPostProcessor.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,56 @@ | ||
package context; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.beans.factory.config.BeanDefinitionVisitor; | ||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.util.StringValueResolver; | ||
|
||
import java.util.Set; | ||
|
||
/** | ||
* @author JingQ at 2019-06-14 | ||
*/ | ||
public class CarBeanFactoryPostProcessor implements BeanFactoryPostProcessor { | ||
|
||
/** | ||
* 敏感词 | ||
*/ | ||
private Set<String> obscenties; | ||
|
||
@Override | ||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { | ||
// 从 beanFactory 中获取 bean 名字列表 | ||
String[] beanNames = beanFactory.getBeanDefinitionNames(); | ||
for (String beanName : beanNames) { | ||
BeanDefinition definition = beanFactory.getBeanDefinition(beanName); | ||
StringValueResolver valueResolver = strVal -> { | ||
if (isObscene(strVal)) return "*****"; | ||
return strVal; | ||
}; | ||
BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver); | ||
// 这一步才是真正处理 bean 的配置信息 | ||
visitor.visitBeanDefinition(definition); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* 判断 value 是否在敏感词列表中 | ||
* @param value 值 | ||
* @return boolean | ||
*/ | ||
private boolean isObscene(Object value) { | ||
String potentialObscenity = value.toString().toUpperCase(); | ||
return this.obscenties.contains(potentialObscenity); | ||
} | ||
|
||
public Set<String> getObscenties() { | ||
return obscenties; | ||
} | ||
|
||
public void setObscenties(Set<String> obscenties) { | ||
this.obscenties = obscenties; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
spring-analysis-note/src/main/java/context/HardCodeBeanFactoryPostProcessor.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,15 @@ | ||
package context; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
|
||
/** | ||
* @author JingQ at 2019-06-16 | ||
*/ | ||
public class HardCodeBeanFactoryPostProcessor implements BeanFactoryPostProcessor { | ||
@Override | ||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { | ||
System.out.println("Hard Code BeanFactory Post Processor execute time"); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
spring-analysis-note/src/main/resources/factory.bean/factory-post-processor.xml
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,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> | ||
|
||
<bean id="carPostProcessor" class="context.CarBeanFactoryPostProcessor"> | ||
<property name="obscenties"> | ||
<!--set 属性--> | ||
<set> | ||
<value>奔驰</value> | ||
<value>特斯拉</value> | ||
</set> | ||
</property> | ||
</bean> | ||
|
||
<bean id="car" class="base.factory.bean.Car"> | ||
<property name="price" value="10000"/> | ||
<property name="brand" value="奔驰"/> | ||
</bean> | ||
|
||
</beans> |
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
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
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
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