Spring Boot Annotation @Autowired of Service失败 [英] Spring Boot Annotation @Autowired of Service fails
问题描述
我试图在Spring Boot应用程序的Service类中使用@Autowired
批注,但是它总是抛出No qualifying bean of type
异常.但是,如果我将服务类更改为Bean,则可以正常工作.这是我的代码:
I'm trying to use @Autowired
annotation for a Service class in Spring Boot application, but it keeps throwing No qualifying bean of type
exception. However, if I change the service class to a bean, then it works fine. This is my code:
package com.mypkg.domain;
@Service
public class GlobalPropertiesLoader {
@Autowired
private SampleService sampleService;
}
package com.mypkg.service;
@Service
public class SampleService{
}
这是我的SpringBoot类:
And this is my SpringBoot class:
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();
}
}
当我尝试运行此命令时,出现以下异常:
When I try to run this, I get the following exception:
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类中时,如下所示,它可以正常工作:
But when I remove the @Service
annotation from the SampleService class, and add it as a bean in my AppConfig class as below, it works fine:
@Configuration
public class AppServiceConfig {
public AppServiceConfig() {
}
@Bean(name="sampleService")
public SampleService sampleService(){
return new SampleService();
}
}
这些类位于不同的程序包中.我没有使用@ComponentScan
.相反,我使用的是@SpringBootApplication
,它会自动执行此操作.但是,我也尝试了ComponentScan,但这没有帮助.
The classes are in different packages. I am not using @ComponentScan
. Instead, I'm using @SpringBootApplication
which does that automatically. However, I tried with ComponentScan as well but that didn't help.
我在做什么错了?
推荐答案
您正在使用两种方法来构建Spring的bean.您只需要使用其中之一即可.
You are using two ways to build a Spring's bean. You just need to use one of them.
-
@Service
在POJO上
@Service
public class SampleService
配置类中的
@Bean
,必须用@Configuration
@Bean
in the configuration class which must be annotated with @Configuration
@Bean
public SampleService sampleService(){
return new SampleService();
}
@Autowired
由类类型解析,则不需要@Bean(name="sampleService")
,因为您只有一个具有该类类型的bean.
@Autowired
is resolved by class type then @Bean(name="sampleService")
is not needed is you have only one bean with that class type.
编辑01
打包com.example
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);
}
}
打包com.example.config
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();
}
}
打包com.example.repository
package com.example.repository
@Service
public class UserRepository {
@PostConstruct
public void init() {
System.out.println("repo from @service");
}
}
打包com.example.service
package com.example.service
@Service
public class UserService {
@PostConstruct
public void init() {
System.out.println("service from @service");
}
}
使用此代码,您可以注释AppConfig
类,然后您将看到如何自动连接UserRepository
和UserService
.在每个类中添加注释@Service
之后,取消注释AppConfig
和类也将自动连接.
Using this code you can comment the AppConfig
class and then you will see how UserRepository
and UserService
are autowired. After that comment @Service
in each class and un-comment AppConfig
and classes will be autowired too.
这篇关于Spring Boot Annotation @Autowired of Service失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!