Spring Boot App deployed in Tomcat : Quartz Scheduler bean is not visible from @Service

落爺英雄遲暮 提交于 2020-01-07 03:08:14

问题


Using Spring Java Config, the scheduler bean is not visible when loading my war packaged app into a tomcat 7.

This configuration was working in another test app... How can I point whats wrong ?

Catalina output :

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.quartz.Scheduler fr.myapp.schdtool.service.SchedulerService.scheduler; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.quartz.Scheduler] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 90 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.quartz.Scheduler] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
    ... 92 more

Config :

package fr.myapp;

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.jpa.HibernatePersistenceProvider;
import org.postgresql.Driver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("fr.myapp")
public class WebAppConfig extends WebMvcConfigurerAdapter {
    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public DataSource dataSource() {
            SimpleDriverDataSource d = new SimpleDriverDataSource();
            d.setConnectionProperties(dProperties());
            d.setDriverClass(Driver.class);
            d.setUrl("jdbc:postgresql://localhost:5432/mydb");
            d.setUsername("BNF0016779");
            d.setPassword("");
            return d;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
            LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
            entityManagerFactoryBean.setDataSource(dataSource());
            entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
            entityManagerFactoryBean.setPackagesToScan("fr.myapp");

            entityManagerFactoryBean.setJpaProperties(hibProperties());

            return entityManagerFactoryBean;
    }

    private Properties hibProperties() {
            Properties properties = new Properties();
            properties.put("hibernate.hbm2ddl.auto", "update");
            properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
            properties.put("hibernate.show_sql", "false");
            return properties;        
    }

    private Properties dProperties() {
        Properties properties = new Properties();
        properties.put("spring.jpa.database", "POSTGRESQL");
        return properties;        
}   

    @Bean
    public SchedulerFactoryBean configureScheduler() {
        SchedulerFactoryBean f = new SchedulerFactoryBean();
        return f;
    }
}

EDIT : Service

@Service
public class SchedulerService extends AbstractService implements ISchedulerService {
    @Autowired
    private Scheduler scheduler;

//Blah...
}

Another question : My War is generated inside a war project.

Maven project arch :

-parent
+-war (just generates war)
+-web

My war project has a web dependency My @Configuration class is inside web project.

Could it be the problem ?

EDIT 2 : Apprently, yes !


回答1:


Considering the following maven projects :

parent
+-war-project (just generates war)
+-web-project (web app project itself)

Since the WAR is generated from war project, I need to re-define @Configuration classes in war-project, beside the @SpringBootApplication class.



来源:https://stackoverflow.com/questions/34227710/spring-boot-app-deployed-in-tomcat-quartz-scheduler-bean-is-not-visible-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!