[英]Spring Boot App deployed in Tomcat : Quartz Scheduler bean is not visible from @Service
使用Spring Java Config,將War包裝好的應用程序加載到tomcat 7中時,調度程序bean不可見。
此配置正在其他測試應用程序中運行...我該怎么指出問題所在?
卡塔琳娜輸出:
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
配置:
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;
}
}
編輯:服務
@Service
public class SchedulerService extends AbstractService implements ISchedulerService {
@Autowired
private Scheduler scheduler;
//Blah...
}
另一個問題:我的戰爭是在戰爭項目中產生的。
Maven項目拱門:
-parent
+-war (just generates war)
+-web
我的war項目具有Web依賴項My @Configuration類位於Web項目內。
可能是問題所在嗎?
編輯2:顯然,是的!
考慮以下Maven項目:
parent
+-war-project (just generates war)
+-web-project (web app project itself)
由於WAR是從war項目生成的,因此我需要在war項目中的@SpringBootApplication類旁邊重新定義@Configuration類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.