This is first class:
@Service
public class ServiceA{
@Autowired
ServiceB serviceB;
String neededString;
public void methodSome(){
//uses neededString
}
}
This is second
@Service
public class ServiceB{
String neededString;
public void methodSome(){
//uses neededString
}
}
and i need to get the variable from another service:
neededString = thirdServiceToReturn();
Both classes need that neededString variable to do their own methods. I can get from 3. service in the method seperately or i can get with PostConstruct for each class.
But i want to get from a common place for both classes. Maybe from 2. class . I can try static but it wont be thread safe i guess.
The variable wont change for months maybe. It takes some values and returns from database. After some deploy, it can change so, it should get new one at some point.
I use Spring Boot
also. What do you suggest?
Maybe a Constant
class with @Bean
and do the initializing there and then Autowired
to second class? Like that:
Is there any way to pass @Autowired variable to some other class in a Spring Boot application?
The variable will change in future. It is reading from database, which is derived with @cached so it will read from cache until the application is restarted or is deployed. Variable needs to be parametric, not hardcoded.
Static variable
is not thread safe. I want to reach the solution. Thread safe.