Skip to content

Commit

Permalink
添加OrgDataSourceCreateEvent,公司注册,给公司创建一个数据源,此事件为数据源创建完成后触发,通过此事件,程序员可以…
Browse files Browse the repository at this point in the history
…对数据源对象自定义修改其属性
  • Loading branch information
kevin committed Apr 25, 2018
1 parent b269ae7 commit a5cb152
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.malagu.multitenant.listener;

import javax.sql.DataSource;

import org.springframework.context.ApplicationEvent;

/**
* 公司注册,给公司创建一个数据源,此事件为数据源创建完成后触发,通过此事件,程序员可以对数据源对象自定义修改其属性
* @author Kevin Yang (mailto:[email protected])
* @since 2018年4月25日
*/
public class OrgDataSourceCreateEvent extends ApplicationEvent {

private static final long serialVersionUID = 1L;

public OrgDataSourceCreateEvent(DataSource dataSouce) {
super(dataSouce);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
import org.malagu.multitenant.domain.DataSourceInfo;
import org.malagu.multitenant.domain.Organization;
import org.malagu.multitenant.listener.DataSourceCreateListener;
import org.malagu.multitenant.listener.OrgDataSourceCreateEvent;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
Expand All @@ -28,7 +32,7 @@
* @since 2017年11月24日
*/
@Service
public class DataSourceServiceImpl implements DataSourceService, InitializingBean {
public class DataSourceServiceImpl implements DataSourceService, InitializingBean, ApplicationContextAware {

@Autowired
private DataSource dataSource;
Expand All @@ -47,6 +51,8 @@ public class DataSourceServiceImpl implements DataSourceService, InitializingBea
@Autowired
private DatabaseNameService databaseNameService;

private ApplicationContext applicationContext;

@Override
public DataSource getDataSource(Organization organization) {
return dataSourceMap.get(organization.getId());
Expand Down Expand Up @@ -84,6 +90,7 @@ public DataSource createDataSource(Organization organization) {
dataSouce = dataSourceLookup.getDataSource(dataSourceInfo.getJndiName());
}
dataSourceMap.put(organization.getId(), dataSouce);
this.applicationContext.publishEvent(new OrgDataSourceCreateEvent(dataSouce));
return dataSouce;
});

Expand Down Expand Up @@ -151,6 +158,12 @@ public void removeDataSource(Organization organization) {
public void clearDataSource() {
dataSourceMap.clear();
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;

}



Expand Down

0 comments on commit a5cb152

Please sign in to comment.