Closed Alice52 closed 3 years ago
configuration
@Configuration public class IocContainer implements ApplicationContextAware { private ApplicationContext applicationContext; public ApplicationContext getApplicationContext() { return this.applicationContext; } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } @Bean public Person person() { return new Person(); } }
junit test
@Slf4j public class IocContainerTest { private ApplicationContext applicationContext = new AnnotationConfigApplicationContext(IocContainer.class); @Test public void testGetIocContainer() { IocContainer contextBean = applicationContext.getBean(IocContainer.class); ApplicationContext applicationContext = contextBean.getApplicationContext(); Person person0 = applicationContext.getBean(Person.class); log.info(String.valueOf(person0)); Person person = applicationContext.getBean(Person.class); log.info(String.valueOf(person)); Assert.isTrue(person0 == person); Assert.isFalse(applicationContext == contextBean); } }
@Component/@Controller + @ComponentScan
@Component/@Controller
@Bean 将第三方包中的组件归置到 IOC 管理
@Bean
@Import
& 表示获取FactoryBean 本身
get ioc container
configuration
junit test
xxAware: 获取 spring 底层的组件, 只需要自定义的组件实现 xxAware 接口