Spring学习总结(3)-了解Spring框架

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();
        }

}
复制代码

 

posted @   闲人鹤  阅读(180)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示