I'm currently working on a web application using Angular 7 for front-end, spring-boot for back-end (in which I'm developing a restful web service). I'm using @Autowired annotation to inject my services into each other and my rest controller. The problem is that in some of my services, there are some attributes that get shared when the injection is done. How do I prevent that?
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class ServiceA {
private boolean test;
public ServiceA (){
test = true;
}
public changeValues(){
test = false;
}
}
@Service
public class ServiceB {
@Autowired
private ServiceA serivceA;
public void method1() {
serviceA.changeValues();
}
}
@Service
public class ServiceC {
@Autowired
private ServiceA serivceA;
public void method2(){
if(serviceA.getTest()){
doSomethingNeeded();
}
}
}
public class Application{
@Autowired
private ServiceB b;
@Autowired
private ServiceC c;
public static void main(String[] args) {
b.method1();
c.method2();
}
}
In this case the method doSomethingNeeded()
in ServiceC
won't be able to be executed because the ressource 'test' of ServiceA
is shared between both ServiceB
and ServiceC
. How do I prevent That?
P.S. In my case, the dependency injections are way too complex for applying any modifications to the services, that's why I need a way to configure spring-ioc
dependency injection in a way to create instances of private attributes to each client session.