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

SpringIplugin类:

SpringPlugin 是作为 JFinal 的 Plugin 而存在的,所以使用时需要在 JFinalConfig 中配置SpringPlugin,以下是 Plugin 配置示例代码:

 /**
* 配置插件
*/
@Override
public void configPlugin(Plugins me) {
//ConfigDBPluginHelper.configOraclePlugin(me);
//配置Spring挂件, 自动找spring包中所有的xml配置文件
me.add(new SpringPlugin("classpath*:spring/*.xml"));
}

配置SpringPlugin

以上设置默认spring配置xml在src/spring包下

如果设置sringPlugin的时候,没有设置路径

 /**
* 配置插件
*/
@Override
public void configPlugin(Plugins me) {
//ConfigDBPluginHelper.configOraclePlugin(me);
//配置Spring挂件, 自动找spring包中所有的xml配置文件
me.add(new SpringPlugin());
}

无参数设置

SpringPlugin 将 自动去WebRoot/WEB-INF 目录下寻找 applicationContext.xml 作为配置文件进行初始化。

IocInterceptor类:
IocInterceptor 拦截 action 并对其进行依赖注入,以下是示例代码:

 package com.demo.testSpring;

 import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.spring.IocInterceptor;
import com.jfinal.plugin.spring.Inject; @Before(IocInterceptor.class)
public class testSpringController extends Controller{ @Inject.BY_NAME
private Animal animal; public void test(){
animal.move();
animal.jump();
renderNull();
}
}

IocInterceptor

 package com.demo.testSpring;

 public class Animal {
private String animalName; public void setAnimalName(String animalName) {
this.animalName = animalName;
} public void move(){
System.out.println(animalName+" move");
} public void jump(){
System.out.println(animalName+" jump");
}
}

依赖Animal类

 <?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="animal" class="com.demo.testSpring.Animal">
<property name="animalName">
<value>dog</value>
</property>
</bean> </beans>

配置文件

需要的jar包

注:IocInterceptor可以设置为全局拦截器;

 /**
* 配置全局拦截器
*/
public void configInterceptor(Interceptors me) {
me.add(new IocInterceptor());
}

设置为全局拦截器

如果在非Controller中的类中使用spring注入,不需要那么多注解,只需要在xml中配置即可。如下:

 package com.demo.testSpring;

 public class Cat {

     private Animal animal;

     public void setAnimal(Animal animal) {
this.animal = animal;
} public void test(){
animal.move();
animal.jump();
}
}

Cat类

 package com.demo.testSpring;

 import com.jfinal.core.Controller;
import com.jfinal.plugin.spring.Inject; public class testSpringController extends Controller{ @Inject.BY_NAME
private Cat cat; public void test(){ cat.test();
renderNull();
}
}

Controller

 <?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="animal" class="com.demo.testSpring.Animal">
<property name="animalName">
<value>dog</value>
</property>
</bean> <bean id="cat" class="com.demo.testSpring.Cat">
<property name="animal">
<ref bean="animal"/>
</property>
</bean> </beans>

配置项

jfinal不提供自动注入功能,如autowared

Jfinal集成Spring的更多相关文章

  1. jfinal集成spring cxf做webservice服务

    链接地址:http://zhengshuo.iteye.com/blog/2154047 废话不说,直接上代码 新增cxf的plugin CXFPlugin package com.jfinal.pl ...

  2. MyBatis6:MyBatis集成Spring事物管理(下篇)

    前言 前一篇文章<MyBatis5:MyBatis集成Spring事物管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事物的做法,本文的目的是在这个的基 ...

  3. CXF集成Spring实现webservice的发布与请求

    CXF集成Spring实现webservice的发布(服务端) 目录结构: 主要代码: package com.cxf.spring.pojo; public class User { int id ...

  4. Dubbo集成Spring与Zookeeper实例

    >>Dubbo最佳实践 使用Dubbo结合Zookeeper和Spring,是使用比较广泛的一种组合,下面参考官方文档,做个简单的示例,一步步搭建一个使用dubbo结合Zookeeper和 ...

  5. Thymeleaf 集成spring

    Thymeleaf 集成spring 如需先了解Thymeleaf的单独使用,请参考<Thymeleaf模板引擎使用>一文. 依赖的jar包 Thymeleaf 已经集成了spring的3 ...

  6. 06_在web项目中集成Spring

    在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = ...

  7. Hibernate 检索查询的几种方式(HQL,QBC,本地SQL,集成Spring等)

    1.非集成Spring hibernate的检索方式,主要有以下五种. 1.导航对象图检索方式.(根据已经加载的对象,导航到其他对象.) 2.OID检索方式.(按照对象的OID来检索对象.) 3.HQ ...

  8. SpringMVC 3.1集成Spring Security 3.1

    这篇算是一个入门文章,昨天看见有网友提问,spring mvc集成spring security 的时候出错,揣测了一下问题木有解决.我就帮忙给搭建了一个集成框架他说可以,他告诉我这样的文章网上少.今 ...

  9. 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事

    Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...

随机推荐

  1. 浅谈提高Django性能

    Django性能优化是一件困难的事情,但是也不常常如此: 下面4步将能够轻松的提高你的网站的性能,它们非常简单你应该将它们 作为标配. 持久化数据库连接 django1.6以后已经内置了数据库持久化连 ...

  2. 用VsCode写Markdown

    Markdown 基本语法 段落 非常自然,一行文字就是一个段落. 比如: 这是一个段落 会被解释成: <p>这是一个段落.</p> 如果你需要另起一段,请在两个段落之间隔一个 ...

  3. iptables 分析(三)

    原文:http://blog.chinaunix.net/uid-24207747-id-2622902.html find_target查到目标并加载成功,返回一个xtables_target型对象 ...

  4. vue 之 筛选功能实现

    要实现的效果如下:根据输入框里面输入的内容筛选下面列表: 推荐实现代码如下:其中 allProductData 就是用来下拉列表的数据,allProductList 为从获取的所有列表的数据:

  5. Centos不能上外网解决

    检查路由 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0 ...

  6. python数据结构-如何在列表、字典、集合中根据条件筛选数据

    如何在列表.字典.集合中根据条件筛选数据 问题举例: 过滤列表[1, 2, 5, -1, 9, 10]中的负数 筛选字典{“zhangsan”:97, "lisi":80, &qu ...

  7. #WEB安全基础 : HTTP协议 | 0x0 TCP/IP四层结构

    学完HTML/CSS了? 做了这么多网页,但是你知道它们是怎么工作的吗? 作为你的朋友,我也对这些东西感兴趣,在写博客的同时也在和你一起学. 废话少说,进入正题 网络中的通信包括两个端分别为:客户端( ...

  8. 第四章 DOM节点操作

    1.什么是DOM:DOM(document object model)文档对象模型,把每一个元素看做是一个节点,然后对节点进行增删改查的操作 2.DOM的分类:(1)Core Dom:可以对html, ...

  9. 灵雀云容器PaaS平台助力知名股份制银行金融科技革新

    互联网.科技和金融的碰撞给银行业带来巨大影响.IT技术起初是传统金融提升效率的工具和方法,随着新技术的演进,技术成为驱动变革的核心要素.Fintech金融科技以技术和数据为驱动,用创新的方法改变了金融 ...

  10. Practical Lessons from Predicting Clicks on Ads at Facebook

    ABSTRACT 这篇paper中作者结合GBDT和LR,取得了很好的效果,比单个模型的效果高出3%.随后作者研究了对整体预测系统产生影响的几个因素,发现Feature(能挖掘出用户和广告的历史信息) ...