1

私は、次のコンフィギュレーションクラスがあります。Spring Bootアプリケーションのmainメソッドで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 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(); 

} 

私はアプリケーションを実行すると、私は次の取得エラー:

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)

schedulerTriggerJpaDao依存関係を初期化できるように、schedulerService Beanを正しく初期化するために何を変更する必要がありますか?

+0

どのように 'schedulerTriggerJpaDao'を定義しましたか? – NiVeR

答えて

2

あなたSchedulerTriggerJpaDaoクラスには、次の注釈

@Repository 

はそれはBeanとして認識されるべきであるしている場合。

+0

'@ Repository'アノテーションを' SchedulerTriggerJpaDao'インターフェースに追加しました。しかし、私はまだ起動時に同じエラーが発生します。 – crm

+0

'@ ComponentScan'アノテーションを使って明示的に' com.crm.dao'パッケージを追加しようとすることができます。 – VoShoo

1

SchedulerTriggerJpaDaoは単なるインターフェイスです。あなたは

  • またはあなたのためのDAO実装を生成しますいくつかのフレームワークを使用する(FYI @Repository@Serviceが自動的にコンポーネントとしてクラスをマーク)

    1. を必要とするいずれかのDAO実装を自分で提供して@Component
      でそれに注釈を付けますあなたのインターフェイスに基づいて。例えば。春データJPA(http://projects.spring.io/spring-data-jpa/

  • 0

    問題は、スプリングによって管理されていないSchedulerServiceのnewインスタンスを返すされていることです。クラスには@Serviceと注釈を付けていますが、スプリングは@Injectおよび/または@Autowireによって注入されたもののみを管理しています。

    関連する問題