将<t>设置为Bean - 如何使用基于Spring Annotation的配置注入它

时间:2016-05-25 20:31:44

标签: spring spring-annotations

我有这个类本质上是一个基于配置文件配置的bean的配置类。它的“输出”是礼堂对象列表:

package ua.xxx.spring.hometask;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.stereotype.Component;
import ua.xxx.spring.hometask.domain.Auditorium;


import java.util.*;

@PropertySource("classpath:auditorium.properties")
public class AuditoriumConfig {

    @Value("${olkuszAuditorium.name}")
    private String auditorium1Name = null;

    @Value("#{'${olkuszAuditorium.vipSeats}'.split(',')}")
    private Set<Long> auditorium1VipSeats = null;

    @Value("${olkuszAuditorium.numberOfSeats}")
    private Long auditorium1NumberOfSeats = 0L;

    @Value("${krakowAuditorium.name}")
    private String auditorium2Name = "";

    @Value("#{'${krakowAuditorium.vipSeats}'.split(',')}")
    private Set<Long> auditorium2VipSeats = null;

    @Value("${krakowAuditorium.numberOfSeats}")
    private Long auditorium2NumberOfSeats = 0L;

    @Bean(name = "auditoriums")
    public Set<Auditorium> getAuditoriums() {
        Auditorium a1 = new Auditorium();
        a1.setName(auditorium1Name);
        a1.setNumberOfSeats(auditorium1NumberOfSeats);
        a1.setVipSeats(auditorium1VipSeats);

        Auditorium a2 = new Auditorium();
        a2.setName(auditorium2Name);
        a2.setNumberOfSeats(auditorium2NumberOfSeats);
        a2.setVipSeats(auditorium2VipSeats);

        return new HashSet<>(Arrays.asList(a1, a2));
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfig() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

现在我想在需要的地方使用Set<Auditorium>,例如:

@Bean
public AuditoriumService getAuditoriumService(@Qualifier(value = "auditoriums") Set<Auditorium> auditoriums) {
    return new InMemoryAuditoriumServiceImpl(auditoriums);
}

但我得到了这个例外:

Unsatisfied dependency expressed through constructor argument with index 0 of type [java.util.Set]: : No qualifying bean of type [ua.xxx.spring.hometask.domain.Auditorium] found for dependency [collection of ua.xxx.spring.hometask.domain.Auditorium]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=auditoriums)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ua.xxx.spring.hometask.domain.Auditorium] found for dependency [collection of ua.xxx.spring.hometask.domain.Auditorium]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=auditoriums)}

我的配置有什么问题?

这就是InMemory....类的样子:

@Component
public class InMemoryAuditoriumServiceImpl implements AuditoriumService {

    Set<Auditorium> config;

    public InMemoryAuditoriumServiceImpl(Set<Auditorium> config) {
        this.config = config;
    }
}

1 个答案:

答案 0 :(得分:3)

@Qualifier更改为@Resource,如:

@Bean
public AuditoriumService getAuditoriumService(@Resource(value = "auditoriums") Set<Auditorium> auditoriums) {
    return new InMemoryAuditoriumServiceImpl(auditoriums);
}