当前位置: 首页 > 工具软件 > JFinal-event > 使用案例 >

JFinal -spring整合的代码

邬朗
2023-12-01

JFinal在1.9版本的时候,整合的有springPlugin插件,但是 在2.0以后,就不在使用SpringPlugin插件,现在要整合一下spring,经过试验,终于整合好了,下面贴代码:

首先:web.xml配置:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Archetype Created Web Application</display-name>


<!-- 配置spring监听器 -->
<context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:spring/applicationContent.xml</param-value>

  </context-param>

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>


  <!-- JFinal拦截器 -->
  <filter>
    <filter-name>jfinal</filter-name>
    <filter-class>com.jfinal.core.JFinalFilter</filter-class>
    <init-param>
      <param-name>configClass</param-name>
      <param-value>com.demo.config.denoConfig</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>jfinal</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>


配置spring的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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
      >
      <!-- 加载SpringUtils -->
      <bean id="springUtils" class="com.demo.util.SpringUtils" />
<!-- 自动装配 -->
<context:component-scan base-package="com.demo"></context:component-scan>
</beans>

创建一个spring获取IOC的上下文工具类


package com.demo.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;

/**
 * Spring IOC上下文工具类
 * Created by T430 on 2017/5/17.
 */
public class SpringUtils implements ApplicationContextAware {
    /**
     *  当前IOC
     */
    private static ApplicationContext context;

    /**
     *  设置当前上下文环境,此方法由spring自动装配
     */
    private SpringUtils() {}
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        SpringUtils.context = context;
    }

    public static <T> T getBean(Class<T> clazz){
        if (clazz == null) return null;

        return context.getBean(clazz);
    }

    public static <T> T getBean(String beanName, Class<T> clazz) {
        if (null == beanName || "".equals(beanName.trim())) {
            return null;
        }
        if (clazz == null) return null;
        return (T) context.getBean(beanName, clazz);
    }

    public static ApplicationContext getContext(){
        if (context == null) return null;
        return context;
    }

    public static void publishEvent(ApplicationEvent event) {
        if (context == null) return;
        context.publishEvent(event);
    }


}


最后是controller里获取bean


    //获取bean
   private   UserService userService= SpringUtils
			.getBean(UserServiceImpl.class);;


这些搞定,就可以开始JFinal-spring 之旅了




 类似资料: