1、IDEA
开发环境为ubuntu,直接在官网下载IDEA社区版并解压执行即可。但是刚下好的IDEA有一些小问题,可以做一些小的优化
(1)打开.md文件崩溃问题:关闭markdown显示插件即可(ctrl + alt + s 打开插件管理目录)
(2)运行缓慢问题:修改./bin/idea64.vmoptions文件中的Xms(可用内存)大小
(3)英文转汉化:下载安装汉化插件插件
2、SpringBoot模板
(1)上start.spring.io选择对应的代码模板,填写信息并勾选对应的模块(会生成对应的pom文件)
(2)下载代码模板,用IDEA打开,会根据pom自动下载对应的模块
(3)shift + F10编译运行,ctrl + F2停止运行
(4)可以在控制台输入mvn spring-boot:run编译执行,但是需在pom指定main类位置
3、SpringBoot网页模板小改
和原生的网页模板相比,将web配置单独拎出来,添加了rest风格的控制器和log4j2日志。
src的文件目录结构如下所示:
-java
- com.lypt.rumia
- main
- appController
- RumiaApplication
- RumiaConfiguration
- tool
- appLogger
- resources
- static
- templates
- application.properties
- log4j2-spring.xml
- test
..
其中各部分代码如下:
(1)RumiaApplication
package com.lypt.rumia.main;
import org.springframework.boot.SpringApplication;
public class RumiaApplication
{
public static void main(String[] args)
{
SpringApplication.run(RumiaConfiguration.class, args);
}
}
(2)RumiaConfiguration
package com.lypt.rumia.main;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ServletComponentScan
@EnableAutoConfiguration
public class RumiaConfiguration
{
@Bean
public appController retController()
{
return new appController();
}
}
(3)appController
package com.lypt.rumia.main;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
import com.lypt.rumia.tool.appLogger;
@RestController
@RequestMapping("test")
public class appController
{
public appController()
{
appLogger.writeLog(this.getClass(), "WEB Controller Start");
}
@GetMapping
public Map<String, String> testGet()
{
appLogger.writeLog(this.getClass(), "WEB Controller Get a REQ");
Map<String, String> res = new HashMap<>();
res.put("name", "rumia");
res.put("age", "10");
return res;
}
}
(4)appLogger
package com.lypt.rumia.tool;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
public class appLogger
{
public static boolean writeLog(Class c, String msg)
{
try
{
if(msg == null)
return false;
Logger logger = LogManager.getLogger(c);
logger.log(Level.INFO, msg);
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
}
4、其他配置
(1)pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lypt</groupId>
<artifactId>rumia</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rumia</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.lypt.rumia.main</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
(2)application.properties
需要注意的是,这里的classpath等价于 main/java + main/resources + 第三方jar包的根目录
logging.config = classpath:log4j2-spring.xml
(3)log4j2-spring-xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="RumiaLog4j2: %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>