Spring引号注释@Autowired of Service失败

dru*_*ist 8 spring annotations spring-mvc spring-boot

我正在尝试@Autowired在Spring Boot应用程序中为Service类使用注释,但它不断抛出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{

}
Run Code Online (Sandbox Code Playgroud)

这是我的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();

    }

}
Run Code Online (Sandbox Code Playgroud)

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

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)}
Run Code Online (Sandbox Code Playgroud)

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

@Configuration
public class AppServiceConfig {

    public AppServiceConfig() {
    }

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

}
Run Code Online (Sandbox Code Playgroud)

这些课程分为不同的课程.我没用@ComponentScan.相反,我正在使用@SpringBootApplication哪个会自动执行此操作.但是,我尝试使用ComponentScan,但这没有用.

我在这做错了什么?

Edd*_*dez 15

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

  1. @Service通过POJO

    @Service
    public class SampleService
    
    Run Code Online (Sandbox Code Playgroud)
  2. 配置类中的@Bean必须使用@Configuration注释

    @Bean
    public SampleService sampleService(){
        return new SampleService();
    }
    
    Run Code Online (Sandbox Code Playgroud)

@Autowired由类类型解析然后@Bean(name="sampleService")不需要你只有一个具有该类类型的bean.

编辑01

包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);
}
}
Run Code Online (Sandbox Code Playgroud)

包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();
}
}
Run Code Online (Sandbox Code Playgroud)

package com.example.repository

@Service
public class UserRepository {

@PostConstruct
public void init() {
    System.out.println("repo from @service");
}
}
Run Code Online (Sandbox Code Playgroud)

包com.example.service

@Service
public class UserService {

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

}
Run Code Online (Sandbox Code Playgroud)

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