Design and Implementation of the Service Layer for a High‑Concurrency Seckill Application Using Java Spring
This article explains the purpose of the Service layer in a Java SSM‑based high‑concurrency seckill project, walks through interface design, DTO/VO/PO concepts, provides full Service implementation with transaction management, custom exceptions, enumeration for status codes, Spring XML configuration, and unit testing examples.
The Service layer in a multi‑tier Java application encapsulates business logic, isolates it from the controller and DAO layers, and improves code reuse and maintainability.
First, an interface SeckillService is defined with methods for retrieving seckill records, exposing the seckill URL, and executing the seckill operation.
public interface SeckillService {
List<Seckill> getSeckillList();
Seckill getById(long seckillId);
Exposer exportSeckillUrl(long seckillId);
SeckillExecution executeSeckill(long seckillId, long userPhone, String md5) throws SeckillException;
}Several data‑transfer objects are introduced to avoid returning raw POJOs: Exposer (contains URL exposure info), SeckillExecution (holds execution result), and custom exception classes ( SeckillException, SeckillCloseException, RepeatKillException) to represent domain‑specific error conditions.
public class Exposer {
private boolean exposed;
private String md5;
private long seckillId;
private LocalDateTime now, start, end;
// constructors, getters, setters, toString()
} public class SeckillExecution {
private long seckillId;
private int state;
private String stateInfo;
private SuccessKilled successKilled;
// constructors using SeckillStatEnum, getters, setters, toString()
}Business logic is implemented in SeckillServiceImpl, annotated with @Service. It uses injected mappers, a salt for MD5 generation, and Redis caching. The exportSeckillUrl method checks product existence, validates the seckill time window, and returns an Exposer. The executeSeckill method validates the MD5, performs inventory reduction, inserts a purchase record, and returns a SeckillExecution with appropriate status from SeckillStatEnum.
@Service
public class SeckillServiceImpl implements SeckillService {
@Autowired private SeckillMapper seckillMapper;
@Autowired private SuccessKilledMapper successKilledMapper;
// ... other fields and methods
@Transactional
public SeckillExecution executeSeckill(long seckillId, long userPhone, String md5) throws SeckillException {
// MD5 validation, inventory update, purchase record insertion, exception handling
}
}Transaction management is enabled via Spring XML ( applicationContext-service.xml) which declares a DataSourceTransactionManager bean and activates annotation‑driven transactions with <tx:annotation-driven/>. The service class is scanned with context:component-scan.
Finally, a JUnit test class SeckillServiceImplTest demonstrates how to load the Spring context, autowire the service, and test each method (list retrieval, single record fetch, URL exposure, execution, and stored‑procedure execution).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
