Tôi đang cố sử dụng chú thích @Autowired
cho lớp Dịch vụ trong ứng dụng Khởi động mùa xuân, nhưng nó vẫn giữ ngoại lệ No qualifying bean of type
. Tuy nhiên, nếu tôi thay đổi lớp dịch vụ thành một bean, thì nó hoạt động tốt. Đây là mã của tôi:Chú thích khởi động mùa xuân @Autowired of Service không hoạt động
package com.mypkg.domain;
@Service
public class GlobalPropertiesLoader {
@Autowired
private SampleService sampleService;
}
package com.mypkg.service;
@Service
public class SampleService{
}
Và đây là lớp SpringBoot tôi:
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();
}
}
Khi tôi cố gắng chạy này, tôi nhận được ngoại lệ sau đây:
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)}
Nhưng khi tôi loại bỏ chú thích @Service
từ lớp SampleService và thêm nó như là một bean trong lớp AppConfig của tôi như dưới đây, nó hoạt động tốt:
@Configuration
public class AppServiceConfig {
public AppServiceConfig() {
}
@Bean(name="sampleService")
public SampleService sampleService(){
return new SampleService();
}
}
Các lớp học có các gói khác nhau. Tôi không sử dụng @ComponentScan
. Thay vào đó, tôi đang sử dụng @SpringBootApplication
tự động thực hiện điều đó. Tuy nhiên, tôi đã thử với ComponentScan là tốt nhưng điều đó đã không giúp đỡ.
Tôi đang làm gì sai ở đây?
Bạn có thể hiển thị lớp 'Application' của mình (lớp có phương thức' main') không? –
Tôi đã cập nhật câu hỏi của mình với mã cho lớp chính. – drunkenfist
@drunkenfist Tại sao bạn đóng ngữ cảnh? Bạn biết nó sẽ phá hủy tất cả các nghi thức đậu. – minion