Spring Injection - Thread Protection

public class Test{
private First first;
private Second second;

public void setFirst(First first){
this.first = first;
}
public First getFirst(){
 return first;
}
// same approach for second
}

If I insert an instance through spring injection, is it thread safe? If not, how to make it thread safe? I googled and found conflicting answers, unable to come to any conclusion. Please suggest. Thanks in advance. -RW

+3
source share
3 answers

If you are talking about what I think you are talking about, then this is an interesting discussion.

Technically, since setFirst()it is getFirst()not synchronized, then it is possible setFirst()to introduce one object into Thread 1 and getFirst()to return another object to Thread 2. Memory The Java model reserves the right to make this "ultimately consistent," as they say.

, Spring, bean ( ), , HTTP- () , bean, - .

: . HTTP- Spring, Java , .

( , , ). - , .

, .

, , . .

+5

Spring, , Spring , , .

, (.. ) beans , .

, Spring ( , Spring ).

0

, Test / (a) Spring (b) . get/set:

public synchronized void setFirst(First first){
    this.first = first;
}
public synchronized First getFirst(){
    return first;
}

-1

Source: https://habr.com/ru/post/1792202/


All Articles