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}

 

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