SSH2和wabacus框架共用数据源的实现方法
最近在使用wabacus框架开发项目,确实非常方便和高效,但有的非常复杂业务还需要用传统的ssh2进行开发,这个时候就想两个共用一个数据源,即都用ssh2配置的数据源,经过请教作者,终于实现了,方法大致如下:
1:创建一个类MyContext.java,如下
public class MyContext implements ApplicationContextAware { private static ApplicationContext context;// 声明一个静态变量保存 @Override public void setApplicationContext(ApplicationContext context) throws BeansException { // TODO Auto-generated method stub this.context = context; } public static ApplicationContext getContext() { return context; } }
2:在applicationContext.xml里面注入该类,
<bean class="com.util.MyContext"></bean>
这样就可以在类里面使用applicationContext提供的bean啦。
创建SpringDataSource.java,该类为wabacus提供数据源(调用的是applicationContext里面的数据源)如下:
public class SpringDataSource extends AbsDataSource { private static Logger logger = Logger.getLogger(SpringDataSource.class); private DataSource ds; public SpringDataSource(){ this.ds = (DataSource) MyContext.getContext().getBean ("dataSource"); System.out.println("########"+ this.ds.toString()); } @Override public Connection getConnection() { try { logger.debug("从数据源" + this.getName() + "获取数据库连接"); return this.ds.getConnection(); } catch (SQLException e) { throw new WabacusRuntimeException("获取" + this.getName() + "数据源的数据库连接失败", e); } } @Override public DataSource getDataSource() { return this.ds; } }
4:在wabacus的配置文件wabacus.cfg.xml 中就可以这样配置来获取数据源啦,如下:
<datasources default="ds_mysql"> <datasource name="ds_mysql" type="com.wabacus.SpringDataSource" dbtype="com.wabacus.config.database.type.MySql"> <property name="driver">driver</property> </datasource> </datasources>
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30