IDEA Spring环境搭建+简单入门(图文教程)
运用此博主的教程:jiachengren
首先利用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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/utilhttp://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 {@Beanpublic 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 {@Autowiredprivate User user;@Testpublic void test(){user.add();}}
7.3运行结果
IDEA Spring环境搭建+简单入门(图文教程)相关推荐
- 数据流程图顶层一层二层_只需三个公式,三阶魔方超简单入门图文教程5:完全复原第二层...
大家好,欢迎来到大超小志,上节课我们讲了"复原底层角块",也就是完全复原第一层,今天我们来复原第二层的棱块,也就是完全复原第二层. 第二层的棱块要么在第二层,要么在最顶层. 我们先 ...
- 四阶魔方java1002四阶魔方java_4x4四阶魔方一看就懂,超简单入门图文教程2:复原中心块...
大家好,欢迎来到大超小志,上节课我们讲了四阶魔方的基本知识和操作,这节课我们就要开始进行复原四阶魔方的第一步了,复原四阶魔方的第一步就是:复原中心四块. 温馨提示:文末有视频链接 对于我们初学者,复原 ...
- 四阶魔方java1002四阶魔方java_4x4四阶魔方一看就懂,超简单入门图文教程1:基本知识和操作...
大家好,欢迎来到大超小志,今天我们开始一起学习四阶魔方.在学习之前,我们还是老规矩,先要了解四阶魔方. 其实,四阶魔方是二阶魔方和三阶魔方的结合体,为什么这么说呢,大家可以观察一下,中心的这四个中心块 ...
- 三阶魔方大中小魔公式_只需三个公式,3x3三阶魔方超简单入门图文教程7:复原顶层棱块...
大家好,欢迎来到大超小志,上节课我们讲了"复原顶层棱块"的第一大步,也就是做一个顶部的黄色十字面,今天我们来第二大步,也就是完全复原顶层棱块. 我们在讲底部十字的时候,让大家记住一 ...
- java mvc 小程序_[Java教程]Spring MVC 的环境搭建和入门小程序
[Java教程]Spring MVC 的环境搭建和入门小程序 0 2017-02-17 00:00:16 1.1.下载spring框架包. 1.1.1百度搜索Spring Framework. 进入s ...
- 云服务器架设网站教程_手把手教你搭建腾讯云服务器入门(图文教程)
本文由博主 威威喵 原创 博客主页:https://blog.csdn.net/smile_running 背景 暑假期间,愁着无聊但也不能荒废学业吧,毕竟以后想靠技术混口饭吃!为了实施自己的计划,特 ...
- vscode如何运行python新手教程_从零开始的TensorFlow+VScode开发环境搭建的步骤(图文)...
VScode不愧是用户数量上升最快的编辑器,界面华丽(当然,需要配合各种主题插件),十分容易上手且功能强大.之前用它写C++体验十分nice,现在需要学习tensorflow,而工欲善其事必先利其器, ...
- Spring Boot 2.0 配置图文教程 1
Spring Boot 2.0 配置图文教程 摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本章内容 自定义 ...
- asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程...
最近在学习张善友老师的NanoFabric 框架的时了解到Exceptionless : https://exceptionless.com/ !因此学习了一下这个开源框架!下面对Exceptionl ...
- 真正从零开始,TensorFlow详细安装入门图文教程!(linux)
读懂智能&未来 首页 专栏 专题 公开课 AI慕课学院 爱搞机 极客购 申请专栏作者 业界人工智能智能驾驶AI+Fintech未来医疗网络安全AR/VR机器人开发者智能硬件物联网GAIR 业界 ...
最新文章
- ios预支付系统 java_支付系统的预发布环境如何搭建
- Please use new-style autograd function with static forward method
- python2.7可以同时连接两个数据库吗
- 戴尔电脑 linux ssh,使用SSH管理Dell iDRAC远程控制卡
- ZOJ The Sum of Unitary Totient(min_25 筛)
- 限定域文本语料的短语挖掘(Phrase Mining)
- 服务器安装三节点RabbitMQ集群(2)
- jdk1.8之lambda表达式
- 学习拾遗--用RadioButtonList 或者DropDownList 进行动态赋值问题
- work summary(2)
- mysql全局读写怎么锁_MySQL全局锁和表锁
- python生成wifi字典_python生成密码字典的方法
- WPF :MVVM简介
- 微软商店无法下载出错问题。
- 使用Bandicam录屏
- 开发直播APP时,视频图片等上传到七牛云存储的实现流程
- 安装TypeScript
- 利用 Global mapper制作地图瓦片
- Python--进程池与线程池
- 老板:你们和外包有什么区别?