Spring Boot注释@Autowired of Service失败
我试图在Spring Boot应用程序中为服务类使用Spring Boot注释@Autowired of Service失败,spring,spring-mvc,annotations,spring-boot,Spring,Spring Mvc,Annotations,Spring Boot,我试图在Spring Boot应用程序中为服务类使用@Autowired注释,但它不断抛出类型的符合条件的bean异常。但是,如果我将服务类更改为bean,那么它可以正常工作。这是我的代码: package com.mypkg.domain; @Service public class GlobalPropertiesLoader { @Autowired private SampleService sampleService; } package com.m
@Autowired
注释,但它不断抛出类型的符合条件的bean
异常。但是,如果我将服务类更改为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,但没有帮助
我做错了什么?您正在使用两种方法来构建Spring的bean。你只需要使用其中一个
@Service
通过POJO
@Service
public class SampleService
@Bean
,必须用@configuration
@Bean
public SampleService sampleService(){
return new SampleService();
}
@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);
}
}
包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();
}
}
包com.example.repository
@Service
public class UserRepository {
@PostConstruct
public void init() {
System.out.println("repo from @service");
}
}
包com.example.service
@Service
public class UserService {
@PostConstruct
public void init() {
System.out.println("service from @service");
}
}
使用此代码,您可以对
AppConfig
类进行注释,然后您将看到UserRepository
和UserService
是如何自动连接的。在每个类中添加注释@Service
之后,取消注释AppConfig
,类也将自动连接。您可以显示您的应用程序
类(使用main
方法的类)吗?我已经用主类的代码更新了我的问题。@DuckenFist为什么要关闭上下文?你知道它会毁掉所有的豆子。我一开始无法复制这个问题,代码与你发布的代码几乎相同。然后在某个阶段,我意识到当我添加@Service
注释并点击快速导入时,我有两个选择。因此,第二个@服务
来自于我在pom中的某种依赖性,在本例中是spring boot starter球衣
,但可能还有其他的。这听起来不太可能,但是你能检查一下你的@服务的确切导入情况吗?我输入了一个类似的示例,所有这些都非常有效。如上所述,您可以检查导入的@Service注释的完全限定包名吗?我想您弄错了我的问题。我只使用其中一种方法。当我在POJO上用@Service
注释它时,它失败了。但是当我在我的配置类中使用@Bean
时,它是有效的。我想知道@服务
方式失败的原因。我已经更新了答案。静态注释ConfigApplicationContext上下文;使用了什么?spring boot管理生命周期,因此不确定为什么要使用context.refresh()和context.close()。对于我来说,@Service
方法不起作用。它只有在声明为@Bean
时才进行抓取。我使用的是spring boot 1.2.3.RELEASE。您使用的是哪个版本?您使用的是我的示例还是两者的混合?我有相同的问题,我使用的是spring boot 1.4.2.RELEASE。当我使用@Bean(name=“sampleService”)时它可以工作,但当我只使用@Bean时它不工作