메소드를 보여줍니다. 실행 흐름을 단계별로 탐색합시다
1. 프로그램 실행이 시작됩니다 (main ()) BeanNameAware
setBeanName()
메소드로 시작됩니다. 스프링 컨텍스트는 에서로드 구성을 사용하여 초기화됩니다. 그런 다음
2. 스프링 컨텍스트 초기화
3. Bean Creation (TenantConfig) main()
스프링은 AnnotationConfigApplicationContext 4. TENANTDATASOURCE 초기화 TenantConfig.class
TenantService
구현
<code class="language-java">public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TenantConfig.class); TenantService tenantService = context.getBean(TenantService.class); tenantService.processTenantData(); }</code>
5. TENANTSERVICE Bean Creation
스프링을 찾습니다. 생성자는
6. 임차인 서비스 검색 AnnotationConfigApplicationContext
@Configuration
in , TenantConfig
는 컨텍스트 ()에서 검색됩니다. 종속성으로 완전히 초기화되었습니다
7. Calling ProcessTenantData () basePackages
<code class="language-java">@Configuration @ComponentScan(basePackages = "org.example4") public class TenantConfig { @Bean(name = "tenantA-dataSource") public TenantDataSource tenantADataSource() { return new TenantDataSource(); } @Bean(name = "tenantB-dataSource") public TenantDataSource tenantBDataSource() { return new TenantDataSource(); } }</code>
및 는 호출되어 연결 세부 사항을 인쇄합니다 9. 프로그램 종료
완전한 콘솔 출력 : @Bean
tenantADataSource()
tenantBDataSource()
에 대한 자세한 내용은 스프링 프레임 워크 문서를 참조하십시오. 이 예제는 TenantDataSource
가 스프링 컨테이너 내에 할당 된 이름을 알고 있어서이 이름을 기반으로 동적 구성을 가능하게하는 방법을 보여줍니다.
위 내용은 Spring- : SetBeanName ()-Beannameaware-BeanFactory의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!