Skip to content

快速开始

本指南将帮助你在 5 分钟内开始使用 LCGYL Framework。

前置要求

  • JDK 21 或更高版本
  • Gradle 8.0+ 或 Maven 3.8+
  • 你喜欢的 IDE(推荐 IntelliJ IDEA)

安装

使用 Gradle

build.gradle 中添加依赖:

gradle
dependencies {
    implementation 'com.lcgyl:lcgyl-framework-core:2.2.0'
}

使用 Maven

pom.xml 中添加依赖:

xml
<dependency>
    <groupId>com.lcgyl</groupId>
    <artifactId>lcgyl-framework-core</artifactId>
    <version>2.2.0</version>
</dependency>

Hello World

创建你的第一个 LCGYL 应用:

java
import com.lcgyl.framework.core.annotation.Component;
import com.lcgyl.framework.core.annotation.Inject;
import com.lcgyl.framework.core.Application;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
public class HelloWorld {
    
    private static final Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    
    public void sayHello() {
        logger.info("Hello, LCGYL Framework!");
    }
    
    public static void main(String[] args) {
        // 启动应用
        Application app = Application.run(HelloWorld.class, args);
        
        // 获取组件
        HelloWorld hello = app.getBean(HelloWorld.class);
        hello.sayHello();
    }
}

运行程序,你将看到:

Hello, LCGYL Framework!

依赖注入

LCGYL 提供了简单而强大的依赖注入:

java
@Component
public class UserService {
    
    private final UserRepository userRepository;
    
    @Inject
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
    
    public User findById(Long id) {
        return userRepository.findById(id);
    }
}

@Component
public class UserRepository {
    
    public User findById(Long id) {
        // 查询用户
        return new User(id, "张三");
    }
}

事件驱动

使用事件总线进行组件间通信:

java
// 定义事件
public record UserCreatedEvent(User user) implements Event {
}

// 发布事件
@Component
public class UserService {
    
    @Inject
    private EventBus eventBus;
    
    public User createUser(String name) {
        User user = new User(name);
        // 发布事件
        eventBus.publish(new UserCreatedEvent(user));
        return user;
    }
}

// 监听事件
@Component
public class EmailService {
    
    @Subscribe
    public void onUserCreated(UserCreatedEvent event) {
        // 发送欢迎邮件
        sendWelcomeEmail(event.user());
    }
}

配置管理

使用配置文件管理应用配置:

properties
# application.properties
app.name=MyApp
app.version=1.0.0
app.server.port=8080
java
@Component
public class AppConfig {
    
    @Value("app.name")
    private String name;
    
    @Value("app.version")
    private String version;
    
    @Value("app.server.port")
    private int port;
}

下一步

恭喜!你已经学会了 LCGYL Framework 的基础用法。

深入学习

核心功能

进阶功能

Spring 用户

如果你熟悉 Spring Framework:

实战示例

Released under the Apache License 2.0