这是我的配置类为什么spring不使用泛型限定符进行注入?
@Configuration
class Factories {
@Bean
Collection<Log> logs() {
List<Log> logs = new ArrayList<>();
for (int i = 0; i < 10; i++) { // gross example code
Log log = new Log();
log.setEntry(RandomStringUtils.randomAlphabetic(10));
logs.add(log);
}
return logs;
}
}
和这里的如何,我想自动装配它
@Service
@Transactional
public class LogService {
private final LogRepository repository;
private final ObjectFactory<Instant> now;
private final Collection<Log> logs;
@Autowired
LogService(final LogRepository repository, final ObjectFactory<Instant> now, final Collection<Log> logs) {
this.repository = repository;
this.now = now;
this.logs = logs;
}
,但我得到这个例外
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.xenoterracide.example.log.Log] found for dependency [collection of com.xenoterracide.example.log.Log]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1326)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1024)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:967)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 24 more
according to the documentation我认为这应该工作。 Here's my full code in case you need it。我误解了文档吗?
不要创建'Log'豆作为收藏。相反,使用自己的'@ Bean'注释创建每个'Log'。 '收集'注射应该工作。 –
RJo
@ajb [它们是](https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getGenericReturnType--)和Spring。 –
我不知道那个......我猜太多认真地对待“类型删除”了...... – ajb