spring:annotation @Primary @Qualifier

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class  {

private MovieCatalog[] movieCatalogs;

}

public class {
private Set<MovieCatalog> movieCatalogs;

public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
this.movieCatalogs = movieCatalogs;
}

}

public class {
private Map<String, MovieCatalog> movieCatalogs;

public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) {
this.movieCatalogs = movieCatalogs;
}
}

自动引入容器中的对象

You can also use @Autowired for interfaces that are well-known resolvable dependencies: BeanFactory , ApplicationContext , Environment , ResourceLoader ,
ApplicationEventPublisher , and MessageSource . These interfaces and their extended interfaces,such as ConfigurableApplicationContext or ResourcePatternResolver , are automatically resolved, with no special setup necessary

1
2
3
4
5
6
7
public class  {

private ApplicationContext context;
public () {
}

}

@Primary

1
2
3
4
5
6
7
8
9
@Configuration
public class MovieConfiguration {
@Bean
@Primary //如果只需要注入一个bean,这个优先注入
public MovieCatalog firstMovieCatalog() { ... }
@Bean
public MovieCatalog secondMovieCatalog() { ... }

}

@Qualifier