我目前正在开发一个web应用程序,前端使用Angular 7,后端使用spring-boot (我正在开发一个restful web服务)。我使用@Autowired注解将我的服务注入到彼此和我的rest控制器中。问题是,在我的一些服务中,有一些属性在注入完成时会被共享。我如何防止这种情况发生?
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();
}
}
在这种情况下,ServiceC
中的doSomethingNeeded()
方法将无法执行,因为ServiceA
的资源“测试”在ServiceB
和ServiceC
之间共享。我如何防止这种情况发生?
在我的例子中,依赖注入对于将任何修改应用到服务来说太复杂了,这就是为什么我需要一种方法来配置spring-ioc
依赖注入,以便为每个客户端会话创建私有属性的实例。
https://stackoverflow.com/questions/56134058
复制相似问题