Spring引号注释@Autowired of Service失败

时间:2015-04-22 20:23:23

标签: spring spring-mvc annotations spring-boot

我正在尝试在Spring Boot应用程序中为Service类使用@Autowired注释,但它不断抛出No qualifying bean of type异常。但是,如果我将服务类更改为bean,那么它可以正常工作。这是我的代码:

package com.mypkg.domain;
@Service
public class GlobalPropertiesLoader {

    @Autowired
    private SampleService sampleService;        
}

package com.mypkg.service;
@Service
public class SampleService{

}

这是我的SpringBoot类:

package com.mypkg;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;

@SpringBootApplication
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableTransactionManagement
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);
        context = new AnnotationConfigApplicationContext();
        context.refresh();
        context.close();

    }

}

当我尝试运行它时,我得到以下异常:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.mypkg.service.SampleService] 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)}

但是当我从SampleService类中删除@Service注释,并将其作为bean添加到我的AppConfig类中时,如下所示,它可以正常工作:

@Configuration
public class AppServiceConfig {

    public AppServiceConfig() {
    }

    @Bean(name="sampleService")
    public SampleService sampleService(){
        return new SampleService();
    }

}

这些类位于不同的包中。我没有使用@ComponentScan。相反,我正在使用@SpringBootApplication自动执行此操作。但是,我尝试使用ComponentScan,但这没有帮助。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:15)

您正在使用两种方法来构建Spring的bean。你只需要使用其中一个。

  1. 通过POJO的@Service

    @Service
    public class SampleService
    
  2. 配置类中的@Bean必须使用@Configuration

    进行注释
    @Bean
    public SampleService sampleService(){
        return new SampleService();
    }
    
  3. @Autowired由类类型解析,然后不需要@Bean(name="sampleService")你是否只有一个具有该类类型的bean。

    编辑01

    package com.example

    @SpringBootApplication
    public class Application implements CommandLineRunner {
    
    public static void main(String... args) {
        SpringApplication.run(Application.class);
    }
    
    @Autowired
    private UserRepository userRepository;
    
    @Autowired
    private UserService userService;
    
    @Override
    public void run(String... strings) throws Exception {
        System.out.println("repo " + userRepository);
        System.out.println("serv " + userService);
    }
    }
    

    package com.example.config

    @Configuration
    public class AppConfig {
    
    @Bean
    public UserRepository userRepository() {
        System.out.println("repo from bean");
        return new UserRepository();
    }
    
    @Bean
    public UserService userService() {
        System.out.println("ser from bean");
        return new UserService();
    }
    }
    

    package com.example.repository

    @Service
    public class UserRepository {
    
    @PostConstruct
    public void init() {
        System.out.println("repo from @service");
    }
    }
    

    package com.example.service

    @Service
    public class UserService {
    
    @PostConstruct
    public void init() {
        System.out.println("service from @service");
    }
    
    }
    

    使用此代码,您可以注释AppConfig类,然后您将了解UserRepository和UserService如何自动装配。在每个类中注释@Service并取消注释AppConfig和类也将自动装配。