简体   繁体   English

在 Spring Boot 应用程序中跨不同组件共享数据

[英]Sharing data across different components in the spring boot application

I have some authorization info (json data) that comes in a request header of a GET request in a spring-boot application.我在 spring-boot 应用程序的 GET 请求的请求标头中提供了一些授权信息(json 数据)。

I have a HandlerInterceptor which reads this header in preHandle function and stores this JSON data into an object of class AuthData .我有一个 HandlerInterceptor,它在preHandle函数中读取此标头并将此 JSON 数据存储到AuthData类的对象中。 I'd like to share this data with other classes/services and components without passing it around as an object.我想与其他类/服务和组件共享这些数据,而不是将其作为对象传递。 Probably using some spring feature.可能使用了一些弹簧功能。

I've tried to annotate the AuthData class as @Service and in the HandlerInterceptor and other places where I want to read this data with我试图将AuthData类注释为@Service并在 HandlerInterceptor 和其他我想读取这些数据的地方

@Autowired
private AuthData authData;

But the classes where I want to read the authData, it is always null.但是我想读取 authData 的类,它总是为空的。

What would be the best way to do that?最好的方法是什么?

Thanks!谢谢!

Not specific to Spring.不特定于 Spring。 Usually this type of information is put in a thread local and accessed where required.通常这种类型的信息放在本地线程中,并在需要的地方访问。 However, the thread local needs to be set and cleared from one place, for example a filter or Valve in Tomcat which handles the request before any library.但是,本地线程需要从一个地方设置和清除,例如 Tomcat 中的过滤器或 Valve,它在任何库之前处理请求。 I am not well versed in Spring, the framework should have such a point when the thread local can be set and cleared.我对Spring不是很熟悉,框架应该有这么一个点,可以设置和清除线程本地。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM