1.jfinal-5.0 使用spring-boot-maven-plugin
1.1.创建普通的jfinal工程
1.1.1.创建工程
工程名jfinal-5.0.0-spring-boot-maven-plugin
pom.xml
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal-undertow</artifactId> <version>3.3</version> </dependency> <dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal</artifactId> <version>5.0.6</version> </dependency> <!-- cos 文件上传 --> <dependency> <groupId>com.jfinal</groupId> <artifactId>cos</artifactId> <version>2022.2</version> </dependency> <!-- junit 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.68</version> </dependency> </dependencies>
1.1.2.代码
controller
package demo; import com.jfinal.core.Controller; import com.jfinal.core.Path; @Path("/") public class HelloController extends Controller { public void index() { renderText("Hello JFinal World."); } }
config
package demo; 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.template.Engine; public class DemoConfig extends JFinalConfig { public void configConstant(Constants me) { me.setDevMode(true); } public void configRoute(Routes me) { me.scan("demo"); } public void configEngine(Engine me) { } public void configPlugin(Plugins me) { } public void configInterceptor(Interceptors me) { } public void configHandler(Handlers me) { } }
启动类
package demo; import com.jfinal.server.undertow.UndertowServer; public class DemoApp { public static void main(String[] args) { UndertowServer.start(DemoConfig.class, 8000, true); } }
1.1.3.启动
访问测试
1.1.4.测试热加载
如果使用eclipse开发,修复java保存后即可热加载生效
1.2.使用spring-boot-maven-plugin启动jfinal工程
1.2.1.使用spring-boot-maven-plugin
在pom.xml中添加spring-boot-maven-plugin
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.7.4</version> <configuration> <mainClass>demo.DemoApp</mainClass> </configuration> </plugin> </plugins> </build>
使用spring-boot-maven-plugin启动工程
进入工程命令
执行命令mvn spring-boot:run
启动日志
E:\code\java\java-study\java-ee-jfinal-study\jfinal-5.0-study\jfinal-5.0.0-spring-boot-maven-plugin>mvn spring-boot:run [INFO] Scanning for projects... [INFO] [INFO] --------< com.litongjava:jfinal-5.0.0-spring-boot-maven-plugin >-------- [INFO] Building jfinal-5.0.0-spring-boot-maven-plugin 1.0 [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] >>> spring-boot-maven-plugin:2.7.4:run (default-cli) > test-compile @ jfinal-5.0.0-spring-boot-maven-plugin >>> [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ jfinal-5.0.0-spring-boot-maven-plugin --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ jfinal-5.0.0-spring-boot-maven-plugin --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ jfinal-5.0.0-spring-boot-maven-plugin --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ jfinal-5.0.0-spring-boot-maven-plugin --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] <<< spring-boot-maven-plugin:2.7.4:run (default-cli) < test-compile @ jfinal-5.0.0-spring-boot-maven-plugin <<< [INFO] [INFO] [INFO] --- spring-boot-maven-plugin:2.7.4:run (default-cli) @ jfinal-5.0.0-spring-boot-maven-plugin --- [INFO] Attaching agents: [] Starting JFinal 5.0.6 Info: jfinal-undertow 3.3, undertow 2.2.20.Final, jvm 17.0.4.1 21:01:15.716 [main] DEBUG org.jboss.logging - Logging Provider: org.jboss.logging.Slf4jLoggerProvider 21:01:15.720 [main] DEBUG io.undertow.session - Setting default session timeout to 1800 21:01:15.723 [main] DEBUG io.undertow.session - Registered session listener io.undertow.servlet.core.SessionListenerBridge@37d31475 21:01:15.790 [main] INFO io.undertow - starting server: Undertow - 2.2.20.Final 21:01:15.795 [main] INFO org.xnio - XNIO version 3.8.7.Final 21:01:15.803 [main] INFO org.xnio.nio - XNIO NIO Implementation Version 3.8.7.Final 21:01:15.931 [main] DEBUG org.xnio - Creating worker:null, pool size:128, max pool size:128, keep alive:60000, io threads:16, stack size:0 21:01:15.939 [main] INFO org.jboss.threads - JBoss Threads version 3.1.0.Final 21:01:15.965 [XNIO-1 I/O-1] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-1', selector sun.nio.ch.WEPollSelectorImpl@5501c9b3 21:01:15.965 [XNIO-1 I/O-2] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-2', selector sun.nio.ch.WEPollSelectorImpl@397b8b1d 21:01:15.965 [XNIO-1 I/O-14] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-14', selector sun.nio.ch.WEPollSelectorImpl@767dab68 21:01:15.965 [XNIO-1 I/O-7] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-7', selector sun.nio.ch.WEPollSelectorImpl@566f0b76 21:01:15.965 [XNIO-1 I/O-6] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-6', selector sun.nio.ch.WEPollSelectorImpl@659c4a47 21:01:15.965 [XNIO-1 I/O-3] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-3', selector sun.nio.ch.WEPollSelectorImpl@21045353 21:01:15.966 [XNIO-1 I/O-8] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-8', selector sun.nio.ch.WEPollSelectorImpl@b4963ed 21:01:15.966 [XNIO-1 I/O-12] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-12', selector sun.nio.ch.WEPollSelectorImpl@565e2276 21:01:15.966 [XNIO-1 I/O-4] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-4', selector sun.nio.ch.WEPollSelectorImpl@5ea8fb5 21:01:15.966 [XNIO-1 I/O-5] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-5', selector sun.nio.ch.WEPollSelectorImpl@431718cc 21:01:15.966 [XNIO-1 I/O-13] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-13', selector sun.nio.ch.WEPollSelectorImpl@1dce95b8 21:01:15.966 [XNIO-1 Accept] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 Accept', selector sun.nio.ch.WEPollSelectorImpl@615c92cb 21:01:15.966 [XNIO-1 I/O-15] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-15', selector sun.nio.ch.WEPollSelectorImpl@22f0b3c9 21:01:15.966 [XNIO-1 I/O-16] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-16', selector sun.nio.ch.WEPollSelectorImpl@78af752d 21:01:15.966 [XNIO-1 I/O-9] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-9', selector sun.nio.ch.WEPollSelectorImpl@39b9faf0 21:01:15.965 [XNIO-1 I/O-11] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-11', selector sun.nio.ch.WEPollSelectorImpl@7f1ad1cf 21:01:15.966 [XNIO-1 I/O-10] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-10', selector sun.nio.ch.WEPollSelectorImpl@787439b5 21:01:15.967 [main] DEBUG io.undertow - Configuring listener with protocol HTTP for interface 0.0.0.0 and port 8000 Starting Complete in 0.4 seconds. Welcome To The JFinal World (^_^) Server running at: > Local: http://localhost:8000 > Network: http://192.168.0.8:8000
1.2.2.测试热加载
目前工程已经使用spring-boot-maven-plugin启动,
在eclipse中修改文件java代码并保存,测试热加载生效
Loading changes ...... 21:02:13.690 [HotSwapWatcher] INFO io.undertow - stopping server: Undertow - 2.2.20.Final 21:02:13.696 [HotSwapWatcher] DEBUG io.undertow.session - Setting default session timeout to 1800 21:02:13.696 [HotSwapWatcher] DEBUG io.undertow.session - Registered session listener io.undertow.servlet.core.SessionListenerBridge@312b4298 21:02:13.724 [HotSwapWatcher] INFO io.undertow - starting server: Undertow - 2.2.20.Final 21:02:13.725 [HotSwapWatcher] DEBUG org.xnio - Creating worker:null, pool size:128, max pool size:128, keep alive:60000, io threads:16, stack size:0 21:02:13.745 [XNIO-3 I/O-2] DEBUG org.xnio.nio - Started channel thread 'XNIO-3 I/O-2', selector sun.nio.ch.WEPollSelectorImpl@48f36c1c 21:02:13.745 [HotSwapWatcher] DEBUG io.undertow - Configuring listener with protocol HTTP for interface 0.0.0.0 and port 8000 21:02:13.745 [XNIO-3 I/O-14] DEBUG org.xnio.nio - Started channel thread 'XNIO-3 I/O-14', selector sun.nio.ch.WEPollSelectorImpl@526e6eef 21:02:13.745 [XNIO-3 I/O-6] DEBUG org.xnio.nio - Started channel thread 'XNIO-3 I/O-6', selector sun.nio.ch.WEPollSelectorImpl@362514c0 21:02:13.745 [XNIO-3 I/O-9] DEBUG org.xnio.nio - Started channel thread 'XNIO-3 I/O-9', selector sun.nio.ch.WEPollSelectorImpl@48d76da2 21:02:13.745 [XNIO-3 I/O-4] DEBUG org.xnio.nio - Started channel thread 'XNIO-3 I/O-4', selector sun.nio.ch.WEPollSelectorImpl@60535e 21:02:13.746 [XNIO-3 I/O-11] DEBUG org.xnio.nio - Started channel thread 'XNIO-3 I/O-11', selector sun.nio.ch.WEPollSelectorImpl@1f01a450 21:02:13.746 [XNIO-3 I/O-12] DEBUG org.xnio.nio - Started channel thread 'XNIO-3 I/O-12', selector sun.nio.ch.WEPollSelectorImpl@7ff825c5 21:02:13.745 [XNIO-3 I/O-1] DEBUG org.xnio.nio - Started channel thread 'XNIO-3 I/O-1', selector sun.nio.ch.WEPollSelectorImpl@54a22e00 21:02:13.745 [XNIO-3 I/O-8] DEBUG org.xnio.nio - Started channel thread 'XNIO-3 I/O-8', selector sun.nio.ch.WEPollSelectorImpl@110f9397 21:02:13.745 [XNIO-3 I/O-7] DEBUG org.xnio.nio - Started channel thread 'XNIO-3 I/O-7', selector sun.nio.ch.WEPollSelectorImpl@50fe0346 21:02:13.745 [XNIO-3 I/O-3] DEBUG org.xnio.nio - Started channel thread 'XNIO-3 I/O-3', selector sun.nio.ch.WEPollSelectorImpl@5ec2f37 21:02:13.745 [XNIO-3 I/O-10] DEBUG org.xnio.nio - Started channel thread 'XNIO-3 I/O-10', selector sun.nio.ch.WEPollSelectorImpl@4c3367b0 21:02:13.746 [XNIO-3 I/O-13] DEBUG org.xnio.nio - Started channel thread 'XNIO-3 I/O-13', selector sun.nio.ch.WEPollSelectorImpl@7fe9d40e Loading complete in 0.1 seconds (^_^)
1.3.使用spring-boot-maven-plugin打包jfinal工程
1.3.1.添加spring-boot-maven-plugin配置
修改spring-boot-maven-plugin配置添加executions
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.7.4</version> <configuration> <mainClass>demo.DemoApp</mainClass> </configuration> <!-- 设置执行目标 --> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
执行打包命令mvn package 打包完成,日志如下
E:\code\java\java-study\java-ee-jfinal-study\jfinal-5.0-study\jfinal-5.0.0-spring-boot-maven-plugin>mvn package [INFO] Scanning for projects... [INFO] [INFO] --------< com.litongjava:jfinal-5.0.0-spring-boot-maven-plugin >-------- [INFO] Building jfinal-5.0.0-spring-boot-maven-plugin 1.0 [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ jfinal-5.0.0-spring-boot-maven-plugin --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ jfinal-5.0.0-spring-boot-maven-plugin --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ jfinal-5.0.0-spring-boot-maven-plugin --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ jfinal-5.0.0-spring-boot-maven-plugin --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ jfinal-5.0.0-spring-boot-maven-plugin --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ jfinal-5.0.0-spring-boot-maven-plugin --- [INFO] Building jar: E:\code\java\java-study\java-ee-jfinal-study\jfinal-5.0-study\jfinal-5.0.0-spring-boot-maven-plugin\target\jfinal-5.0.0-spring-boot-maven-plugin-1.0.jar [INFO] [INFO] --- spring-boot-maven-plugin:2.7.4:repackage (default) @ jfinal-5.0.0-spring-boot-maven-plugin --- [INFO] Replacing main artifact with repackaged archive [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.307 s [INFO] Finished at: 2022-11-10T21:04:51+08:00 [INFO] ------------------------------------------------------------------------
查看打包后的文件
1.3.2.启动
使用java -jar 命令启动即可
启动命令java -jar target\jfinal-5.0.0-spring-boot-maven-plugin-1.0.jar
E:\code\java\java-study\java-ee-jfinal-study\jfinal-5.0-study\jfinal-5.0.0-spring-boot-maven-plugin>java -jar target\jfinal-5.0.0-spring-boot-maven-plugin-1.0.jar Starting JFinal 5.0.6 Info: jfinal-undertow 3.3, undertow 2.2.20.Final, jvm 17.0.4.1 21:05:43.387 [main] DEBUG org.jboss.logging - Logging Provider: org.jboss.logging.Slf4jLoggerProvider 21:05:43.392 [main] DEBUG io.undertow.session - Setting default session timeout to 1800 21:05:43.395 [main] DEBUG io.undertow.session - Registered session listener io.undertow.servlet.core.SessionListenerBridge@4590c9c3 21:05:43.491 [main] INFO io.undertow - starting server: Undertow - 2.2.20.Final 21:05:43.499 [main] INFO org.xnio - XNIO version 3.8.7.Final 21:05:43.512 [main] INFO org.xnio.nio - XNIO NIO Implementation Version 3.8.7.Final 21:05:43.668 [main] DEBUG org.xnio - Creating worker:null, pool size:128, max pool size:128, keep alive:60000, io threads:16, stack size:0 21:05:43.682 [main] INFO org.jboss.threads - JBoss Threads version 3.1.0.Final 21:05:43.717 [XNIO-1 I/O-3] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-3', selector sun.nio.ch.WEPollSelectorImpl@76b83f98 21:05:43.717 [XNIO-1 I/O-4] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-4', selector sun.nio.ch.WEPollSelectorImpl@12db6a7a 21:05:43.717 [XNIO-1 I/O-7] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-7', selector sun.nio.ch.WEPollSelectorImpl@19724d2a 21:05:43.717 [XNIO-1 I/O-5] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-5', selector sun.nio.ch.WEPollSelectorImpl@777a11bc 21:05:43.716 [XNIO-1 I/O-2] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-2', selector sun.nio.ch.WEPollSelectorImpl@7ee59801 21:05:43.717 [XNIO-1 I/O-10] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-10', selector sun.nio.ch.WEPollSelectorImpl@1f124f32 21:05:43.717 [XNIO-1 I/O-6] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-6', selector sun.nio.ch.WEPollSelectorImpl@37dbb672 21:05:43.716 [XNIO-1 I/O-1] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-1', selector sun.nio.ch.WEPollSelectorImpl@1e2aca24 21:05:43.717 [XNIO-1 I/O-9] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-9', selector sun.nio.ch.WEPollSelectorImpl@6a8543e 21:05:43.717 [XNIO-1 I/O-12] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-12', selector sun.nio.ch.WEPollSelectorImpl@7a2d89a1 21:05:43.718 [XNIO-1 I/O-16] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-16', selector sun.nio.ch.WEPollSelectorImpl@10550965 21:05:43.717 [XNIO-1 I/O-11] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-11', selector sun.nio.ch.WEPollSelectorImpl@3a848abd 21:05:43.717 [XNIO-1 I/O-13] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-13', selector sun.nio.ch.WEPollSelectorImpl@36296b9c 21:05:43.717 [XNIO-1 I/O-14] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-14', selector sun.nio.ch.WEPollSelectorImpl@302a342b 21:05:43.718 [XNIO-1 I/O-15] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-15', selector sun.nio.ch.WEPollSelectorImpl@2bcf0a52 21:05:43.718 [XNIO-1 Accept] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 Accept', selector sun.nio.ch.WEPollSelectorImpl@49235263 21:05:43.718 [XNIO-1 I/O-8] DEBUG org.xnio.nio - Started channel thread 'XNIO-1 I/O-8', selector sun.nio.ch.WEPollSelectorImpl@7c3a0e25 21:05:43.719 [main] DEBUG io.undertow - Configuring listener with protocol HTTP for interface 0.0.0.0 and port 8000 Starting Complete in 0.5 seconds. Welcome To The JFinal World (^_^) Server running at: > Local: http://localhost:8000 > Network: http://192.168.0.8:8000