我有以下配置类:
@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();
}
}
我希望能够加载
schedulerService
来自应用程序的bean
main
方法类似这样:
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();
}
当我运行应用程序时,出现以下错误:
组织。springframework。豆。工厂未满足的依赖异常:
创建名为“schedulerService”的bean时出错:未满足要求
例外情况是
组织。springframework。豆。工厂NoSuchBeanDefinitionException:否
“com”类型的限定bean。crm。道。SchedulerTriggerJpaDao'
可用:需要至少1个符合autowire条件的bean
候选人依赖项批注:
{@org.springframework.beans.factory.annotation.Autowired(必需=true)}
在
在
组织。springframework。豆。工厂注释。注入元数据。注入(InjectionMetadata.java:88)
组织。springframework。豆。工厂注释。AutowiredAnnotationBeanPostProcessor。后处理属性值(AutowiredAnnotationBeanPostProcessor.java:366)
在
组织。springframework。豆。工厂支持AbstractAutowireCapableBeanFactory。populateBean(AbstractAutowireCapableBeanFactory.java:1264)
在
组织。springframework。豆。工厂支持AbstractAutowireCapableBeanFactory。doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
在
为了正确初始化schedulerService bean,使其也可以初始化
schedulerTriggerJpaDao
附属国?