Spring @Autowired注解使用
@Autowired 自动装载
@Autowired
注解可以使用的地方很多,比如常见的setter方法,constructors方法,普通方法等,一下逐一罗列并举例说明
1. Constructors 中使用
@Service
public class MovieService {
private static final Logger logger = LoggerFactory.getLogger(MovieService.class);
private final MovieRepository movieRepository;
// 构造方法注入
@Autowired public MovieService(MovieRepository movieRepository) {
this.movieRepository = movieRepository;
}
public void printName() {
String name = this.movieRepository.getMovieName();
logger.info("Movie Name: {}",name);
}
}
从Spring4.3开始如果类仅有一个构造函数,可以不使用
@Autowired
来标记构造函数,但是如果类中有几个构造函数,则至少需要有一个构造函数被@Autowired
标记,来告诉IoC容器使用那个构造函数进行初始化。
2. 传统的setter方法中使用
@Service
public class MovieService {
private static final Logger logger = LoggerFactory.getLogger(MovieService.class);
private MovieRepository movieRepository;
public void printName() {
String name = this.movieRepository.getMovieName();
logger.info("Movie Name: {}",name);
}
// setter 方法注入
@Autowired public void setMovieRepository(MovieRepository movieRepository) {
this.movieRepository = movieRepository;
}
}
3. 任意方法上使用
@Service
public class MovieService {
private static final Logger logger = LoggerFactory.getLogger(MovieService.class);
private MovieRepository movieRepository;
private MovieCatalogRepository movieCatalog;
public void printName() {
String name = this.movieRepository.getMovieName();
String catalog = this.movieCatalog.getCatalog();
logger.info("Movie Name: {},Catalog: {}", new Object[] { name, catalog });
}
// 任意方法名称,和多个参数的注入
@Autowired public void prepare(MovieRepository movieRepository, MovieCatalogRepository movieCatalog) {
this.movieCatalog = movieCatalog;
this.movieRepository = movieRepository;
}
}
@Autowired
除了在setter和constructors上之外还可以在任意方法名称中使用,将依据传入参数进行注入
4. 注入一组特定类型的bean
public interface BasicRepository {
}
@Repository
public class MovieCatalogRepository implements BasicRepository{
public String getCatalog() {
return "Catalog..";
}
}
@Repository
public class MovieRepository implements BasicRepository{
public String getMovieName() {
return "King..";
}
}
@Service
public class MovieService {
private static final Logger logger = LoggerFactory.getLogger(MovieService.class);
// 以Set形式注入所有实现BasicRepository接口的bean
@Autowired private Set<BasicRepository> repositorySet;
// 以数组形式注入所有实现了BasicRepository接口的bean
@Autowired private BasicRepository[] repositoryArray;
// 以Map形式注入所有实现了BasicRepository接口的bean
@Autowired private Map<String, BasicRepository> repositoryMap;
// 注入spring上下文
@Autowired private ApplicationContext applicationContext;
public void printName() {
logger.info("打印Set中注入的bean...");
// 打印set中注入的bean
this.repositorySet.forEach(System.out::println);
logger.info("打印数组中注入的bean...");
// 打印数组中注入的bean
Arrays.stream(this.repositoryArray).forEach(System.out::println);
logger.info("打印Map中注入的bean...");
// 打印Map中注入的bean
this.repositoryMap.forEach((key, value) -> {
System.out.println(String.format("Key:%s,Value:%s", key, value));
});
logger.info("打印ApplicationContext中所有BasicRepository类型的bean");
this.applicationContext.getBeansOfType(BasicRepository.class).forEach((key, value) -> {
System.out.println(String.format("Key:%s,Value:%s", key, value));
});
}
}
2018-12-06 10:09:53.800 INFO 15940 --- [ main] o.s.a.MovieService : 打印Set中注入的bean...
org.samples.annotations.MovieCatalogRepository@2d83c5a5
org.samples.annotations.MovieRepository@48d7ad8b
2018-12-06 10:09:53.801 INFO 15940 --- [ main] o.s.a.MovieService : 打印数组中注入的bean...
org.samples.annotations.MovieCatalogRepository@2d83c5a5
org.samples.annotations.MovieRepository@48d7ad8b
2018-12-06 10:09:53.801 INFO 15940 --- [ main] o.s.a.MovieService : 打印Map中注入的bean...
Key:movieCatalogRepository,Value:org.samples.annotations.MovieCatalogRepository@2d83c5a5
Key:movieRepository,Value:org.samples.annotations.MovieRepository@48d7ad8b
2018-12-06 10:09:53.802 INFO 15940 --- [ main] o.s.a.MovieService : 打印ApplicationContext中所有BasicRepository类型的bean
Key:movieCatalogRepository,Value:org.samples.annotations.MovieCatalogRepository@2d83c5a5
Key:movieRepository,Value:org.samples.annotations.MovieRepository@48d7ad8b
当以Key-Value形式注入一组特定类型的bean时,key为bean的名称,value为bean的引用。
5. 结合@Primary、@Qualifier
注入特定类型的bean
如上例中所示,实现同一个接口的一组bean可以同时被注入,也可以被单个注入,在单个注入时可以结合@Primary或者@Qualifer注解使用。
- 使用
@Primary
注解默认注入beanPrimary用于指定多个相同类型bean中,在没有指定候选bean的情况下默认注入的bean
public interface BasicRepository {
}
@Repository
@Primary
public class MovieCatalogRepository implements BasicRepository{
public String getCatalog() {
return "Catalog..";
}
}
@Repository
public class MovieRepository implements BasicRepository{
public String getMovieName() {
return "King..";
}
}
@Service
public class MovieService {
private static final Logger logger = LoggerFactory.getLogger(MovieService.class);
/*
* 此处只指定了接口类型,设置@Primary以后,将默认注入@Primary标记的bean
* 若多个实现类中都没有标记@Primary则会抛出运行时异常[UnsatisfiedDependencyException]
*/
@Autowired private BasicRepository repository;
public void printName() {
logger.info("打印默认注入的bean");
logger.info("Bean Name:{},Bean HashCode:{}", this.repository.getClass().getName(),
this.repository.getClass().hashCode());
}
}
2018-12-06 11:14:09,960 INFO :-- [main .. ] o.s.a.MovieService 打印默认注入的bean
2018-12-06 11:14:09,961 INFO :-- [main .. ] o.s.a.MovieService Bean Name:org.samples.annotations.MovieCatalogRepository,Bean HashCode:1852661033
上面
MovieCatalogRepository
、MovieRepository
都是实现了BasicRepository
,在注入时如果采用接口注入的方式,我们可以使用@Primary
注解标明在默认情况下注入的bean,此例中为MovieCatalogRepository
。
- 使用
@Qualifier
来指出候选bean在接口注入中,若要注入指定的实现类,可以使用@Qualifier来指出候选bean的名称
public interface BasicRepository {
}
@Repository
@Primary
public class MovieCatalogRepository implements BasicRepository{
public String getCatalog() {
return "Catalog..";
}
}
@Repository
public class MovieRepository implements BasicRepository{
public String getMovieName() {
return "King..";
}
}
@Service
public class MovieService {
private static final Logger logger = LoggerFactory.getLogger(MovieService.class);
/*
* 此处使用@Qualifier注解来标记了默认候选bean为movieRepository,则在Ioc容器注入时将
* 注入名称为movieRepository的bean
* 虽然MovieCatalogRepository类标记了@Primary,但是这只是在没有指定候选bean时默认的选择
* 当指定了候选Bean时,会注入候选的Bean
*
*/
@Autowired @Qualifier("movieRepository") private BasicRepository repository;
public void printName() {
logger.info("打印默认注入的bean");
logger.info("Bean Name:{},Bean HashCode:{}", this.repository.getClass().getName(),
this.repository.getClass().hashCode());
}
}
2018-12-06 11:25:21,385 INFO :-- [main .. ] o.s.a.MovieService 打印默认注入的bean
2018-12-06 11:25:21,387 INFO :-- [main .. ] o.s.a.MovieService Bean Name:org.samples.annotations.MovieRepository,Bean HashCode:1027319653
6. 泛型注入
public interface Store<T> {
T get();
}
@Component
public class StringStore implements Store<String>{
@Override
public String get() {
return "String..";
}
}
@Component
public class IntegerStore implements Store<Integer>{
@Override
public Integer get() {
return 1;
}
}
@Component
public class StoreHandler {
private static final Logger logger= LoggerFactory.getLogger(StoreHandler.class);
@Autowired private Store<Integer> integerStore;
@Autowired private Store<String> stringStore;
public void test() {
logger.info("test bean type");
logger.info("IntegerStore get():{},StringStore get():{}",integerStore.get(),stringStore.get());
}
}
2018-12-06 13:29:05,424 INFO :-- [main .. ] o.s.a.StoreHandler test bean type
2018-12-06 13:29:05,424 INFO :-- [main .. ] o.s.a.StoreHandler IntegerStore get():1,StringStore get():String..