在java中,同一个实例如何在另一个类的不同方法上工作



基于提供的参数,在多个方法中使用相同的实例。我们可以在这里考虑线程安全性吗。假设我们有2个类,Class Main&课堂演示

Class Demo {
returnType methodExample(args...) {
//based on the argument provide it returns
return something
}
}
Class Main {
Demo demo = new Demo();
returnType method1() {
value = demo.methodExample(args);
}

returnType method2() {
value = demo.methodExample(args);
}
}

它会共享相同的演示实例吗。以及这两种方法的异步工作方式。还请描述@AutoWired注释在Spring Injecting中的情况

对打字错误或其他错误表示歉意。

是的,它将共享相同的Demo实例。但计算是在方法methodExample((下执行的,该方法基于传递给参数的值。要首先使用@autowire,必须在Demo类上使用@component注释。
@Component
Class Demo {
returnType methodExample(args...) {
//based on the argument provide it returns
return something
}
}
Class Main {
@Autowired
Demo demo;
returnType method1() {
value = demo.methodExample(args);
}
returnType method2() {
value = demo.methodExample(args);
}
}

最新更新