一、文章前提

1、在学习springcloud的热更新的时候并没有提到@RefreshScope注解,因此去查询了下,并总结下热更新

二、不使用@RefreshScope

1、创建SpringConfig配置文件

image.png

2、创建配置文件的对应类(使用@Component注解,也可以不使用,而改用@EnableConfigurationProperties注解)

package com.hmall.cart.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "hm.cart")
public class CartProperties {
    private Integer maxAmount;
}

三、使用@RreshScope

1.nacos的配置文件如上
2.使用@Value注解和@RefreshScope实现

/**
 * <p>
 * 订单详情表 服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2023-05-05
 */
@Service
@RequiredArgsConstructor
@Data
@RefreshScope
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {

    //1.这里RequiredArgsConstructor是lombok注解,用于生成构造器,但他只为final修饰的变量进行构造器创建,没有的话就不会注入构造器中
    //2.这里就不需要书写@AutoWired,也是spring推荐的注入方式
    private final IItemService itemService;

//    private final RestTemplate restTemplate;
//
//    private final DiscoveryClient discoveryClient;

    private final ItemClient itemClient;


//    private final CartProperties cartProperties;

    @Value("${hm.cart.maxAmount}")
    private String count;

原来是小袁呐
1 声望0 粉丝