我需要構建一個從數據庫生成報表的小型報表應用程序。目前只有2份報告每天和每週一次。好的夥計這是我想要做的,以便將來可以輕鬆擴展。動態構建彈簧集成路線
1)掃描報告目錄.properties文件,其內容是這樣的: report.name =每週管理報告 report.datasource = myDataSource report.bootstrap.sql = SELECT GETDATE() report.cron。表達式= 0/2 * * * MON-FRI report.service.activator.class = reporting.qvalent.JDBCReportExtractor [email protected]
2)對於每個文件,創建一個彈簧集成將執行以下操作的路由: a)根據引導查詢輪詢數據庫到cron表達式 B)調用激活劑類,將實際從數據源收集所有必要的數據,也許充實Thymeleaf方面 c)與模板合併thymeleaf上下文並通過電子郵件發送
我現在做的是這樣的:
public class ReportDefinitionLoader implements BeanDefinitionRegistryPostProcessor {
private ConfigurableListableBeanFactory beanFactory;
private void prepareBeansForReport(Resource resource) throws IOException {
try {
Properties props = PropertiesLoaderUtils.loadProperties(resource);
String reportName = props.getProperty(REPORT_NAME_PROPERTY);
String reportDatasource = props.getProperty(REPORT_DATASOURCE_PROPERTY);
String reportCronExpression = props.getProperty(REPORT_CRON_EXPRESSION_PROPERTY);
String reportBootstrapQuery = props.getProperty(REPORT_BOOTSTRAP_QUERY_PROPERTY);
CronTrigger cronTrigger = new CronTrigger(reportCronExpression);
beanFactory.registerSingleton(reportName + CRON_TRIGGER_BEAN_NAME, cronTrigger);
DataSource dataSource = (DataSource) beanFactory.getBean(reportDatasource);
beanFactory.getBean()
beanFactory.autowireBean(dataSource);
QueueChannel channel1 = new QueueChannel();
JdbcPollingChannelAdapter jdbcPollingChannelAdapter = new JdbcPollingChannelAdapter(dataSource, reportBootstrapQuery);
SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
TimerManagerTaskScheduler taskScheduler = new TimerManagerTaskScheduler();
taskScheduler.schedule(new Runnable() {
@Override
public void run() {
}
}, cronTrigger);
adapter.setOutputChannel(channel1);
adapter.setSource(jdbcPollingChannelAdapter);
adapter.setBeanFactory(beanFactory);
adapter.setTaskScheduler(taskScheduler);
adapter.start();
} catch (IOException e) {
LOG.error("Could not load properties from resource: " + resource.getFile().getName(), e);
}
}
}
但是則dataSource bean被具有如在XML
<bean id="myDataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
定義懸而未決屬性
您能否請指教如何從Spring上下文中獲取bean,以便在另一個類中使用它,該類也將放置在Spring上下文中?目前,我得到這些在啓動時:
2014-09-04 11:15:12,545 [WARN] - Could not load driverClass ${jdbc.driver}
java.lang.ClassNotFoundException: ${jdbc.driver}
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.mchange.v2.c3p0.DriverManagerDataSource.ensureDriverLoaded(DriverManagerDataSource.java:100)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:132)
當我使用XML配置,數據源屬性都解決好了,這個問題是不是配置不當PropertyPlaceholderConfigurer的感覺。
它*威力*是它似乎財產分辨率太早 - 從BeanDefinitionRegistryPostProcessor的Javadoc:允許 進一步bean定義登記前定期 的BeanFactoryPostProcessor檢測踢英寸 – Konstantin 2014-09-04 01:35:09