File tree Expand file tree Collapse file tree 2 files changed +38
-1
lines changed
springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/ioc Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments