2019独角兽企业重金招聘Python工程师标准>>>

最近公司使用Jfinal开发项目,不知道什么原因Jfinal和其他的几个插件集成的时候,事物管理并不那么随心,所以就选择了Spring作为Jfinal的插件来管理事物.废话不多说,直接上代码.

public class MyConfig extends JFinalConfig{@Overridepublic void configConstant(Constants me) {IocKit.processJFinalConfig(this);loadPropertyFile("db.properties");me.setEncoding("UTF-8");me.setDevMode(getPropertyToBoolean("devMode", true));}@Overridepublic void configRoute(Routes me) {System.out.println("configRoute");me.add("/blog", BlogController.class);}@Overridepublic void configPlugin(Plugins me) {SpringPlugin springPlugin = new SpringPlugin("classpath*:spring/applicationContext-*.xml");me.add(springPlugin);SpringDataSourceProvider prov = new SpringDataSourceProvider(springPlugin);// 配置ActiveRecord插件,将jfinal的事物交给Spring管理ActiveRecordPlugin arp = new ActiveRecordPlugin(prov);me.add(arp);arp.addMapping("system_admin", SystemAdmin.class);}@Overridepublic void configInterceptor(Interceptors me) {System.out.println("configInterceptor");}@Overridepublic void configHandler(Handlers me) {System.out.println("configHandler");}@Overridepublic void configEngine(Engine me) {System.out.println("configEngine");}}

IocKit.java

public class IocKit {// ioc @Inject @Value @Autowiredstatic AutowiredAnnotationBeanPostProcessor autowiredAnnotationBeanPostProcessor;// ioc @Resourcestatic CommonAnnotationBeanPostProcessor commonAnnotationBeanPostProcessor;public static AutowiredAnnotationBeanPostProcessor getAutowiredAnnotationBeanPostProcessor() {Assert.notNull(autowiredAnnotationBeanPostProcessor,"The main autowiredAnnotationBeanPostProcessor is null, initialize SpringPlugin first");return autowiredAnnotationBeanPostProcessor;}public static CommonAnnotationBeanPostProcessor getCommonAnnotationBeanPostProcessor() {Assert.notNull(commonAnnotationBeanPostProcessor,"The main commonAnnotationBeanPostProcessor is null, initialize SpringPlugin first");return commonAnnotationBeanPostProcessor;}// --------------------------------------------------// JFinalConfig// --------------------------------------------------/*** @Title: 处理 JFinalConfig*/public static void processJFinalConfig(JFinalConfig finalConfig) {ApplicationContext ctx = org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(JFinal.me().getServletContext());processJFinalConfig(finalConfig, ctx);}/*** @Title: 处理 JFinalConfig*/public static void processJFinalConfig(JFinalConfig jfinalConfig, ApplicationContext ctx) {Assert.notNull(ctx, "ApplicationContext not be null");Assert.notNull(jfinalConfig, "jfinalConfig not be null");AutowiredAnnotationBeanPostProcessor autowiredAnnotationBeanPostProcessor = setBeanFactory(new AutowiredAnnotationBeanPostProcessor(), ctx);CommonAnnotationBeanPostProcessor commonAnnotationBeanPostProcessor = setBeanFactory(new CommonAnnotationBeanPostProcessor(), ctx);processInjection(jfinalConfig, commonAnnotationBeanPostProcessor, autowiredAnnotationBeanPostProcessor);}/*** @Title: 注入 bean 调用*/public static void invokeForProcessInjection(Object invocation) {Invocation inv = as(invocation, Invocation.class);Controller controller = inv.getController();processInjection(controller);inv.invoke();}public static void processInjection(Object bean) {processInjection(bean, commonAnnotationBeanPostProcessor, autowiredAnnotationBeanPostProcessor);}// ---------------------------------------------------------// function// ---------------------------------------------------------static void init(ApplicationContext ctx) {autowiredAnnotationBeanPostProcessor = setBeanFactory(new AutowiredAnnotationBeanPostProcessor(), ctx);commonAnnotationBeanPostProcessor = setBeanFactory(new CommonAnnotationBeanPostProcessor(), ctx);}static <T extends BeanFactoryAware> T setBeanFactory(T beanPostProcessor, ApplicationContext ctx) {beanPostProcessor.setBeanFactory(ctx.getAutowireCapableBeanFactory());return beanPostProcessor;}static void processInjection(Object bean, CommonAnnotationBeanPostProcessor commonAnnotationBeanPostProcessor,AutowiredAnnotationBeanPostProcessor autowiredAnnotationBeanPostProcessor) {commonAnnotationBeanPostProcessor.postProcessPropertyValues(null, null, bean, null);autowiredAnnotationBeanPostProcessor.processInjection(bean);}// ----------------------------------------------------------------// as proxy// ----------------------------------------------------------------private final static Map<String, Method> methodCache = new HashMap<String, Method>(16);private static Method cachedMethod(Class<?> type, Method method) throws NoSuchMethodException {String name = method.getName();String key = type.getName() + "." + name;Method m = methodCache.get(key);if (m == null) {synchronized (methodCache) {m = methodCache.get(key);if (m == null) {Class<?>[] parameterTypes = method.getParameterTypes();try {// Actual method name matches always come firstm = type.getMethod(name, parameterTypes);} catch (SecurityException ignore) {m = type.getDeclaredMethod(name, parameterTypes);}methodCache.put(key, m);}}}return m;}/*** Create a proxy for the wrapped object allowing to typesafely invoke* methods on it using a custom interface** @param proxyType*            The interface type that is implemented by the proxy* @return A proxy for the wrapped object*/@SuppressWarnings("unchecked")private static <P> P as(final Object object, Class<P> proxyType) {final Class<?> clazz = object.getClass();final InvocationHandler handler = new InvocationHandler() {public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {Method m = cachedMethod(clazz, method);boolean accessible = m.isAccessible();try {if (!accessible) m.setAccessible(true);return m.invoke(object, args);} finally {m.setAccessible(accessible);}}};return (P) Proxy.newProxyInstance(proxyType.getClassLoader(), new Class[] { proxyType }, handler);}private static interface Invocation {void invoke();Controller getController();}
}

SpringDataSourceProvider.java

public class SpringDataSourceProvider implements IDataSourceProvider {private static final String proxyDsName = "proxyDataSource";private SpringPlugin springPlugin;private String beanName;public SpringDataSourceProvider(SpringPlugin springPlugin, String beanName) {this.springPlugin = springPlugin;this.beanName = beanName;}public SpringDataSourceProvider(SpringPlugin springPlugin) {this.springPlugin = springPlugin;this.beanName = proxyDsName;}public DataSource getDataSource() {ApplicationContext ctx = springPlugin.getApplicationContext();return (DataSource)ctx.getBean(beanName,TransactionAwareDataSourceProxy.class);}
}

SpringPlugin.java

private static boolean isStarted = false;private String[] configurations;private ApplicationContext ctx;public SpringPlugin() {}public SpringPlugin(ApplicationContext ctx) {this.setApplicationContext(ctx);}public SpringPlugin(String... configurations) {this.configurations = configurations;}public void setApplicationContext(ApplicationContext ctx) {Assert.notNull(ctx, "ApplicationContext can not be null.");this.ctx = ctx;}public ApplicationContext getApplicationContext() {return this.ctx ;}public boolean start() {if (isStarted) {return true;}else if (!isStarted && configurations != null){ctx = new FileSystemXmlApplicationContext(configurations);}else {ctx = new FileSystemXmlApplicationContext(PathKit.getWebRootPath() + "/WEB-INF/applicationContext.xml");}Assert.notNull(ctx, "ApplicationContext can not be null.");IocKit.init(ctx);return isStarted = true;}public boolean stop() {ctx = null;isStarted = false;return true;}
}

applicationContext-core.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context" default-autowire="byName"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"><!-- 引入配置文件 --><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:db.properties"/></bean><!-- dataSource配置 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"init-method="init" destroy-method="close"><property name="driverClassName" value="${jdbc.driverClassName}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/><property name="filters" value="log4j"/><property name="maxActive" value="5"/><property name="initialSize" value="1"/><property name="maxWait" value="6000"/></bean><bean id="proxyDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy"><constructor-arg><ref bean="dataSource" /></constructor-arg></bean><!-- 声明式事务的配置 --><!-- 事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></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="pointcut" expression="execution(public * cn.minions.testSpring.Service.impl.*.*(..))" /><aop:advisor pointcut-ref="pointcut" advice-ref="txAdvice" /></aop:config><context:component-scan base-package="cn.minions.testSpring" />
</beans>

完整测试项目的地址:https://gitee.com/fhcspring/jfinal-spring

测试环境还集成了activiti mybatis

转载于:https://my.oschina.net/u/3053883/blog/2051637

Jfinal集成Spring插件相关推荐

  1. Jfinal集成Spring

    JFinal框架也整合了spring框架,下面实现JFinal怎么去配置Spring框架.在JFinal中整合Spring使用到的类是SpringPlugin和IocInterceptor类 Spri ...

  2. 集成spring mvc_向Spring MVC Web应用程序添加社交登录:集成测试

    集成spring mvc 我已经写了关于为使用Spring Social 1.1.0的应用程序编写单元测试的挑战,并为此提供了一种解决方案 . 尽管单元测试很有价值,但它并不能真正告诉我们我们的应用程 ...

  3. IDEA集成Docker插件实现项目打包镜像一键部署与Docker CA加密认证

    IDEA集成Docker插件实现项目打包镜像一键部署与Docker CA加密认证 Docker开启远程访问 修改该Docker服务文件 加载配置与重启 验证是否开启成功 IDEA配置docker 编写 ...

  4. 从0开始集成Spring和mybatis

    如何从0开始集成Spring和mybatis; 1.新建数据库: CREATE DATABASE /*!32312 IF NOT EXISTS*/`smbms` USE `smbms`; DROP T ...

  5. spring官网上下载的所有版本的spring插件

    废话不多说,直接上干货,Eclipse4.2到Eclipse4.14.0的所有版本的spring插件:https://pan.baidu.com/s/1UEsTexAILKYZwShMnHeM6A 我 ...

  6. 解决SpringBoot集成分页插件pagehelper出现的循环依赖问题

    版权声明 本文原创作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl 问题描述 SpringBoot2.6.7中集成分页插件com.github.pagehelpe ...

  7. 类型转换神器Mapstruct新出的Spring插件真好用

    胖哥在几年前安利过Mapstruct这个神器,它可以代替BeanUtil来进行DTO.VO.PO之间的转换.它使用的是Java编译期的  annotation processor 机制,说白了它就是一 ...

  8. IntelliJ IDEA社区版安装spring插件

    IntelliJ IDEA社区版安装spring插件 IntelliJ IDEA商业版比社区版的功能强大,支持的开发语言.框架.技术工具等更全面.Version 2021.2.*以后版本的社区版不提供 ...

  9. 持续集成工具Jenkins学习4 Idea集成Jenkins插件

    持续集成工具Jenkins学习4 Idea集成Jenkins插件 一.功能简介 二.安装Idea插件 1. 搜索安装 2. 设置 三.Jenkins开启CSRF 四.使用 一.功能简介 Idea可以方 ...

最新文章

  1. java 回调(callback)函数简介.
  2. 桥接路由器总是掉线_光猫集成了路由功能,路由器的路由功能会多余吗?
  3. 磨刀不误砍柴工—Exceptionless搭配log4net记录日志
  4. mysql 主从_搭建mysql主从并编写监控主从状态脚本
  5. Python2.7本地安装numpy包
  6. background 渐变_今日重点:April安卓渐变黑。| 明日日程:BUG修复。
  7. Java应用开发的一条重要经验:先建立基础设施
  8. 初学C语言2--C语言项目的基本框架
  9. linux模拟键盘按键_Linux上的自动键盘按键
  10. B站百万粉丝是如何做起来的?解密UP主成长之路
  11. slidebox使用教程 设定焦点数量
  12. 入侵检测领域数据集总结
  13. sql内外连接的区别
  14. 图片处理工具类ImageHelper
  15. 30岁上下的你,现在混得怎么样?
  16. Unsupported class file major version 55
  17. 输出4+44+444+4444
  18. 超好看的粒子效果文字动画特效HTML5源码
  19. 【Fiddler介绍】
  20. 【科创人】悦跑圈CTO钱荣明:创业成瘾,识人为先

热门文章

  1. 面试 -- ListView对其指定的子Item进行单独的刷新
  2. IntelliJ IDEA中JAVA连接MYSQL
  3. 最新CAX/EDA/CFD/GIS/光学/化工/液压软件资源网
  4. sql server 2008建域时提示admin密码不符合要求解决方法
  5. 深度解析:mPaaS 3.0全新组件
  6. Maven之jar包和项目管理
  7. 【重要预警】“永恒之蓝”漏洞又现新木马 组成僵尸网络挖矿虚拟货币
  8. php函数,static,globalkeyword及三种变量作用域
  9. System.exit(0)和System.exit(1)区别
  10. 模拟storage copy 功能失败的记录