信通院可信开源社区、可信开源项目,信创国产化,spring全家桶平替,solon,更快、更小、更简单
写在前面
随着信创项目的发展,国产化的步伐越来越稳了,作为Java后端开发必学必会的框架spring生态,已经形成了后端技术栈代表,信创项目讲究自主可控,国产开源solon应运而生,值得大家一学。
Init
和 Spring Boot 一样,Solon 同样提供了初始器,一键生成项目原生结构。
初始器地址:https://solon.noear.org/start/
选择项目构建方式、语音类型、版本、包相关信息,一键生成:
项目入口
App,只需要添加 SolonMain 标签即可。
package com.example.demo;
import org.noear.solon.Solon;
import org.noear.solon.annotation.SolonMain;
@SolonMain
public class App {
public static void main(String[] args) {
Solon.start(App.class, args);
}
}
DemoController
hello world 示例方法,注解和 Spring Boot 类似。
package com.example.demo;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Param;
import org.noear.solon.core.handle.ModelAndView;
@Controller
public class DemoController {
@Mapping("/hello")
public String hello(@Param(defaultValue = "world") String name) {
return String.format("Hello %s!", name);
}
@Mapping("/hello2")
public ModelAndView hello2(@Param(defaultValue = "world") String name) {
return new ModelAndView("hello2.ftl").put("name", name);
}
}
配置文件 app.yml
配置文件格式和Spring 同样类似,一脉相承。
server.port: 8080
solon.app:
name: 'demo-app'
group: 'demo'
项目依赖 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.noear</groupId>
<artifactId>solon-parent</artifactId>
<version>2.9.2</version>
<relativePath />
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<description>Demo project for Solon</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-web</artifactId>
</dependency>
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-view-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-logging-logback</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.noear</groupId>
<artifactId>solon-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>tencent</id>
<url>https://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
运行
直接启动 App,控制台即可打印出启动日志
solon.connector:main: smarthttp: Started ServerConnector@{HTTP/1.1,[http/1.1]}{http://localhost:8080}
Server:main: smarthttp: Started (smart http 1.5/2.9.2) @169ms
169毫秒启动,快的飞起。
Api测试
你也来试试吧!