Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TokenBucketAlgorithmImpl这个类里面的rateLimiter这个对象是怎么注入进去的 #5

Closed
mrsWangxing opened this issue Nov 6, 2018 · 4 comments

Comments

@mrsWangxing
Copy link

我下载到源码后 必须在rateLimiter这个变量上加@Autowired这个注解才能注册进去值 否则 打印出来rateLimiter为空
是我的环境的问题?还是框架有问题呢?

@shiyujun
Copy link
Owner

shiyujun commented Nov 7, 2018

我下载到源码后 必须在rateLimiter这个变量上加@Autowired这个注解才能注册进去值 否则 打印出来rateLimiter为空
是我的环境的问题?还是框架有问题呢?

有没有增加启动注解@EnableSyjRateLimit呢,如果增加了启动注解的话会在EnableSyjRateLimitConfiguration这个类里面自动装配的

@shiyujun shiyujun closed this as completed Nov 8, 2018
@mrsWangxing
Copy link
Author

@EnableSyjRateLimit 这个注解加了
EnableSyjRateLimitConfiguration这个类确实会在spring容器中放入 一个RateLimiter类型的对象
但是 在 TokenBucketAlgorithmImpl 这个类里面 的 rateLimiter 这个属性 没有@Autowired 在consume方法中用的时候 会报空指针异常
假如不加这个注解 @Autowired ;是什么原理把rateLimiter 注入到TokenBucketAlgorithmImpl 这个类里面的呢

@shiyujun
Copy link
Owner

@EnableSyjRateLimit 这个注解加了
EnableSyjRateLimitConfiguration这个类确实会在spring容器中放入 一个RateLimiter类型的对象
但是 在 TokenBucketAlgorithmImpl 这个类里面 的 rateLimiter 这个属性 没有@Autowired 在consume方法中用的时候 会报空指针异常
假如不加这个注解 @Autowired ;是什么原理把rateLimiter 注入到TokenBucketAlgorithmImpl 这个类里面的呢

这个对象注入主要是使用了Lombok插件的@requiredargsconstructor,这个注解为类提供了一个包含带有@nonnull注解变量的构造方法,Spring在执行时就会自动把这些变量注入进去。你的情况可能是因为本地开发工具没有集成Lombok的原因。可以选择使用maven中央仓库的依赖或者开发工具增加Lombok集成环境

@shiyujun shiyujun reopened this Nov 14, 2018
@mrsWangxing
Copy link
Author

嗯 应该是这个原因

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants