一次由Spring构造注入引发的错误Parameter 1 of constructor in org.rongyilian.service.impl.VerificationCodeService

SrpringBoot服务启动后报错如下:

Description:

Parameter 1 of constructor in org.rongyilian.service.impl.VerificationCodeServiceImpl required a bean of type 'java.lang.String' that could not be found.


Action:

Consider defining a bean of type 'java.lang.String' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:53222', transport: 'socket'

Process finished with exit code 1

以下是本地代码:

@Service
@AllArgsConstructor
public class VerificationCodeServiceImpl implements VerificationCodeService {
    private final RedisService redisService;

    @Value("${redis.key.register}")
    private String REDIS_REGISTER_KEY;
    @Value("${redis.expire.three}")
    private Long THREE_MIN;
    @Override
    public void sendVerificationCode(String phone, String code) {
        String key = REDIS_REGISTER_KEY + ":" + phone;
        redisService.set(key, code, THREE_MIN);
    }
}

报错后我首先想到的是配置文件没注入的问题,检查yml配置文件中是否有语法错误

redis:
  key:
    register: 'user:register'
  expire:
    common: 86400 # 24小时
    three: 180 #3分钟

检查后发现语法正确,百度搜索,说报错信息是由于A类中定义了含参数的构造函数,Spring自动构造和注入时未为该Bean传入参数,引起报错。
反复检查RedisService类发现没有问题

最后发现是由于引用了Lombok的@AllArgsConstructor注解,创造了一个全参的构造器,将@Value修饰的两个变量作为构造器入参,导致报错
以下是修改后的代码,正常启动

@Service
public class VerificationCodeServiceImpl implements VerificationCodeService {
    private final RedisService redisService;

    public VerificationCodeServiceImpl(RedisService redisService) {
        this.redisService = redisService;
    }
    @Value("${redis.key.register}")
    private String REDIS_REGISTER_KEY;
    @Value("${redis.expire.three}")
    private Long THREE_MIN;
    @Override
    public void sendVerificationCode(String phone, String code) {
        String key = REDIS_REGISTER_KEY + ":" + phone;
        redisService.set(key, code, THREE_MIN);
    }
}
  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值