Spring的核心Jar包

在Spring4的官方文档里,提到了Sping的核心包是:spring-context,只要引用了这个jar包,就可以实现Spring90%的基础功能。maven引用如下:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>

而在Spring5时,就不再这么说了,因为Spring5推荐用户使用Spring Boot构建项目。

测试Spring的几种方法:

1. 在main方法中使用ClassPathXmlApplicationContext,这个类可以加载Spring的配置文件,下面的getBean是使用byName方式自动注入的。

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");
UserService service = (UserService)contex.getBean("service");
service.getUser();
}
}

2. 在main方法中使用AnnotationConfigApplicationContext,这个类可以加载Spring的启动类,或者有@Configuration注解的类,这里的getBean使用的是byType方式注入。@ComponentScan注解是声明Spring要扫描资源的包路径。

import com.zh.service.SpringConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService bean = context.getBean(UserService.class);
bean.getUser();
}
}
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan("com.zh.service")
public class SpringConfig {
}

3. 使用spring-test包,如果你使用的spring boot构建项目,只需要使用spring-boot-starter-test包就可以了,因为它里面带了spring-test

<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.0.RELEASE</version>
<scope>test</scope>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.8.RELEASE</version>
<scope>test</scope>
</dependency>

spring boot的测试方法,如果是Spring Cloud的话,注解需要这么写:@SpringBootTest(classes={ServerApplication.class,UserServiceTest.class}) ,其中ServerApplication是Spring Cloud的启动类,UserServiceTest是当前的测试类。

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {
@Autowired
private UserService userService; @Test
public void getUser(){
userServce.getUser();
} }

Spring学习总结(3)-了解Spring框架的更多相关文章

  1. Spring学习之——手写Spring源码V2.0(实现IOC、D、MVC、AOP)

    前言 在上一篇<Spring学习之——手写Spring源码(V1.0)>中,我实现了一个Mini版本的Spring框架,在这几天,博主又看了不少关于Spring源码解析的视频,受益匪浅,也 ...

  2. Spring学习(三)——Spring中的依赖注入的方式

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  3. Spring学习(二)——Spring中的AOP的初步理解[转]

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  4. Spring学习(二)——Spring中的AOP的初步理解

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  5. spring 学习(四): spring 的 jdbcTemplate 操作

    spring 学习(四): spring 的 jdbcTemplate 操作 spring 针对 javaee 的每一层,都提供了相应的解决技术,jdbcTemplate 的主要操作在 dao 层. ...

  6. spring学习(二) ———— AOP之AspectJ框架的使用

    前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...

  7. Spring学习(一)——Spring中的依赖注入简介【转】

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  8. Spring学习(一)——Spring中的依赖注入简介

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  9. Spring学习(九)Spring 和数据库编程【了解】

    一.传统 JDBC 回顾 用一个大佬的demo来简单看一下 /** * 使用jdbc,根据id查询单个Student的信息 */ public class JdbcManage { public St ...

  10. Spring学习(一)--Spring的设计与整体架构

    之前只是在学校里大概的学习了一下Spring框架的使用以及一些最基本.浅显的原理,并没有做出深入的学习,等到工作之后想提升自己的时候发现所掌握的Spring框架的简直烂如狗屎,为监督自己的学习进度,立 ...

随机推荐

  1. 返回报文变成xml格式了!

    首先,google chrome之前有安装jsonview插件: 然后,自己弄springCloud项目,搭建eureka后,访问url发现返回报文变成xml格式了,一通摸索及查找,现整理如下: 1. ...

  2. mac篇---使用iTerm2快捷连接SSH

    大家都知道使用iTerm2连接shh 使用命令 ssh -p22 root@129.10.10.1,然后输入密码即可. 但是每次都输入还是比较麻烦的.iTerm2为我们提供了快捷的方式.三步即可完成此 ...

  3. 隐写工具outguess安装使用介绍

      0x00 outguess下载安装 Kail终端命令输入git clone https://github.com/crorvick/outguess 安装包随即下载到文件夹.双击打开文件夹,右键点 ...

  4. O(1)求解自然数异或和

    序 又是一个不眠之夜. 求: \[f_i=1 \bigoplus 2 \bigoplus 3 \bigoplus...\bigoplus (i-1) \bigoplus i \] 思路1:周期分析 \ ...

  5. 记一次在Grafana中使用Worldmap Panel的经历

    背景 因与工作相关,以下内容皆做了脱敏处理 主要的需求是要根据地理位置查看可视化的数据. 安装及创建 安装命令来源于官网 grafana-cli plugins install grafana-wor ...

  6. 521我发誓读完本文,再也不会担心Spring配置类问题了

    当大潮退去,才知道谁在裸泳.关注公众号[BAT的乌托邦]开启专栏式学习,拒绝浅尝辄止.本文 https://www.yourbatman.cn 已收录,里面一并有Spring技术栈.MyBatis.中 ...

  7. Maven 专题(二):vscode 创建一个java Maven项目(vscode)以HdfsClientDemo 为例

    1.打开vs code软件,最好创建好自己的工程并使用vs code打开   2. 使用快捷键ctrl + shift + p按键,输入maven进行搜索,选中如图诉讼hi的create mavene ...

  8. linux09 /消息队列、saltstack工具

    linux09 /消息队列.saltstack工具 目录 linux09 /消息队列.saltstack工具 1. 消息队列之rabbitmq 2. 云计算 3. 远程过程调用的实现:rpc 4. s ...

  9. Python并发编程01 /操作系统发展史、多进程理论

    Python并发编程01 /操作系统发展史.多进程理论 目录 Python并发编程01 /操作系统发展史.多进程理论 1. 操作系统 2. 进程理论 1. 操作系统 定义:管理控制协调计算机中硬件与软 ...

  10. Nginx to start, restart, shutdown and upgrade

    1.start cd usr/local/nginx/sbin ./nginx 2.restart kill -HUP PID #主进程号或进程号文件路径 #或者使用 cd /usr/local/ng ...