我有以下的配置類:如何在Spring Boot應用程序的主要方法中正確初始化Bean?
@Configuration
public class StartupConfig {
private final Logger log = LoggerFactory.getLogger(this.getClass().getSimpleName());
@PostConstruct
public void init() {
log.debug("Start up config initialized.");
}
@Bean
public SchedulerService schedulerService() {
return new SchedulerService();
}
}
我希望能夠從應用main
方法加載schedulerService
豆。事情是這樣的:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.crm.config.StartupConfig;
import com.crm.service.SchedulerService;
@SpringBootApplication
@EnableCaching
public class Server {
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(StartupConfig.class);
context.refresh();
SpringApplication.run(Server.class, args);
SchedulerService schedulerService = (SchedulerService) context.getBean("schedulerService");
schedulerService.start();
}
}
的schedulerService類有一個Autowired
依賴性:
@Service
@Transactional
public class SchedulerService {
@Autowired
private SchedulerTriggerJpaDao schedulerTriggerJpaDao;
...
這裏是SchedulerTriggerJpaDao
定義:
package com.crm.dao;
import java.util.Collection;
import javax.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import com.crm.entity.SchedulerTrigger;
@Transactional
public interface SchedulerTriggerJpaDao extends JpaRepository<SchedulerTrigger, Integer> {
public Collection<SchedulerTrigger> findByEnabledTrueAndDeletedFalse();
}
當我跑起來的應用程序,我得到以下錯誤:
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'schedulerService': Unsatisfied dependency expressed through field 'schedulerTriggerJpaDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.crm.dao.SchedulerTriggerJpaDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
我需要更改什麼才能正確初始化schedulerService bean,以便它還可以初始化schedulerTriggerJpaDao
依賴項?
你是怎麼定義'schedulerTriggerJpaDao'? – NiVeR