Skip to content

Spring 集成概述

LCGYL Framework 提供与 Spring 生态系统的无缝集成,让你可以在 Spring Boot 应用中使用框架的所有功能。

为什么集成 Spring?

优势

  • 生态丰富:利用 Spring 庞大的生态系统
  • 开发效率:Spring Boot 的自动配置
  • 企业级支持:成熟的企业级解决方案
  • 社区活跃:大量的文档和社区支持

集成方式

LCGYL Framework 提供两种使用方式:

  1. 独立使用:不依赖 Spring,使用框架自身的 IoC 容器
  2. Spring 集成:与 Spring Boot 集成,使用 Spring 的依赖注入

快速开始

添加依赖

gradle
dependencies {
    implementation 'com.lcgyl:lcgyl-framework-spring:${version}'
}
xml
<dependency>
    <groupId>com.lcgyl</groupId>
    <artifactId>lcgyl-framework-spring</artifactId>
    <version>${version}</version>
</dependency>

启用框架

java
@SpringBootApplication
@EnableLcgylFramework
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

配置

yaml
# application.yml
lcgyl:
  enabled: true
  plugin:
    enabled: true
    scan-packages:
      - com.example.plugins
  event:
    async: true
    thread-pool-size: 10

核心集成

IoC 容器集成

LCGYL 组件自动注册为 Spring Bean:

java
// LCGYL 组件
@Component
public class UserService {
    // 自动成为 Spring Bean
}

// 在 Spring 组件中使用
@Service
public class OrderService {
    
    @Autowired
    private UserService userService;  // 可以注入 LCGYL 组件
}

事件总线集成

java
// 发布 LCGYL 事件
@Service
public class OrderService {
    
    @Autowired
    private EventBus eventBus;
    
    public void createOrder(Order order) {
        orderRepository.save(order);
        eventBus.publish(new OrderCreatedEvent(order));
    }
}

// 监听 LCGYL 事件
@Component
public class OrderEventListener {
    
    @Subscribe
    public void onOrderCreated(OrderCreatedEvent event) {
        // 处理事件
    }
}

// 也可以监听 Spring 事件
@Component
public class SpringEventListener {
    
    @EventListener
    public void onApplicationReady(ApplicationReadyEvent event) {
        // 应用启动完成
    }
}

配置集成

java
// 使用 Spring 配置
@ConfigurationProperties(prefix = "app")
public class AppProperties {
    private String name;
    private int maxConnections;
    // getters and setters
}

// 使用 LCGYL 配置
@Component
public class MyService {
    
    @Value("app.name")
    private String appName;
    
    @Inject
    private Configuration configuration;
    
    public void doSomething() {
        String name = configuration.getString("app.name");
    }
}

自动配置

插件自动配置

java
@Configuration
@ConditionalOnProperty(name = "lcgyl.plugin.enabled", havingValue = "true")
public class PluginAutoConfiguration {
    
    @Bean
    @ConditionalOnMissingBean
    public PluginManager pluginManager() {
        return new DefaultPluginManager();
    }
    
    @Bean
    @ConditionalOnMissingBean
    public PluginRegistry pluginRegistry(PluginManager pluginManager) {
        return new DefaultPluginRegistry(pluginManager);
    }
}

事件总线自动配置

java
@Configuration
@ConditionalOnProperty(name = "lcgyl.event.enabled", havingValue = "true", matchIfMissing = true)
public class EventBusAutoConfiguration {
    
    @Bean
    @ConditionalOnMissingBean
    public EventBus eventBus(EventBusProperties properties) {
        return new DefaultEventBus(properties.isAsync(), properties.getThreadPoolSize());
    }
}

条件注解

常用条件注解

java
@Configuration
public class ConditionalConfig {
    
    // 当类存在时
    @Bean
    @ConditionalOnClass(name = "com.example.SomeClass")
    public SomeService someService() {
        return new SomeService();
    }
    
    // 当 Bean 不存在时
    @Bean
    @ConditionalOnMissingBean
    public DefaultService defaultService() {
        return new DefaultService();
    }
    
    // 当配置属性满足条件时
    @Bean
    @ConditionalOnProperty(name = "feature.enabled", havingValue = "true")
    public FeatureService featureService() {
        return new FeatureService();
    }
    
    // 当环境是生产环境时
    @Bean
    @Profile("prod")
    public ProductionService productionService() {
        return new ProductionService();
    }
}

生命周期集成

启动顺序

1. Spring 容器启动
2. LCGYL 框架初始化
3. 插件加载
4. 组件扫描和注册
5. 事件总线启动
6. 应用就绪

生命周期回调

java
@Component
public class LifecycleListener {
    
    // Spring 生命周期
    @PostConstruct
    public void init() {
        // Bean 初始化后
    }
    
    @PreDestroy
    public void destroy() {
        // Bean 销毁前
    }
    
    // LCGYL 生命周期
    @OnStart
    public void onStart() {
        // 框架启动时
    }
    
    @OnStop
    public void onStop() {
        // 框架停止时
    }
}

模块列表

模块描述
lcgyl-framework-springSpring Boot 启动器
lcgyl-spring-boot-autoconfigure自动配置
lcgyl-spring-data数据访问集成
lcgyl-spring-webWeb 集成
lcgyl-spring-security安全集成
lcgyl-spring-cloud微服务集成

版本兼容性

LCGYL 版本Spring Boot 版本Java 版本
1.0.x3.0.x - 3.2.x17+
1.1.x3.2.x - 3.3.x21+
2.1.x3.3.x+21+

下一步

Released under the Apache License 2.0