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

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

问题描述:

我有以下的配置类:如何在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依赖项?

+0

你是怎么定义'schedulerTriggerJpaDao'? – NiVeR

如果您SchedulerTriggerJpaDao类具有以下注释

@Repository 

那么它应该被视为一个bean。

+0

我已将'@ Repository'注释添加到'SchedulerTriggerJpaDao'接口。但我仍然在启动时遇到同样的错误。 – crm

+0

你可以尝试使用'@ ComponentScan'注释明确地添加你的'com.crm.dao'包。 – VoShoo

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

  1. 要么提供一个DAO实现自己与@Component
    将其标注为(FYI @Repository@Service自动标记类作为组件)

  2. ,或者使用一些框架,会为你生成一个DAO实现基于你的界面。例如。春季数据JPA(http://projects.spring.io/spring-data-jpa/

的问题是,你正在返回的SchedulerService,这不是弹簧管理的new实例。您将课程标注为@Service,但弹簧仅管理由@Inject和/或@Autowire注入的课程。