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. PAT1018 Public Bike Management【dfs】【最短路】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805489282433024 题意: 给定一个图,一个目的地和每个节 ...

  2. ASP.NET常见异常处理示例

    将指定的年月日转初始化为DateTime新的实例(Nop.Admin.Controllers.OrderController.ParseProductAttributes) case Attribut ...

  3. Java随机字符串:随机数字字符串,工具类

    Java中生成随机数,字符串的工具类 1. 调用方法需要传入生成字符串的长度和需要的类型 生成随机数字 生成随机字母字符串 生成随机字符串+数字等 ......... 2. 总共8种类型,具体看工具类 ...

  4. Spring Boot事务管理(下)

    在上两篇 Spring Boot事务管理(上)和Spring Boot事务管理(中)的基础上介绍注解@Transactional. 5 @Transactional属性 属性 类型 描述 value ...

  5. wordpress调用the_excerpt()不带<p>标签

    我们知道wordpress调用摘要内容用<?php the_excerpt(); ?>就可以,但是它会自动添加一个p标签,例如<p>这里是description</p&g ...

  6. eclipse出现An internal error occurred during: "Building workspace". Java heap space 错误

    出现这个错误,eclipse 会卡死,以及自动退出 解决方案 工程根目录 找到项目中.project文件 删除这两处 第一处: <buildCommand> <name>org ...

  7. 【LeetCode每天一题】Jump Game II(跳跃游戏II)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  8. JAVA:IDEA使用Junit(3)

    1.配置 File ——> Project Structure ——> Libraries ——> + ——>搜索Junit类库 确认ok即可 2.编辑项目 选择创建 配置生成 ...

  9. es6阮一峰读后感

    不经意间看了你一眼(阮一峰的es6读后感)我自己常用的 字符串篇:ES6 为字符串添加了遍历器接口(详见<Iterator>一章),使得字符串可以被for...of循环遍历.只要有遍历器接 ...

  10. 在vue2.x项目中怎么引入Element UI

    参考:https://blog.csdn.net/u014054437/article/details/79862793 Element使用方法:https://element.eleme.cn/#/ ...