代码之家  ›  专栏  ›  技术社区  ›  crmepham

如何在Spring引导应用程序的主方法中正确初始化Bean?

  •  2
  • crmepham  · 技术社区  · 9 年前

    我有以下配置类:

    @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 附属国?

    3 回复  |  直到 9 年前
        1
  •  2
  •   VoShoo    9 年前

    如果SchedulerTriggerJpaDao类具有以下注释

    @Repository
    

    那么它应该被认为是一个豆子。

        2
  •  1
  •   Aleh Maksimovich    9 年前

    你的 SchedulerTriggerJpaDao 只是一个界面。你需要

    1. @Component
      (仅供参考) @Repository 和 @Service 自动将类标记为组件)

    2. 或者使用一些框架,根据您的接口为您生成Dao实现。E、 g.弹簧数据JPA( http://projects.spring.io/spring-data-jpa/

        3
  •  0
  •   NiVeR    9 年前

    问题是您正在返回一个 new SchedulerService的实例,它不由spring管理。您正在将该类注释为 @Service @Inject 和/或 @Autowire .

    推荐文章