Spring4bean与泛型的自动连接

Spring4bean与泛型的自动连接,spring,javabeans,autowired,Spring,Javabeans,Autowired,我通过SpringBoot1.1.8使用Spring4,并创建了一个类来缓存一些数据。这个类依赖于泛型来工作,但是我在Spring和将这个类作为另一个服务中的bean自动连接时遇到了问题 我会遇到如下错误: Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [orm.repository.BaseRepository] is define

我通过SpringBoot1.1.8使用Spring4,并创建了一个类来缓存一些数据。这个类依赖于泛型来工作,但是我在Spring和将这个类作为另一个服务中的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;
    }
}

我还没有找到太多关于创建使用泛型的bean的信息。我怀疑我需要在AppConfig中创建另一个@Bean定义,但我无法实现任何有效的方法。

因为
BaseRepository
也是一个泛型类型,我认为您没有在那里添加泛型类型。这将有助于Spring找到合适的bean进行注入:

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

为什么不干脆使用springs
@Cacheable
支持呢?只需添加一个注释和宾果,缓存…是的,谢谢你-我确实计划在将来使用它,但我的问题更多的是关于bean实例化,而不是缓存方面。
@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;
    }

    .
    .
    .
}
@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);
    }
}
public class CacheByName<TRepo extends BaseRepository<TItem, ? extends Serializable>, TItem extends BaseWithName>
item = repo.findByName(name);