Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c7808b5

Browse files
Update Spring常见问题总结.md
1 parent a10b604 commit c7808b5

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

‎docs/system-design/framework/spring/Spring常见问题总结.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,14 @@ AOP(Aspect-Oriented Programming:面向切面编程)能够将那些与业务无
111111

112112
### 5.2 Spring 中的单例 bean 的线程安全问题了解吗?
113113

114-
大部分时候我们并没有在系统中使用多线程,所以很少有人会关注这个问题。单例 bean 存在线程问题,主要是因为当多个线程操作同一个对象的时候,对这个对象的非静态成员变量的写操作会存在线程安全问题
114+
的确是存在安全问题的。因为,当多个线程操作同一个对象的时候,对这个对象的成员变量的写操作会存在线程安全问题
115115

116-
常见的有两种解决办法:
116+
但是,一般情况下,我们常用的 `Controller``Service``Dao` 这些 Bean 是无状态的。无状态的 Bean 不能保存数据,因此是线程安全的。
117117

118-
1. 在Bean对象中尽量避免定义可变的成员变量(不太现实)。
118+
常见的有 2 种解决办法:
119119

120-
2. 在类中定义一个ThreadLocal成员变量,将需要的可变成员变量保存在 ThreadLocal 中(推荐的一种方式)。
120+
2. 在类中定义一个 `ThreadLocal` 成员变量,将需要的可变成员变量保存在 `ThreadLocal` 中(推荐的一种方式)。
121+
2. 改变 Bean 的作用域为 "prototype":每次请求都会创建一个新的 bean 实例,自然不会存在线程安全问题。
121122

122123

123124
### 5.3 @Component@Bean 的区别是什么?

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /