Spring Boot uygulamasında bir Hizmet sınıfı için @Autowired
ek açıklamasını kullanmaya çalışıyorum ancak No qualifying bean of type
istisnasını atmaya devam ediyor. Ancak servis sınıfını fasülye değiştirirsem iyi çalışır. Bu benim kodudur:Spring Boot Annotation @Outdoor of Service başarısız olur
package com.mypkg.domain;
@Service
public class GlobalPropertiesLoader {
@Autowired
private SampleService sampleService;
}
package com.mypkg.service;
@Service
public class SampleService{
}
Ve bu benim SpringBoot sınıftır: Bu çalıştırmayı denediğinizde
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();
}
}
, aşağıdaki özel durum alıyorum: Ben kaldırdığınızda
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)}
Ama SampleService sınıfından @Service
ek açıklaması ve AppConfig sınıfımda aşağıdaki gibi bir fasulye olarak ekleyin, iyi çalışıyor:
@Configuration
public class AppServiceConfig {
public AppServiceConfig() {
}
@Bean(name="sampleService")
public SampleService sampleService(){
return new SampleService();
}
}
Sınıflar farklı paketlerde. @ComponentScan
kullanmıyorum. Bunun yerine, bunu otomatik olarak yapan @SpringBootApplication
kullanıyorum. Ancak, ComponentScan ile de denedim ama bu yardımcı olmadı.
Burada yanlış olan ne yapıyorum?
"Uygulama" sınıfınızı ("ana" yöntemle) gösterebilir misiniz? –
Sorumu ana sınıfın koduyla güncelledim. – drunkenfist
@drunkenfist Neden bağlamı kapatıyorsunuz? Bütün fasulye ritelerini yok edeceğini biliyorsun. – minion