Spring启动Bean中的自动装配服务实现失败

时间:2015-04-13 22:08:47

标签: spring spring-boot

我试图在我的一个bean中自动装配服务实现,但我不断获得NoSuchBeanDefinitionException。这是我的代码:

存储库:

@Repository
public interface GlobalPropertiesRepository extends BaseRepository<GlobalProperties, Long>{
}

服务:

public interface GlobalPropertiesService {
    GlobalProperties findOne(Long id);
}

Base Repository:

import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;
import java.io.Serializable;
import java.util.List;

@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends Repository<T, ID> {
    void delete(T deleted);
    List<T> findAll();
    List<T> findAll(Iterable<ID> ids);
    T findOne(ID id);
    T save(T persisted);
    <S extends T> S saveAndFlush(S entity);
}

服务Impl:

@Service("globalPropertiesService")
public class GlobalPropertiesServiceImpl implements GlobalPropertiesService{

    @Autowired
    GlobalPropertiesRepository globalPropertiesRepository;

    @Override
    public GlobalProperties findOne(Long id) {
        return globalPropertiesRepository.findOne(id);
    }

}

然后我在我的一个bean中自动执行实现,如下所示:

public class GlobalPropertiesLoader {

    @Autowired
    private GlobalPropertiesService globalPropertiesService;

    private GlobalProperties globalProperties;

    @PostConstruct
    public void init(){
        globalProperties = globalPropertiesService.findOne(1L);
    }

    public GlobalProperties getGlobalProperties(){
        return globalProperties;
    }           
}

最后,这是我的Configuration类:

@Configuration
public class AppServiceConfig {

    public AppServiceConfig() {
    }

    // Global properties
    @Bean(name="globalPropertiesLoader")
    public GlobalPropertiesLoader globalPropertiesLoader(){
        return new GlobalPropertiesLoader();
    }

}

这是我的SpringBoot类:

@SpringBootApplication
@ComponentScan(basePackages="...")
public class TrackingService {
    private static final Logger LOGGER = LoggerFactory.getLogger(TrackingService.class);

    static AnnotationConfigApplicationContext context;

    public static void main(String[] args) throws Exception {
        SpringApplication.run(TrackingService.class, args);

        ClassPathScanningCandidateComponentProvider provider =
                new ClassPathScanningCandidateComponentProvider(true);
            String basePackage = "...";
            Set<BeanDefinition> components = provider.findCandidateComponents(basePackage);
            for (BeanDefinition component : components) {
                LOGGER.info("Component: "+component.getBeanClassName());       
            }

            context = new AnnotationConfigApplicationContext();
            context.refresh();
            context.close();

    }

}

现在,当我尝试启动应用程序时,我的GlobalPropertiesLoader bean中出现以下错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [GlobalPropertiesService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

0 个答案:

没有答案