spring-jfinaljfinal spring 整合 集成 事务

联合创作 · 2023-09-20 10:56

spring-jfinal 用于整合 spring 和 jfinal。


依赖 jar


jfinal、javassist v3.*、Spring v3+(3.2.0之前需要依赖cglib)


使用Maven



<dependency>
<groupId>cc.ecore</groupId>
<artifactId>spring-jfinal-plugin</artifactId>
<version>x.x.x</version>
</dependency>

1、web.xml



<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>jfinal</filter-name>
<filter-class>com.jfinal.core.SpringJFinalFilter</filter-class>
<init-param>
<param-name>configClass</param-name>
<param-value>com.demo.test.CommonConfig</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jfinal</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

以上通过 SpringJFinalFilter 过滤器加载 spring 。 其中 JfinalConfig 自动注册 springbean(JfinalConfig 中可以使用 注解注入属性哦,或者 实现 ApplicationContextAware  接口自动注入 ApplicationContext)。


初始化顺序(不配置 ContextLoaderListener 的情况下): SpringJFinalFilter -> ApplicationContext -> JFinal -> JFinalConfig ... (其他 照旧 ) . 初始化顺序(配置 ContextLoaderListener 的情况下)。 ApplicationContext -> SpringJFinalFilter -> JFinal -> JFinalConfig ... (其他 照旧 ) .


2、JFinalConfig



public class CommonConfig extends JFinalConfig {

@Autowired
public void setApplicationContext(ApplicationContext ctx) {
System.out.println("HelloController attr[ApplicationContext] 已注入。。");
}
...
}

其中 HelloJFinalConfig 自动注入 springbean , scope = "singleton" .


3、Controller



public class HelloController extends Controller {

@Autowired
public void setApplicationContext(ApplicationContext ctx) {
System.out.println("HelloController attr[ApplicationContext] 已注入。。");
}
}

其中 Controller 自动注入 springbean , scope = "prototype" 原因是 jfinal 也是每次请求创建实例 .


4、spring 事务管理



<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${db.userName}" />
<property name="password" value="${db.passWord}" />
<property name="url" value="${db.jdbcUrl}" />
<property name="driverClassName" value="${db.driverClassName}" />
<property name="initialSize" value="${db.initialSize}" />
<property name="maxActive" value="${db.maxActive}" />
<property name="minIdle" value="${db.minIdle}" />
</bean>
<!-- spring 事务管理 ,ActiveRecordPlugin可以获得此 dataSource 可以把事务交给spring 管理 -->
<bean id="dataSourceProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<property name="targetDataSource" ref="druidDataSource" />
</bean>

<!-- ================================事务相关控制================================================= -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceProxy"></property>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED" read-only="false" />
<tx:method name="insert*" propagation="REQUIRED" read-only="false" />
<tx:method name="update*" propagation="REQUIRED" read-only="false" />

<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="select*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>

<!-- 把事务控制在Service层 -->
<aop:config>
<aop:pointcut id="pc" expression="execution(public * demo.service.*.*(..))" />
<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
</aop:config>

以上是 xml 配置方式。 重点是在 ActiveRecordPlugin 中注入 代理数据源。


5、jetty 启动.



public static void main(String[] args) {
CtFactory.toClass();// 这个必须启动前执行
// ...
}
浏览 20
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报