Skip to content

Commit 1083b66

Browse files
committed
🗑️ 🧱 🩺 🗃️ 🎉 🥚 👔 ⚰️
1 parent 1114b08 commit 1083b66

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.springboot335.springboot335.config;
2+
3+
import org.springframework.beans.MutablePropertyValues;
4+
import org.springframework.beans.factory.config.BeanDefinition;
5+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
6+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
10+
/**
11+
* // 通过实现 BeanFactoryPostProcessor 接口,
12+
* // 可以在 Bean 实例化前修改其配置信息,适用于全局调整 Bean 的元数据,如更改属性值或替换占位符。
13+
*/
14+
15+
@Configuration
16+
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
17+
18+
@Override
19+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
20+
21+
BeanDefinition beanDefinition = beanFactory.getBeanDefinition("demoService");
22+
23+
24+
MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
25+
26+
propertyValues.addPropertyValue("a", "b");
27+
propertyValues.addPropertyValue("b", "a");
28+
29+
// todo zsx
30+
31+
32+
System.out.println("MyBeanFactoryPostProcessor.postProcessBeanFactory");
33+
}
34+
}

springboot3.0/springboot335/src/main/java/com/springboot335/springboot335/config/MyBeanPostProcessor.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
3535
Method[] declaredMethods = bean.getClass().getDeclaredMethods();
3636
for (Method declaredMethod : declaredMethods) {
3737
try {
38-
System.out.println("declaredMethod = " + declaredMethod);
39-
Object invoke = declaredMethod.invoke(bean, null);
4038

41-
System.out.println("invoke = " + invoke);
39+
if (declaredMethod.getName().equals("testGet")) {
40+
System.out.println("declaredMethod = " + declaredMethod);
41+
Object invoke = declaredMethod.invoke(bean, null);
42+
43+
System.out.println("invoke = " + invoke);
44+
}
45+
4246
} catch (IllegalAccessException e) {
4347
throw new RuntimeException(e);
4448
} catch (InvocationTargetException e) {

springboot3.0/springboot335/src/main/java/com/springboot335/springboot335/demo/DemoService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@
55
@Service
66
public class DemoService {
77

8+
private String a = "a";
9+
private String b = "b";
10+
811

912
public String testGet() {
13+
14+
System.out.println("a = " + a);
15+
System.out.println("b = " + b);
16+
1017
return "get";
1118
}
1219

1320

21+
public void setA(String a) {
22+
this.a = a;
23+
}
24+
25+
public void setB(String b) {
26+
this.b = b;
27+
}
1428
}

0 commit comments

Comments
 (0)