Kết hợp Spring6 với JUnit
1. Sử dụng JUnit4
Lớp User:
package com.example.spring.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
@Value("Nguyễn Văn A")
private String ten;
@Override
public String toString() {
return "User{" +
"ten='" + ten + '\'' +
'}';
}
public String getTen() {
return ten;
}
public void setTen(String ten) {
this.ten = ten;
}
public User() {
}
public User(String ten) {
this.ten = ten;
}
}
Cấu hình XML: Bật quét thành phần
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<context:component-scan base-package="com.example.spring.bean"/>
</beans>
Test class:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class TestJunit4 {
@Autowired
private User nguoiDung;
@Test
public void test1(){
System.out.println(nguoiDung.getTen());
}
}
2. Sử dụng JUnit5:
@SpringJUnitConfig(locations = "classpath:spring.xml")
public class TestJunit5 {
@Autowired
private User nguoiDung;
@Test
public void test1(){
System.out.println(nguoiDung.getTen());
}
}
Tích hợp Spring6 với MyBatis
Quy trình tích hợp:
- Chuẩn bị bảng cơ sở dữ liệu
- Tạo module trong IDE và thêm các dependency cần thiết:
- spring-context
- spring-jdbc
- MySQL driver
- mybatis
- mybatis-spring
- Druid connection pool
- junit
- Thiết lập kiến trúc 3 lớp và tạo các package cần thiết
- Viết lớp POJO
- Viết mapper interface
- Viết file cấu hình mapper
- Viết interface service và class implementation
- Viết file jdbc.properties
- Viết file mybatis-config.xml
- Viết file spring.xml với cấu hình:
- Quét thành phần
- Import file thuộc tính bên ngoài
- Cấu hình nguồn dữ liệu
- Cấu hình SqlSessionFactoryBean
- Cấu hình mapper scanner
- Cấu hình transaction manager
- Kích hoạt transaction annotation
- Viết test program, thêm transaction và test
Resource trong Spring
Java URL class không đáp ứng được tất cả các yêu cầu truy cập tài nguyên low-level. Spring Resource cung cấp khả năng truy cập tài nguyên low-level mạnh mẽ hơn.
1. Resource interface
Spring Resource interface nằm trong org.springframework.core.io, cung cấp các phương thức quan trọng:
- getInputStream(): Mở và trả về InputStream để đọc tài nguyên
- exists(): Kiểm tra tài nguyên có tồn tại hay không
- isOpen(): Kiểm tra tài nguyên có đang mở stream hay không
- getDescription(): Trả về mô tả tài nguyên
2. Các lớp triển khai của Resource
UrlResource - Truy cập tài nguyên mạng
Triển khai Resource để truy cập tài nguyên mạng, hỗ trợ đường dẫn URL tuyệt đối:
- http: - Truy cập tài nguyên dựa trên HTTP protocol
- ftp: - Truy cập tài nguyên dựa trên FTP protocol
- file: - Đọc tài nguyên từ hệ thống tệp