IDEA 创建 Java 项目 Spring Maven
一、准备环境
1.1、配置 Maven
Maven 配置阿里云镜像教程
1.2、IDEA Maven 设置

二、创建 Project

三、创建 Moudle

<dependencies>
<!–spring context依赖–>
<!–当你引入Spring Context依赖之后,表示将Spring的基础依赖引入了–>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.2</version>
</dependency>
<!–junit5测试–>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.1</version>
</dependency>
</dependencies>

package yang.junbo.bean;
public class HelloWorld {
public void sayHello(){
System.out.println("HelloWorld");
}
}

<bean id="helloWorld" class="yang.junbo.bean.HelloWorld"></bean>

package yang.junbo.bean;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.lang.reflect.InvocationTargetException;
/**
* ClassName: HelloWorldTest
* Package: yang.junbo.bean
* Description:
*
* @Author 杨钧博
* @Create 2026/6/8 20:37
* @Version 1.0
*/
public class HelloWorldTest {
@Test
public void testHelloWorld(){
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
HelloWorld helloWorld = (HelloWorld) ac.getBean("helloWorld");
helloWorld.sayHello();
}
@Test
public void testHelloWorld2(){
HelloWorld helloWorld = new HelloWorld();
helloWorld.sayHello();
}
@Test
public void testHelloWorld3() throws Exception {
Class clazz = Class.forName("yang.junbo.bean.HelloWorld");
//Object obj = clazz.newInstance();
Object object = clazz.getDeclaredConstructor().newInstance();
}
}
四、Log4j2 日志框架

<!–log4j2的依赖–>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.19.0</version>
</dependency>

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<loggers>
<!–
level指定日志级别,从低到高的优先级:
TRACE < DEBUG < INFO < WARN < ERROR < FATAL
trace:追踪,是最低的日志级别,相当于追踪程序的执行
debug:调试,一般在开发中,都将其设置为最低的日志级别
info:信息,输出重要的信息,使用较多
warn:警告,输出警告的信息
error:错误,输出错误信息
fatal:严重错误
–>
<root level="DEBUG">
<appender-ref ref="spring6log"/>
<appender-ref ref="RollingFile"/>
<appender-ref ref="log"/>
</root>
</loggers>
<appenders>
<!–输出日志信息到控制台–>
<console name="spring6log" target="SYSTEM_OUT">
<!–控制日志输出的格式–>
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss SSS} [%t] %-3level %logger{1024} – %msg%n"/>
</console>
<!–文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,适合临时测试用–>
<File name="log" fileName="F:\\workspace\\logs\\test.log" append="false">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M – %msg%xEx%n"/>
</File>
<!– 这个会打印出所有的信息,
每次大小超过size,
则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,
作为存档–>
<RollingFile name="RollingFile" fileName="F:\\workspace\\logs\\app.log"
filePattern="log/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M – %msg%xEx%n"/>
<SizeBasedTriggeringPolicy size="50MB"/>
<!– DefaultRolloverStrategy属性如不设置,
则默认为最多同一文件夹下7个文件,这里设置了20 –>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
</appenders>
</configuration>

private Logger logger = LoggerFactory.getLogger(HelloWorldTest.class);
五、依赖优化

六、包路径优化

