| Crates.io | spring |
| lib.rs | spring |
| version | 0.4.4 |
| created_at | 2024-08-07 04:43:51.684699+00 |
| updated_at | 2025-09-19 17:02:44.674235+00 |
| description | Rust microservice framework like spring boot in java |
| homepage | |
| repository | https://github.com/spring-rs/spring-rs |
| max_upload_size | |
| id | 1328063 |
| size | 114,843 |
spring-rs is an application framework that emphasizes convention over configuration, inspired by Java's SpringBoot. spring-rs provides an easily extensible plug-in system for integrating excellent projects in the Rust community, such as axum, sqlx, sea-orm, etc.
Compared with SpringBoot in java, spring-rs has higher performance and lower memory usage, allowing you to completely get rid of the bloated JVM and travel light.
web
use spring::{auto_config, App};
use spring_sqlx::{
sqlx::{self, Row},
ConnectPool, SqlxPlugin
};
use spring_web::{get, route};
use spring_web::{
error::Result, extractor::{Path, Component}, handler::TypeRouter, axum::response::IntoResponse, Router,
WebConfigurator, WebPlugin,
};
use anyhow::Context;
#[auto_config(WebConfigurator)]
#[tokio::main]
async fn main() {
App::new()
.add_plugin(SqlxPlugin)
.add_plugin(WebPlugin)
.run()
.await
}
#[get("/")]
async fn hello_world() -> impl IntoResponse {
"hello world"
}
#[route("/hello/{name}", method = "GET", method = "POST")]
async fn hello(Path(name): Path<String>) -> impl IntoResponse {
format!("hello {name}")
}
#[get("/version")]
async fn sqlx_request_handler(Component(pool): Component<ConnectPool>) -> Result<String> {
let version = sqlx::query("select version() as version")
.fetch_one(&pool)
.await
.context("sqlx query failed")?
.get("version");
Ok(version)
}
job
use anyhow::Context;
use spring::{auto_config, App};
use spring_job::{cron, fix_delay, fix_rate};
use spring_job::{extractor::Component, JobConfigurator, JobPlugin};
use spring_sqlx::{
sqlx::{self, Row},
ConnectPool, SqlxPlugin,
};
use std::time::{Duration, SystemTime};
#[auto_config(JobConfigurator)]
#[tokio::main]
async fn main() {
App::new()
.add_plugin(JobPlugin)
.add_plugin(SqlxPlugin)
.run()
.await;
tokio::time::sleep(Duration::from_secs(100)).await;
}
#[cron("1/10 * * * * *")]
async fn cron_job(Component(db): Component<ConnectPool>) {
let time: String = sqlx::query("select TO_CHAR(now(),'YYYY-MM-DD HH24:MI:SS') as time")
.fetch_one(&db)
.await
.context("query failed")
.unwrap()
.get("time");
println!("cron scheduled: {:?}", time)
}
#[fix_delay(5)]
async fn fix_delay_job() {
let now = SystemTime::now();
let datetime: sqlx::types::chrono::DateTime<sqlx::types::chrono::Local> = now.into();
let formatted_time = datetime.format("%Y-%m-%d %H:%M:%S");
println!("fix delay scheduled: {}", formatted_time)
}
#[fix_rate(5)]
async fn fix_rate_job() {
tokio::time::sleep(Duration::from_secs(10)).await;
let now = SystemTime::now();
let datetime: sqlx::types::chrono::DateTime<sqlx::types::chrono::Local> = now.into();
let formatted_time = datetime.format("%Y-%m-%d %H:%M:%S");
println!("fix rate scheduled: {}", formatted_time)
}
spring-web: Based on axumspring-sqlx: Integrated with sqlxspring-postgres: Integrated with rust-postgresspring-sea-orm: Integrated with sea-ormspring-redis: Integrated with redisspring-mail: Integrated with lettrespring-job: Integrated with tokio-cron-schedulerspring-stream: Integrate sea-streamer to implement message processing such as redis-stream and kafkaspring-opentelemetry: Integrate with opentelemetry to implement full observability of logging, metrics, tracingspring-grpc: Integratetonic to implement gRPC callsspring-opendal: Integrate with opendal to implement unified data access layerWe also welcome community experts to contribute their own plugins. Contributing →
Click here to view common problems encountered when using spring-rs Help →