SWAK: A Swiss‑Army‑Knife‑Style Multi‑Implementation Framework for Xianyu Backend
SWAK is a lightweight, Spring-compatible framework that treats product type, category, and region tags as rules, registers multiple implementations during startup, and at runtime selects the appropriate one via priority and reduction, eliminating tangled if-else code, improving reuse, onboarding, and performance for Xianyu’s backend.
The rapid growth of Xianyu’s business and the increasing variety of product types have led to serious code "bad smells": tight coupling between platform and business code and tangled implementations across different business domains.
To address these issues, Xianyu developed SWAK (Swiss Army Knife), a lightweight, flexible framework that enables rule‑based execution of multiple implementations. The framework abstracts common logic while allowing variable logic to be implemented independently for each tag (e.g., product type, category, region).
Typical code for handling different product types often becomes a long chain of if‑else statements, which hampers reuse, increases risk, and raises onboarding difficulty.
if(A类型) {
if(A1类型) {
doSomething1();
}else if(A2类型) {
doSomething2();
}
} else if(B类型) {
doSomething3();
} else if(C类型) {
if(C1类型) {
doSomething4();
}else if(C2类型) {
doSomething5();
}
}SWAK treats each label (type, category, region) as a tag. When an object carries multiple tags, conflicts are resolved by a "rule" consisting of priority and reduction (merge) strategies.
Analyze the tags of the target object.
Separate immutable logic from mutable logic; mutable logic is expressed as interfaces.
Provide independent implementations for each tag.
When multiple tags apply, use priority and reduction to decide the final outcome.
The framework’s core ideas are inspired by Alibaba’s TMF architecture.
Implementation consists of two phases: registration and execution. During registration, SWAK scans annotations or XML configurations to register interfaces, implementations, priority, and reduction strategies into a local cache. Execution then retrieves the appropriate implementation directly from the cache, eliminating runtime scanning and improving response time.
SWAK is fully compatible with Spring; all beans, including SWAK’s own, are managed by the Spring container. The framework uses CGLIB proxies to weave the execution flow transparently.
/**
* Simple demo of configuration; real business is far more complex
*/
@SwakInterface(desc="获取subtitle")
public interface SubtitleFetcher {
@SwakMethod
String fetchSubtitle();
}
@SwakTag(tags = {"tagA"})
@Component
public class TagASubtitleFetcher implements SubtitleFetcher {
@Override
public String fetchSubtitle() {
return "我是TagA";
}
}
@SwakTag(tags = {"tagB"})
@Component
public class TagBSubtitleFetcher implements SubtitleFetcher {
@Override
public String fetchSubtitle() {
return "我是TagB";
}
} @Autowired
private SubtitleFetcher subtitleFetcher;
// ...
String subtitle = subtitleFetcher.fetchSubtitle();
// ...SWAK has been applied to Xianyu’s product publishing and editing flows, and the team plans to extend it to more business processes. Benefits include clearer code separation, higher reuse, isolated implementations per tag, reduced development and testing cost, and faster onboarding for new developers.
In summary, SWAK provides a rule‑based, multi‑implementation solution that decouples platform and business logic, works seamlessly with Spring, and acts like a Swiss‑army‑knife for various backend scenarios.
Xianyu Technology
Official account of the Xianyu technology team
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.