图文教程教你IDEA中的Spring环境搭建+简单入门
作者:晚风时亦鹿 发布时间:2022-04-13 23:14:48
标签:IDEA,Spring,环境搭建,入门
首先利用IDEA创建Maven工程项目
1.选择新建项目
2.选中Maven骨架
3.填写项目名称和项目位置
4.Finsh之后默认打开的是pom.xml文件
5.在pom.xml文件下填写Spring的相关依赖(其中有一些拓宽工具依赖)
5.1完整的pom.xml代码(可直接复制)
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
6.完善简单项目结构和编写测试类文件
6.1简单项目结构编写
6.2在resources包下创建Spring配置文件,整合日志配置文件
6.2.1 applicationContext.xml 是Spring配置文件
applicationContext是约定俗成的叫法
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
</beans>
6.2.2 log4j2.xml 日志配置文件
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss} %level : %msg %l%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="mylog" level="error" additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Root level="error">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
6.3编写简单User类,并加上注解(可以直接复制)
**说明:这里的User类用到了 Lombok工具,具体可参照官网文档
Lombok官网:Lombok
@Data 替我们生成 getter,setter,toString等方法
@AllArgs/NoArgsConstructor分别为全参构造和无参构造
package com.xxx.demo.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class User {
private String userName;
private String password;
public void add(){
System.out.println("User add.........");
}
}
6.4在config包下编写AppConfig类,并加上注解(可直接复制代码)
@ComponentScan: 全局扫描组件
其中的参数 basePackages 扫描组件的包
@Configuration: 声明当前类为JavaConfig类
@Bean: 自动装配
package com.xxx.demo.config;
import com.xxx.demo.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ComponentScan(basePackages = "com.xxx.demo")
@Configuration
public class AppConfig {
@Bean
public User user(){
return new User();
}
}
7.至此Spring简单配置完成,接下来测试
7.1简单完善test包,包名和java包下保持一直一致如图
package com.xxx.demo.pojo;
import com.xxx.demo.config.AppConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class TestUser {
@Autowired
private User user;
@Test
public void test(){
user.add();
}
}
7.3运行结果
来源:https://blog.csdn.net/m0_62491934/article/details/126197453


猜你喜欢
- 环境搭建spring boot的简介以往我们开发时用到spring总是避免不了繁琐的配置,例如我们要配置一个数据库连接,可能需要以下几步:1
- 本文实例为大家分享了Android实现加载对话框的具体代码,供大家参考,具体内容如下这里简单说一下两种实现加载对话框的方式:1.使用动画让一
- 概述线程池的好处和使用本篇文章就不赘叙了,不了解的可以参考下面两篇文章:一文全貌了解线程池的正确使用姿势学习线程池原理从手写一个线程池开始那
- 本文实例讲述了Java Swing实现简单的体重指数(BMI)计算器功能。分享给大家供大家参考,具体如下:BMI,Body Mass Ind
- 中介者模式面向对象设计鼓励将行为分布到各个对象中, 这种分布可能会导致对象间有许多连接. 在最坏的情况下, 每一个对象都需要知道其他所有对象
- JdbcTypeInterceptor运行时自动添加 jdbcType 属性 * 签名@Intercepts({
- Spark Streaming算子开发实例transform算子开发transform操作应用在DStream上时,可以用于执行任意的RDD
- 一:什么是Hystrix在分布式环境中,许多服务依赖项中的一些将不可避免地失败。Hystrix是一个库,通过添加延迟容差和容错逻辑来帮助您控
- 从Java 5开始,Java语言对方法参数支持一种新写法,叫 可变长度参数列表,其语法就是类型后跟...,表示此处接受的参数为0到多个Obj
- 背景先上图由此可见,非自旋锁如果拿不到锁会把线程阻塞,直到被唤醒;自旋锁拿不到锁会一直尝试为什么要这样?好处阻塞和唤醒线程都是需要高昂的开销
- @schedule注解动态配置时间间隔动态配置时间间隔是通过自己实现的任务注册到任务调度实现的,并在每次调度的时候更改下次调度时间间隔,如果
- 1.使用的是maven项目,添加依赖<!-- mybatis-plus begin --> <depend
- 问题分析疑惑满满小枫听到这个面试题的时候,心想这是什么水面试官,怎么问这么简单的题目,心想一个for循环加上equal判断再删除不就完事了吗
- 1.背景在项目中有些敏感信息不能直接展示,比如客户手机号、身份证、车牌号等信息,展示时均需要进行数据脱敏,防止泄露客户隐私。脱敏即是对数据的
- 以前的左右滑动效果采用自定义scrollview或者linearlayout来实现,recyclerview可以很好的做这个功能,一般的需求
- springcloud eureka切换nacos配置中心地址: http://10.166.9.7:8848/nacos/bootstra
- 本节只是介绍实战部分,具体的理论参数,请自行百度。所需工具:linux服务器 Jmeter测试工具 xshell &
- 整理文档,搜刮出一个spring boot实现过滤器和 * demo ,稍微整理精简一下做下分享。 * 定义:@WebServletpubl
- spring 自定义让@Value解析到@Value 可以给字段赋值背景@Value通常与@PropertySource(value = “
- 题目题目要求思路:模拟用一个哈希表记录可出现的字母,然后逐一遍历每个单词每个字母,符合条件则结果加一。Javaclass Solution