谷粒商城实战笔记-203-商城业务-商品详情-环境搭建

这一部分是关于商品详情页的内容,商品详情页包含五部分的内容:

  • sku基本信息
  • sku图片信息
  • spu的销售属性
  • spu的介绍
  • spu的规格参数

由下面几集构成:

  • 203-商城业务-商品详情-环境搭建
  • 204-商城业务-商品详情-模型抽取
  • 205-商城业务-商品详情-规格参数
  • 206-商城业务-商品详情-销售属性组合
  • 207-商城业务-商品详情-详情页渲染
  • 208-商城业务-商品详情-销售属性渲染
  • 209-商城业务-商品详情-sku组合切换
  • 210-商城业务-商品详情-异步编排优化

一,页面环境搭建

1,上传详情页静态资源到nignx

使用xftp将课程提供的详情页静态资源上传到nginx的html/static/item目录。

在这里插入图片描述

2,拷贝详情页html模板到product模块的templates文件夹下

在这里插入图片描述

3,新增ItemController类

在Product模块的web包下,新增ItemController类,在其中添加详情页接口。

@Controller
public class ItemController {

    @Resource
    private SkuInfoService skuInfoService;

    /**
     * 展示当前sku的详情
     * @param skuId
     * @return
     */
    @GetMapping("/{skuId}.html")
    public String skuItem(@PathVariable("skuId") Long skuId, Model model) throws ExecutionException, InterruptedException {
        return "item";
    }
}

二,后台接口逻辑实现

这个接口的实现,逻辑也比较复杂,一定要做好两步:

  • 先理解业务需求
  • 根据需求设计返回给前端的数据结构。

这两步完成之后,编码水到渠成。

@Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {

        SkuItemVo skuItemVo = new SkuItemVo();

        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            // 1、sku基本信息的获取  pms_sku_info
            SkuInfoEntity info = this.getById(skuId);
            skuItemVo.setInfo(info);
            return info;
        }, executor);


        // thenAcceptAsync 能接收到上一步的结果,但返回值
        CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync((res) -> {
            //3、获取spu的销售属性组合
            List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrBySpuId(res.getSpuId());
            skuItemVo.setSaleAttr(saleAttrVos);
        }, executor);


        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res) -> {
            //4、获取spu的介绍    pms_spu_info_desc
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setDesc(spuInfoDescEntity);
        }, executor);


        CompletableFuture<Void> baseAttrFuture = infoFuture.thenAcceptAsync((res) -> {
            //5、获取spu的规格参数信息
            List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setGroupAttrs(attrGroupVos);
        }, executor);



        //2、sku的图片信息    pms_sku_images
        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> imagesEntities = skuImagesService.getImagesBySkuId(skuId);
            skuItemVo.setImages(imagesEntities);
        }, executor);


        //等到所有任务都完成
        CompletableFuture.allOf(saleAttrFuture,descFuture,baseAttrFuture,imageFuture).get();

        return skuItemVo;
    }

三,全局线程池对象

在开发详情页时,我们使用了全局线程池,通过SpringBoot的自动配置装配机制:

  • ①读取配置文件中的配置,封装为属性Bean
  • ②在自动配置类中使用这个Bean对象封装的属性,创建线程池对象
  • ③将线程池对象注入到Spring容器中

第一步:定义配置属性类 ThreadPoolConfigProperties

首先,定义了一个配置属性类 ThreadPoolConfigProperties,用来存储线程池的配置信息。

该类使用了 @ConfigurationProperties 注解来绑定配置文件中的值到类的属性上。

项目启动时,扫描到带注解@ConfigurationProperties的类时,会创建一个该类的对象,读取配置文件当前相关的配置作为属性值,最后将对象注入到Spring容器中。

@ConfigurationProperties(prefix = "gulimall.thread")
@Data
public class ThreadPoolConfigProperties {

    private Integer coreSize;

    private Integer maxSize;

    private Integer keepAliveTime;
}

这里指定了前缀 gulimall.thread,意味着可以通过在配置文件(如 application.ymlapplication.properties)中添加以下类似的配置项来设置线程池的参数:

gulimall:
  thread:
    coreSize: 20
    maxSize: 200
    keepAliveTime: 10

第二步:启用配置属性绑定

为了让 Spring Boot 自动绑定这些配置,我们需要在配置类上使用 @EnableConfigurationProperties 注解,并指定配置类的类型。

@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
@Configuration
public class MyThreadConfig {
    // ...
}

@EnableConfigurationProperties 注解告诉 Spring 容器,当解析配置时应该激活并实例化指定的配置属性类。这意味着当 Spring Boot 启动时,它会查找带有此注解的类,并根据配置文件中的值填充这些类的属性。

第三步:创建线程池 Bean

在配置类 MyThreadConfig 中,我们通过 @Bean 方法创建了一个 ThreadPoolExecutor 实例并返回它。这将允许 Spring 容器管理这个线程池 Bean。

@Bean
public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool) {
    return new ThreadPoolExecutor(
            pool.getCoreSize(),
            pool.getMaxSize(),
            pool.getKeepAliveTime(),
            TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(100000),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy()
    );
}

当 Spring 容器扫描到这个配置类时,它会执行 @Bean 方法来创建 ThreadPoolExecutor 实例。这里有几个关键点需要注意:

  1. 注入配置属性ThreadPoolConfigProperties pool 是方法参数,Spring 会自动注入配置好的 ThreadPoolConfigProperties 实例。这是因为我们在配置类上使用了 @EnableConfigurationProperties 注解。

  2. 创建线程池:在 threadPoolExecutor 方法中,我们根据从 pool 对象获取的配置值来创建 ThreadPoolExecutor。例如,pool.getCoreSize() 返回核心线程数等。

  3. Bean 注入:通过 @Bean 注解,Spring 容器将负责管理 ThreadPoolExecutor 的生命周期。这意味着线程池可以被其他组件注入和使用。

Spring 如何获取配置

在创建 ThreadPoolExecutor Bean 的过程中,Spring 会通过以下步骤来获取配置:

  1. 读取配置文件:当 Spring Boot 启动时,它会读取配置文件中的配置项,并尝试与带有 @ConfigurationProperties 注解的类进行绑定。

  2. 实例化配置类:Spring 会实例化 ThreadPoolConfigProperties 类,并使用配置文件中的值填充其属性。

  3. 注入配置对象:当 @Bean 方法被调用时,Spring 会自动注入已经实例化并填充好的 ThreadPoolConfigProperties 对象,即 pool 参数。

  4. 创建线程池:最后,ThreadPoolExecutor 使用从 pool 获取的配置值来初始化。

总结

通过上述步骤,可以看到 Spring Boot 是如何自动地配置线程池的。

配置文件中的值通过 @ConfigurationProperties 绑定到了 ThreadPoolConfigProperties 类中.

然后通过 @EnableConfigurationProperties@Bean 方法,Spring 容器能够自动创建并管理线程池 Bean。

这种机制极大地简化了配置过程,使得开发人员可以更加专注于业务逻辑而不是配置细节。

根据引用中提供的信息,谷粒商城是一个B2C模式的电商平台,主要销售自营商品给客户。B2C模式指的是商对客的模式,即商家直接向消费者销售商品和服务。谷粒商城使用微服务架构,这是一种将应用程序拆分成独立且可独立开发、部署和扩展的小型服务的架构。引用还提到谷粒商城的技术涵盖微服务架构、分布式、全栈、集群、部署和自动化运维等方面。 因此,从前慢-谷粒商城篇章6主要讨论的可能是谷粒商城的技术方面,包括微服务架构、分布式、全栈、集群、部署和自动化运维等内容。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [从前慢-谷粒商城篇章1](https://blog.csdn.net/m0_67391121/article/details/125383572)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [2020年谷粒商城md笔记](https://download.csdn.net/download/ZHUXIUQINGIT/12543161)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小手追梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值