Skip to content

Commit 9d5b5d5

Browse files
committed
Add StarterConnectionPool.java
1 parent f52f7ee commit 9d5b5d5

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package spring.oldboy.pool;
2+
/* Примерный вид bean-a из реального приложения - неизменяем, много аннотаций */
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.stereotype.Component;
6+
7+
import javax.annotation.PostConstruct;
8+
import javax.annotation.PreDestroy;
9+
10+
/*
11+
Мы можем сами именовать наш будущий bean, а можем оставить без названия,
12+
тогда название будет задано автоматически 'starterConnectionPool' -
13+
по названию класса с маленькой буквы. Все наши bean-ы имутабильны, поля
14+
final, сеттеров нет.
15+
*/
16+
@Component("pool1")
17+
public class StarterConnectionPool {
18+
private final String username;
19+
private final Integer poolSize;
20+
21+
/* Аннотацию можно и не ставить, т.к. будет вызван доступный конструктор */
22+
@Autowired
23+
/* Используя аннотацию @Value и SPEL задаем значения из resources/application.properties */
24+
public StarterConnectionPool(@Value("${db.username}") String username,
25+
@Value("${db.pool.size}") Integer poolSize) {
26+
this.username = username;
27+
this.poolSize = poolSize;
28+
}
29+
30+
@PostConstruct
31+
private void init() {
32+
System.out.println("Init connection pool");
33+
}
34+
35+
@PreDestroy
36+
private void destroy() {
37+
System.out.println("Clean connection pool");
38+
}
39+
}

0 commit comments

Comments
 (0)