首页 技术 正文
技术 2022年11月18日
0 收藏 992 点赞 2,171 浏览 3476 个字

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包下

Jfinal集成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包

Jfinal集成Spring

注: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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905