File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Spring_part_3/src/main/java/spring/oldboy/pool Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments