Skip to content

第一个应用

让我们创建第一个 LCGYL Framework 应用!

创建项目

使用 Gradle

创建 build.gradle 文件:

gradle
plugins {
    id 'java'
    id 'application'
}

group = 'com.example'
version = '1.0.0'
sourceCompatibility = '21'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.lcgyl:framework-core:1.0.0'
    implementation 'org.slf4j:slf4j-api:2.0.9'
    runtimeOnly 'ch.qos.logback:logback-classic:1.4.14'
}

application {
    mainClass = 'com.example.Application'
}

创建主类

创建 src/main/java/com/example/Application.java

java
package com.example;

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

@Component
public class Application {
    private static final Logger logger = LoggerFactory.getLogger(Application.class);
    
    public static void main(String[] args) {
        // 创建容器
        Container container = new Container();
        
        // 扫描组件
        container.scan("com.example");
        
        // 获取应用实例
        Application app = container.getBean(Application.class);
        app.run();
    }
    
    @Inject
    private GreetingService greetingService;
    
    public void run() {
        logger.info("应用启动成功!");
        String message = greetingService.greet("LCGYL");
        logger.info(message);
    }
}

创建服务类

创建 src/main/java/com/example/GreetingService.java

java
package com.example;

import com.lcgyl.framework.core.annotation.Component;

@Component
public class GreetingService {
    
    public String greet(String name) {
        return "Hello, " + name + "! Welcome to LCGYL Framework!";
    }
}

运行应用

使用 Gradle

bash
./gradlew run

使用 IDE

直接运行 Application 类的 main 方法。

预期输出

[INFO] 应用启动成功!
[INFO] Hello, LCGYL! Welcome to LCGYL Framework!

添加配置

创建 src/main/resources/application.properties

properties
app.name=MyFirstApp
app.version=1.0.0
greeting.prefix=Hello

更新 GreetingService

java
package com.example;

import com.lcgyl.framework.core.annotation.Component;
import com.lcgyl.framework.core.annotation.Inject;
import com.lcgyl.framework.core.config.Configuration;

@Component
public class GreetingService {
    
    @Inject
    private Configuration config;
    
    public String greet(String name) {
        String prefix = config.getString("greeting.prefix", "Hello");
        return prefix + ", " + name + "! Welcome to LCGYL Framework!";
    }
}

添加事件

创建事件类:

java
package com.example.event;

import com.lcgyl.framework.core.event.Event;

public record GreetingEvent(String name, String message) implements Event {
}

创建事件监听器:

java
package com.example.listener;

import com.example.event.GreetingEvent;
import com.lcgyl.framework.core.annotation.Component;
import com.lcgyl.framework.core.event.EventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
public class GreetingListener implements EventListener<GreetingEvent> {
    private static final Logger logger = LoggerFactory.getLogger(GreetingListener.class);
    
    @Override
    public void onEvent(GreetingEvent event) {
        logger.info("收到问候事件: {} - {}", event.name(), event.message());
    }
}

更新 GreetingService 发布事件:

java
@Component
public class GreetingService {
    
    @Inject
    private Configuration config;
    
    @Inject
    private EventBus eventBus;
    
    public String greet(String name) {
        String prefix = config.getString("greeting.prefix", "Hello");
        String message = prefix + ", " + name + "! Welcome to LCGYL Framework!";
        
        // 发布事件
        eventBus.publish(new GreetingEvent(name, message));
        
        return message;
    }
}

完整项目结构

my-first-app/
├── build.gradle
├── src/
│   └── main/
│       ├── java/
│       │   └── com/
│       │       └── example/
│       │           ├── Application.java
│       │           ├── GreetingService.java
│       │           ├── event/
│       │           │   └── GreetingEvent.java
│       │           └── listener/
│       │               └── GreetingListener.java
│       └── resources/
│           └── application.properties
└── README.md

下一步

恭喜!你已经创建了第一个 LCGYL Framework 应用。

接下来你可以:

Released under the Apache License 2.0