问题
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