Skip to content

Commit ff3b0b0

Browse files
committed
add bean definition registry post processor sample in spring boot source code analysis
1 parent f3858d3 commit ff3b0b0

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.example.springboot.source.code.analysis.ioc.ann;
2+
3+
import com.example.springboot.source.code.analysis.ioc.pojo.Monkey;
4+
import org.springframework.beans.BeansException;
5+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
6+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
7+
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
8+
import org.springframework.beans.factory.support.RootBeanDefinition;
9+
import org.springframework.stereotype.Component;
10+
11+
/**
12+
* Created by ipipman on 2021/10/16.
13+
*
14+
* @version V1.0
15+
* @Package com.example.springboot.source.code.analysis.ioc.ann
16+
* @Description: (通过BeanDefinitionRegistryPostProcessor 进行Bean的注入)
17+
* @date 2021/10/16 3:18 下午
18+
*/
19+
@Component
20+
public class MyBeanDefinitionRegistry implements BeanDefinitionRegistryPostProcessor {
21+
22+
@Override
23+
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
24+
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
25+
rootBeanDefinition.setBeanClass(Monkey.class);
26+
beanDefinitionRegistry.registerBeanDefinition("monkey", rootBeanDefinition);
27+
}
28+
29+
@Override
30+
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
31+
32+
}
33+
}

springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/ioc/service/HelloService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ public class HelloService {
2323
* animal
2424
*/
2525
@Autowired
26-
@Qualifier("myCatFactoryBean") // 如果有多个同类型的Bean,那么用@Qualifier指定Bean的名称后进行注入
26+
// 如果有多个同类型的Bean,那么用@Qualifier指定Bean的名称后进行注入
27+
//@Qulifier("dog") // 通过@Configuration注解进行Bean注入
28+
//@Qualifier("myCatFactoryBean") // 通过FacotryBean<?>接口进行Bean注入
29+
//@Qualifier("monkey") // 通过BeanDefinitionRegistryPostProcessor接口进行Bean注入
30+
2731
private Animal animal;
2832

2933

0 commit comments

Comments
 (0)