Skip to content

Spring 集成

如果你是 Spring 开发者,这里是你的专属入口。LCGYL Framework 可以与 Spring Boot 无缝集成。

为什么选择集成?

方式适用场景优点
独立使用新项目、微服务、CLI 工具零依赖、启动快、资源少
Spring 集成现有 Spring 项目、企业应用利用 Spring 生态、平滑迁移

集成方式

方式一:在 Spring Boot 中使用 LCGYL 插件

gradle
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'com.lcgyl:lcgyl-framework-spring:2.2.0'
    implementation 'com.lcgyl:lcgyl-cache-plugin:2.2.0'
}
java
@SpringBootApplication
@EnableLcgylPlugins
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

方式二:双向 Bean 同步

LCGYL 和 Spring 的 Bean 可以互相注入:

java
// Spring Bean 注入到 LCGYL 组件
@Component  // LCGYL 注解
public class MyService {
    @Inject
    private JdbcTemplate jdbcTemplate;  // Spring Bean
}

// LCGYL Bean 注入到 Spring 组件
@Service  // Spring 注解
public class SpringService {
    @Autowired
    private EventBus eventBus;  // LCGYL Bean
}

快速对比

注解对比

功能LCGYLSpring
组件@Component@Component
注入@Inject@Autowired
配置值@Value@Value
初始化@PostConstruct@PostConstruct
主要@Primary@Primary
条件@Conditional@Conditional

配置对比

yaml
# LCGYL 配置
lcgyl:
  cache:
    type: local
    ttl: 3600

# Spring 配置
spring:
  cache:
    type: simple

迁移指南

从 Spring 迁移到 LCGYL

如果你想完全迁移到 LCGYL:

  1. 替换注解

    java
    // Before (Spring)
    @Autowired
    private UserService userService;
    
    // After (LCGYL)
    @Inject
    private UserService userService;
  2. 替换配置

    properties
    # Before (Spring)
    spring.datasource.url=jdbc:mysql://localhost:3306/db
    
    # After (LCGYL)
    lcgyl.datasource.url=jdbc:mysql://localhost:3306/db
  3. 替换启动类

    java
    // Before (Spring)
    @SpringBootApplication
    public class App {
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    }
    
    // After (LCGYL)
    public class App {
        public static void main(String[] args) {
            Application.run(App.class, args);
        }
    }

渐进式迁移

不需要一次性全部迁移,可以:

  1. 先添加 lcgyl-framework-spring 依赖
  2. 新功能使用 LCGYL 插件
  3. 逐步替换旧代码

常见问题

Q: 会有 Bean 冲突吗?

不会。LCGYL 和 Spring 使用独立的容器,通过桥接层实现互通。

Q: 性能有影响吗?

影响极小。桥接层只在启动时执行一次同步。

Q: 哪些 Spring 功能可以继续使用?

全部!Spring 的所有功能都可以正常使用,LCGYL 只是增强。

下一步

Released under the Apache License 2.0