如何在Spring Boot应用程序的main方法中正确初始化Bean?

时间:2017-10-07 19:49:25

标签: java spring spring-boot dependency-injection initialization

我有以下配置类:

@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方法加载main bean。像这样:

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();

}

当我运行应用程序时,我收到以下错误:

  

线程“main”中的异常   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名为'schedulerService'的bean时出错:不满意   通过字段'schedulerTriggerJpaDao'表示的依赖关系;嵌套   例外是   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   'com.crm.dao.SchedulerTriggerJpaDao'类型的限定bean   可用:预计至少有1个符合autowire资格的bean   候选人。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}     在   org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)     在   org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)     在   org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)

我需要更改什么来正确初始化schedulerService bean,以便它还可以初始化schedulerTriggerJpaDao依赖项?

3 个答案:

答案 0 :(得分:2)

如果您的SchedulerTriggerJpaDao类具有以下注释

@Repository

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

答案 1 :(得分:1)

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

  1. 自己提供Dao实现并使用@Component添加注释 (仅供参考@Repository@Service自动将类标记为组件)

  2. 或使用一些框架,根据您的界面为您生成Dao实现。例如。 Spring Data JPA(http://projects.spring.io/spring-data-jpa/

答案 2 :(得分:0)

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