Spring 4 bean通用泛型自动装配

时间:2014-11-14 15:02:10

标签: spring javabeans autowired

我通过Spring Boot 1.1.8使用Spring 4并创建了一个缓存一些数据的类。该类依赖于泛型工作,但我遇到了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;
    }
}

类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 / setter。其他更具体的实体类扩展了它。

我正在尝试将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定义,但我无法实现任何有用的东西。

1 个答案:

答案 0 :(得分:0)

由于BaseRepository也是通用类型,我认为您错过了在那里添加泛型类型。这应该有助于Spring找到一个合适的bean来注入:

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

这也应该使演员不再需要:

item = repo.findByName(name);
相关问题