jfinal01jfinal整合springmvc

搭建jfinal-spring-mvc工程

什么是jfinal-spring-mvc

什么是jfinal

jfinal官网https://www.jfinal.com/

jfinal是一个java开发框架,只带web中间件,启动速度飞快

 

为什要使用jfinal整合spring mvc

因为默认的spring-mvc依赖tomcat,十分不方便,而spring-boot有自动配置开启了自动配置,启动速度10分慢,如果关闭了自动配置,需要一些手动的配置,但是打包10不方便

 

使用jfinal-spring-mvc优势

飞一般的开发速度和开发效率

 

使用jfinal-spring-mvc劣势

需要一些手动配置,对spring和java功底要求较高,不建议初学者使用

 

编译后的示例文件下载地址

http://download.uairobot.com/jfinal-spring-mvc/ee-jfinal-spring-mvc-1.0-release.tar.gz

整合spring-mvc

创建maven工程,命名为ee-jfinal-spring-mvc

pom.xml内容人如下

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <jdk.version>1.8</jdk.version>
</properties>
<dependencies>
  <dependency>
    <groupId>com.jfinal</groupId>
    <artifactId>jfinal-undertow</artifactId>
    <version>1.9</version>
  </dependency>

  <dependency>
    <groupId>com.jfinal</groupId>
    <artifactId>jfinal</artifactId>
    <version>4.7</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.0.0.RELEASE</version>
  </dependency>
</dependencies>
<build>
  <plugins>
    <plugin>
      <!-- java编译插件 -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.2</version>
      <configuration>
        <source>${jdk.version}</source>
        <target>${jdk.version}</target>
        <encoding>UTF-8</encoding>
      </configuration>
    </plugin>
  </plugins>
</build>

spring-mvc.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
  xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  <!-- 默认的注解映射的支持 -->
  <mvc:annotation-driven />

  <!-- 开启controller注解支持 -->
  <context:component-scan base-package="com.alit.jfinal.springmvc.controller" />
</beans>

启动类AppConfig.java

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;

public class AppConfig extends JFinalConfig {
  public static void main(String[] args) {
    long startTime = System.currentTimeMillis();
    UndertowServer.create(AppConfig.class).configWeb(builder -> {
      // 配置 Servlet
      String servletName = "dispathcer";
      builder.addServlet(servletName, "org.springframework.web.servlet.DispatcherServlet");
      builder.addServletMapping(servletName, "*.do");
      builder.addServletInitParam(servletName, "contextConfigLocation", "classpath:spring-mvc.xml");
      builder.setServletLoadOnStartup(servletName, 0);
    }).start();
    long endTime = System.currentTimeMillis();
    System.out.println("启动完成,共使用了" + (endTime - startTime) + "ms");
  }

  public void configConstant(Constants me) {
    me.setDevMode(true);
  }

  public void configRoute(Routes me) {
  }

  public void configEngine(Engine me) {
  }

  public void configPlugin(Plugins me) {
  }

  public void configInterceptor(Interceptors me) {
  }

  public void configHandler(Handlers me) {
  }
}

undertow.txt内容如下

undertow.devMode=true
#自动热加载
undertow.devMode=true
#监听的ip和端口
undertow.host=0.0.0.0
undertow.port=8006
#工程路径
undertow.contextPath=/ee-jfinal-spring-mvc
#静态资源路径
undertow.resourcePath = classpath:ee-jfinal-spring-mvc

HelloController.java

package com.alit.jfinal.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("Hello")
public class HelloController {
  @RequestMapping()
  @ResponseBody
  public String index() {
    return "Hello,World";
  }
}
Starting JFinal 4.7 -> http://0.0.0.0:80
Info: jfinal-undertow 1.9, undertow 2.0.25.Final, jvm 1.8.0_211
十一月 23, 2019 8:10:18 下午 io.undertow.servlet.spec.ServletContextImpl log
INFO: Initializing Spring FrameworkServlet 'dispathcer'
十一月 23, 2019 8:10:18 下午 org.springframework.web.servlet.FrameworkServlet initServletBean
信息: FrameworkServlet 'dispathcer': initialization started
十一月 23, 2019 8:10:18 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing WebApplicationContext for namespace 'dispathcer-servlet': startup date [Sat Nov 23 20:10:18 CST 2019]; root of context hierarchy
十一月 23, 2019 8:10:18 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-mvc.xml]
十一月 23, 2019 8:10:18 下午 org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry register
信息: Mapped "{[/Hello]}" onto public java.lang.String com.alit.jfinal.springmvc.controller.HelloController.index()
十一月 23, 2019 8:10:18 下午 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
信息: Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispathcer-servlet': startup date [Sat Nov 23 20:10:18 CST 2019]; root of context hierarchy
十一月 23, 2019 8:10:18 下午 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
信息: Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispathcer-servlet': startup date [Sat Nov 23 20:10:18 CST 2019]; root of context hierarchy
十一月 23, 2019 8:10:18 下午 org.springframework.web.servlet.FrameworkServlet initServletBean
信息: FrameworkServlet 'dispathcer': initialization completed in 710 ms
十一月 23, 2019 8:10:18 下午 org.xnio.Xnio <clinit>
INFO: XNIO version 3.3.8.Final
十一月 23, 2019 8:10:18 下午 org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.3.8.Final
Starting Complete in 1.6 seconds. Welcome To The JFinal World (^_^)

启动完成,共使用了1694ms

启动速度很快,仅仅使用了1694ms

启动之后访问成功

jfinal-spring-mvc部署

添加maven-jar-plugin排除配置文件和静态文件

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.6</version>
  <configuration>
    <excludes>
      <exclude>*.txt</exclude>
      <exclude>*.xml</exclude>
      <exclude>*.properties</exclude>
      <exclude>${artifactId}</exclude>
      <exclude>${artifactId}/*</exclude>
    </excludes>
  </configuration>
</plugin>

在pom.xml中添加maven-assembly-plugin插件

 <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>3.1.0</version>
  <executions>
    <execution>
      <id>make-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <!-- jar 等压缩文件在被打包进入 zip、tar.gz 时是否压缩,设置为 false 可加快打包速度 -->
        <recompressZippedFiles>false</recompressZippedFiles>
        <!-- 打包生成的文件是否要追加 release.xml 中定义的 id 值 -->
        <appendAssemblyId>true</appendAssemblyId>
        <!-- 指向打包描述文件 package.xml -->
        <descriptors>
          <descriptor>src/main/assembly/package.xml</descriptor>
        </descriptors>
        <!-- 打包结果输出的基础目录 -->
        <outputDirectory>${project.build.directory}/</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

src\main\assembly\package.xml内容如下

将静态文件打包到static文件夹下并在static文件夹下再自动创建一个${artifactId}其目的是为了方便使用nginx的root代理

 

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  <!-- assembly 打包配置更多配置可参考官司方文档: http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html -->
  <id>release</id>
  <!-- 设置打包格式,可同时设置多种格式,常用格式有:dir、zip、tar、tar.gz dir 格式便于在本地测试打包结果 zip 格式便于 windows 系统下解压运行 tar、tar.gz 格式便于 linux 系统下解压运行 -->
  <formats>
    <!-- <format>dir</format> <format>zip</format> -->
    <format>tar.gz</format>
  </formats>

  <!-- 打 zip 设置为 true 时,会在 zip 包中生成一个根目录,打 dir 时设置为 false 少层目录 -->
  <includeBaseDirectory>true</includeBaseDirectory>

  <fileSets>
    <!-- src/main/resources 配置文件 copy 到 config 目录下 -->
    <fileSet>
      <directory>${basedir}/src/main/resources</directory>
      <outputDirectory>config</outputDirectory>
      <includes>
        <include>*.xml</include>
        <include>*.properties</include>
        <include>*.txt</include>
      </includes>
    </fileSet>

    <!-- 复制sql文件到config目录下 -->
    <fileSet>
      <directory>${basedir}/src/main/resources/sql</directory>
      <outputDirectory>config/sql</outputDirectory>
    </fileSet>

    <!--复制静态文件 -->
    <fileSet>
      <directory>${basedir}/src/main/resources/${artifactId}</directory>
      <outputDirectory>static/${artifactId}</outputDirectory>
    </fileSet>

    <!-- 复制service文件 -->
    <fileSet>
      <directory>${basedir}/src/main/bin</directory>
      <lineEnding>unix</lineEnding>
      <outputDirectory>service</outputDirectory>
      <!-- 脚本文件在 linux 下的权限设为 755,无需 chmod 可直接运行 -->
      <fileMode>755</fileMode>
      <includes>
        <include>*.service</include>
      </includes>
    </fileSet>

    <!-- 项目根下面的脚本文件 copy 到根目录下 -->
    <fileSet>
      <directory>${basedir}/src/main/bin</directory>
      <lineEnding>unix</lineEnding>
      <outputDirectory></outputDirectory>
      <!-- 脚本文件在 linux 下的权限设为 755,无需 chmod 可直接运行 -->
      <fileMode>755</fileMode>
      <includes>
        <include>*.sh</include>
      </includes>
    </fileSet>

    <fileSet>
      <directory>${basedir}/src/main/bin</directory>
      <lineEnding>windows</lineEnding>
      <outputDirectory></outputDirectory>
      <!-- 脚本文件在 linux 下的权限设为 755,无需 chmod 可直接运行 -->
      <fileMode>755</fileMode>
      <includes>
        <include>*.bat</include>
      </includes>
    </fileSet>
  </fileSets>

  <!-- 依赖的 jar 包 copy 到 lib 目录下 -->
  <dependencySets>
    <dependencySet>
      <outputDirectory>lib</outputDirectory>
    </dependencySet>
  </dependencySets>
</assembly>

jfinal.sh内容如下

#!/bin/sh
# chkconfig: 345 99 01
# description:robot-vp

##########################
# get app home start
###########################
PRG="$0"
while [ -h "$PRG" ] ; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done
##########################
# get app home end
###########################

##########################
# custom variables start
###########################
JAVA_HOME=/usr/java/jdk1.8.0_211
APP_HOME=`dirname "$PRG"`
APP_NAME=`basename "$PRG"`
PID_FILE=$APP_HOME/$APP_NAME.pid
CP=$APP_HOME/config:$APP_HOME/lib/*:$APP_HOME/static
# 启动入口类,该脚本文件用于别的项目时要改这里
MAIN_CLASS=com.alit.jfinal.springmvc.AppConfig
# Java 命令行参数,根据需要开启下面的配置,改成自己需要的,注意等号前后不能有空格
# JAVA_OPTS="-Xms256m -Xmx1024m -Dundertow.port=80 -Dundertow.host=0.0.0.0"
# JAVA_OPTS="-Dundertow.port=80 -Dundertow.host=0.0.0.0"
CMD="$JAVA_HOME/bin/java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS}"
###########################
# custom variables end
###########################
source /etc/init.d/functions
#########################
# define funcation start
##########################
if [[ "$MAIN_CLASS" == "com.yourpackage.YourMainClass" ]]; then
echo "请先修改 MAIN_CLASS 的值为你自己项目启动Class,然后再执行此脚本。"
  exit 0
fi
lock_dir=/var/lock/subsys
lock_file=$lock_dir/$APP_NAME
createLockFile(){
  [ -w $lock_dir ] && touch $lock_file
}

start(){
  [ -e $APP_HOME/logs ] || mkdir $APP_HOME/logs -p
  
  if [ -f $PID_FILE ]
  then
    echo 'alread running...'
  else
    echo $CMD
    nohup $CMD >> $APP_HOME/logs/$APP_NAME.log 2>&1 &
    echo $! > $PID_FILE
    createLockFile
    echo_success
  fi
}

stop(){
  if [ -f $PID_FILE ]
  then
    killproc -p $PID_FILE
    rm -f $PID_FILE
    echo_success
  else
    echo 'not running...'
  fi
}

restart(){
  stop
  start
}

status(){
  cat $PID_FILE
}
##########################
# define function end
##########################
ACTION=$1
case $ACTION in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  status)
    status
    ;;
  *)
    echo usage "{start|stop|restart|status}"
    ;;
esac

jfinal.bat内容如下

java -Xverify:none -cp .\lib\* com.alit.jfinal.springmvc.AppConfig

打包

使用clean package -DskipTests在Eclipse会出现中会出现MANIFEST.MF (系统找不到指定的路径。)所有使用install

clean install -DskipTests

打包后生成一个文件ee-jfinal-spring-mvc-1.0-release.tar.gz,大小是是8.53M,解压后的文件如下

前台启动
在Windwos前台启动使用下面的命令

java -Xverify:none -cp .\lib\* com.alit.jfinal.springmvc.AppConfig

在Windwos前台启动使用下面的命令,虽然没有向classpath中添加config,但是依然找到config的下面的配置文件,但是static文件夹这必须要添加

java -Xverify:none -cp .\lib\*;static com.alit.jfinal.springmvc.AppConfig

在Linux前台启动使用下面的命令

java -Xverify:none -cp ./config:./lib/*:./staticcom.alit.jfinal.springmvc.AppConfig

在Linux后台启动

./jfinal.sh {start|stop|restart}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章