2014-11-14 40 views
0

我通過Spring Boot 1.1.8使用Spring 4,並創建了一個類來緩存一些數據。這個類依賴泛型來工作,但我在Spring中遇到了麻煩,並且在另一個服務中將這個類作爲bean自動裝配。彈簧4 bean與泛型的自動裝配

我得到的錯誤是這樣的:

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [orm.repository.BaseRepository] is defined: expected single matching bean but found 2: dataTypeRepository,propertyNameRepository 

類問題:

/** 
* The purpose of this class is to get data from a cache backed by a database 
* and if it does not exist to create it and insert into the database. 
*/ 
@Service 
public class CacheByName<TRepo extends BaseRepository, TItem extends BaseWithName> { 
    private final TRepo repo; 
    private final Class<TItem> itemClass; 
    private final Map<String, TItem> itemsCache; // TODO: change to better caching strategy 

    @Autowired 
    public CacheByName(TRepo repo, Class<TItem> itemClass) { 
     this.repo = repo; 
     this.itemClass = itemClass; 
     itemsCache = new HashMap(); 
    } 

    public TItem getCreateItem(String name) { 
     TItem item = null; 
     if(itemsCache.containsKey(name)) { 
      item = itemsCache.get(name); 
     } else { 
      // try and load from db 
      item = (TItem) repo.findByName(name); 
      if(item == null) { 
       try { 
        item = itemClass.newInstance(); 
        item.setName(name); 
        repo.saveAndFlush(item); 
       } catch (InstantiationException | IllegalAccessException ex) { 
        // TODO: log and handle better 
        return null; 
       } 

      } 
      itemsCache.put(name, item); 
     } 

     return item; 
    } 
} 

類BaseRepository擴展JpaRepository如下。其他實際存儲庫擴展了這一個。

@NoRepositoryBean 
public interface BaseRepository<T extends Object, ID extends Serializable> extends JpaRepository<T, ID> { 
    public T findByName(String name); 
} 

BaseWithName類是一個MappedSuperclass,它爲它定義一個name屬性和getters/setters。其他更具體的實體類擴展了這一點。

我想將CacheByName類注入到另一個服務,如下所示。請注意,我定義的實際存儲庫和實體類的構造函數仿製藥:

@Service 
public class DataImporter extends BaseImporter { 
    private static final Logger log = LoggerFactory.getLogger(PropertyImporter.class); 
    private final PropertyNameRepository propertyNameRepo; 
    private final CacheByName<DataTypeRepository, DataType> dataTypeCache; 


    @Autowired 
    public PropertyImporter(RestTemplate restTemplateD5, 
          CacheByName<DataTypeRepository, DataType> dataTypeCache) { 
     super(restTemplateD5); 
     this.dataTypeCache = dataTypeCache; 
    } 

    . 
    . 
    . 
} 

我AppConfig.java如下所示:

@Configuration 
@ComponentScan 
@EnableAutoConfiguration 
public class AppConfig { 
    @Value("${username}") 
    private String username; 
    @Value("${password}") 
    private String password; 

    @Bean 
    public RestTemplate restTemplateD5() { 
     return RestTemplateFactory.createWithHttpBasicAuth(username, password); 
    } 
} 

我一直沒能找到多少信息關於創建使用泛型的bean。我懷疑我需要在我的AppConfig中創建另一個@Bean定義,但我無法實現任何有效的工作。

+0

你爲什麼不乾脆usings彈簧'@ Cacheable'支持呢?只需添加註釋和賓果,緩存... – 2014-11-14 15:05:20

+0

是的謝謝 - 我打算將來使用它,但我的問題是關於bean實例化,而不是緩存方面。 – Hamish 2014-11-14 15:26:47

回答

0

由於BaseRepository也是一個泛型類型,我想你錯過了添加泛型類型。這應該有助於春天找到一個妥善的豆注入:

public class CacheByName<TRepo extends BaseRepository<TItem, ? extends Serializable>, TItem extends BaseWithName> 

這也使得中投不再需要:

item = repo.findByName(name);