【Spring源码解析】组件注册-注解注册bean
前言
接上一篇 Spring源码解析之xml注册bean
实现
- 注解配置
1 2 3 4 5 6 7 8
| @Configuration public class BeanConfig {
@Bean public Person person() { return new Person("config", 18); } }
|
- 测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class MainTest {
public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfig.class); Person bean = applicationContext.getBean(Person.class); System.out.println(bean);
String[] namesForType = applicationContext.getBeanNamesForType(Person.class); for (String name: namesForType) { System.out.println(name); }
} }
|
最后
本篇到此结束,欢迎大家关注公众号【当我遇上你】。