代码地址 1. 整合ioc ioc整合很简单。根据jfinal手册操做便可。java
2. 整合事务管理器 个人实现方式是这样的: 用spring管理链接池,而不用jfnal的链接池插件。而后实现一个IDataSourceProvider,套给jfianl的activerecord。mysql
配置插件:git
<!-- lang: java --> /** * 配置插件 */ public void configPlugin(Plugins me) { SpringPlugin spring = new SpringPlugin(); me.add(spring); SpringDataSourceProvider prov = new SpringDataSourceProvider(); // 配置ActiveRecord插件 ActiveRecordPlugin arp = new ActiveRecordPlugin(prov); me.add(arp); arp.addMapping("blog", Blog.class); // 映射blog 表到 Blog模型 }
SpringDataSourceProvider : <!-- lang: java --> public class SpringDataSourceProvider implements IDataSourceProvider {spring
private ApplicationContext ctx; private static final String proxyDsName = "proxyDataSource"; private String beanName; public SpringDataSourceProvider(String beanName){ this.beanName = beanName; } public SpringDataSourceProvider(){ this.beanName = proxyDsName; } @Override public DataSource getDataSource() { if(IocInterceptor.ctx==null){ IocInterceptor.ctx = new FileSystemXmlApplicationContext(PathKit.getWebRootPath() + "/WEB-INF/applicationContext.xml"); } ctx = IocInterceptor.ctx; DataSource ds = null; ds = (DataSource)ctx.getBean(beanName,TransactionAwareDataSourceProxy.class); return ds; }
须要特别注意的是在applicationContext.xml中要配置一个代理的datasource来进行spring的事务管理:sql
<!-- lang: xml --> <!-- datasource 数据源 --> <bean id="targetDataSource" name="targetDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 指定链接数据库的驱动--> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <!-- 指定链接数据库的URL--> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1/jfinal_demo?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull"/> <!-- 指定链接数据库的用户名--> <property name="user" value="root"/> <!-- 指定链接数据库的密码--> <property name="password" value=""/> </bean> <bean id="proxyDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy"> <constructor-arg> <ref bean="targetDataSource" /> </constructor-arg> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="proxyDataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/>
具体项目地址数据库
注: jfinal对spring的支持并非很好,每次请求都会经过iocInterceptor反射controller ,每次getBean在设置值。。 仍是不建议用jfnal+spring的形式。用纯spring或者纯jfinal更好。app