Spring4.0系列6-Generic Qualifier(泛型限定)

Spring4.0系列1-新特性

Spring4.0系列2-环境搭建

Spring4.0系列3-@RestController

Spring4.0系列4-Meta Annotation(元注解)

Spring4.0系列5-@Conditional 

Spring4.0系列6-Generic Qualifier(泛型限定)

Spring4.0系列7-Ordering Autowired Collections

Spring4.0系列8-Groovy DSL

Spring4.0系列9-websocket简单应用

更多正在编写中。。。

在Spring4.0里,泛型是可以用来决定哪一个bean需要依赖注入的(无论xml配置还是注解配置)。

用一个简单的例子解释。假设你有一个使用了泛型的DAO.

public class Dao<T> {
  ...
}

 现在创建两个实现类的bean:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class MyConfiguration {
 
  @Bean
  public Dao<Person> createPersonDao() {
    return new Dao<Person>();
  }
 
  @Bean
  public Dao<Organization> createOrganizationDao() {
    return new Dao<Organization>();
  }
}
在Spring4.0里,Spring容器可以使用泛型决定依赖注入。我们简单改变泛型的类型可自动注入所需要的bean。
@Autowired
private Dao<Person> dao;
 

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


猜你喜欢

转载自wiselyman.iteye.com/blog/2002519