7.5. Dependency Injection

Dependency injection is a process whereby objects define their dependencies, that is, the other objects they work with. It is done only through constructor arguments, arguments to a factory method, or properties set or returned using a factory method. The container then injects those dependencies when it creates the bean. You can find more details about dependency injection at https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans.

7.5.1. Configuring IoC Containers for Dependency Injection

In Spring, dependency injection (DI) is carried out through the Spring IoC (Inversion of Control) container.

As before, we will avoid xml configuration and base our approach on annotations and Java configuration.

The main attributes and parts of the Java configuration of an IoC container are classes with the @Configuration annotation and methods with the @Bean annotation.

The @Bean Annotation

The @Bean annotation is used to define a method’s activity in creating, configuring and initializing a new object controlled by the Spring IoC container. Methods so defined can be used the same way as classes with the @Configuration annotation.

Our IoC container will return

  • the connection pool

  • the transaction manager

  • the exception translator that translates SQLException exceptions into Spring-specific DataAccessException exceptions

  • the DSL context that is the starting point for building all queries using the Fluent API

  • managers for implementing the business logic

  • grids for displaying data

  1. /**
  2. * IoC container configuration
  3. * to implement dependency injection.
  4. */
  5. package ru.ibase.fbjavaex.config;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import javax.sql.DataSource;
  9. import org.apache.commons.dbcp.BasicDataSource;
  10. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  11. import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
  12. import org.jooq.impl.DataSourceConnectionProvider;
  13. import org.jooq.DSLContext;
  14. import org.jooq.impl.DefaultDSLContext;
  15. import org.jooq.impl.DefaultConfiguration;
  16. import org.jooq.SQLDialect;
  17. import org.jooq.impl.DefaultExecuteListenerProvider;
  18. import ru.ibase.fbjavaex.exception.ExceptionTranslator;
  19. import ru.ibase.fbjavaex.managers.*;
  20. import ru.ibase.fbjavaex.jqgrid.*;
  21. /**
  22. * The Spring IoC configuration class of the container
  23. */
  24. @Configuration
  25. public class JooqConfig {
  26. /**
  27. * Return connection pool
  28. *
  29. * @return
  30. */
  31. @Bean(name = "dataSource")
  32. public DataSource getDataSource() {
  33. BasicDataSource dataSource = new BasicDataSource();
  34. // ?????????? ???????????? ???????????
  35. dataSource.setUrl("jdbc:firebirdsql://localhost:3050/examples");
  36. dataSource.setDriverClassName("org.firebirdsql.jdbc.FBDriver");
  37. dataSource.setUsername("SYSDBA");
  38. dataSource.setPassword("masterkey");
  39. dataSource.setConnectionProperties("charSet=utf-8");
  40. return dataSource;
  41. }
  42. /**
  43. * Return transaction manager
  44. *
  45. * @return
  46. */
  47. @Bean(name = "transactionManager")
  48. public DataSourceTransactionManager getTransactionManager() {
  49. return new DataSourceTransactionManager(getDataSource());
  50. }
  51. @Bean(name = "transactionAwareDataSource")
  52. public TransactionAwareDataSourceProxy getTransactionAwareDataSource() {
  53. return new TransactionAwareDataSourceProxy(getDataSource());
  54. }
  55. /**
  56. * Return connection provider
  57. *
  58. * @return
  59. */
  60. @Bean(name = "connectionProvider")
  61. public DataSourceConnectionProvider getConnectionProvider() {
  62. return new DataSourceConnectionProvider(getTransactionAwareDataSource());
  63. }
  64. /**
  65. * Return exception translator
  66. *
  67. * @return
  68. */
  69. @Bean(name = "exceptionTranslator")
  70. public ExceptionTranslator getExceptionTranslator() {
  71. return new ExceptionTranslator();
  72. }
  73. /**
  74. * Returns the DSL context configuration
  75. *
  76. * @return
  77. */
  78. @Bean(name = "dslConfig")
  79. public org.jooq.Configuration getDslConfig() {
  80. DefaultConfiguration config = new DefaultConfiguration();
  81. // ?????????? ??????? SQL ???? Firebird
  82. config.setSQLDialect(SQLDialect.FIREBIRD);
  83. config.setConnectionProvider(getConnectionProvider());
  84. DefaultExecuteListenerProvider listenerProvider =
  85. new DefaultExecuteListenerProvider(getExceptionTranslator());
  86. config.setExecuteListenerProvider(listenerProvider);
  87. return config;
  88. }
  89. /**
  90. * Return DSL context
  91. *
  92. * @return
  93. */
  94. @Bean(name = "dsl")
  95. public DSLContext getDsl() {
  96. org.jooq.Configuration config = this.getDslConfig();
  97. return new DefaultDSLContext(config);
  98. }
  99. /**
  100. * Return customer manager
  101. *
  102. * @return
  103. */
  104. @Bean(name = "customerManager")
  105. public CustomerManager getCustomerManager() {
  106. return new CustomerManager();
  107. }
  108. /**
  109. * Return customer grid
  110. *
  111. * @return
  112. */
  113. @Bean(name = "customerGrid")
  114. public JqGridCustomer getCustomerGrid() {
  115. return new JqGridCustomer();
  116. }
  117. /**
  118. * Return product manager
  119. *
  120. * @return
  121. */
  122. @Bean(name = "productManager")
  123. public ProductManager getProductManager() {
  124. return new ProductManager();
  125. }
  126. /**
  127. * Return product grid
  128. *
  129. * @return
  130. */
  131. @Bean(name = "productGrid")
  132. public JqGridProduct getProductGrid() {
  133. return new JqGridProduct();
  134. }
  135. /**
  136. * Return invoice manager
  137. *
  138. * @return
  139. */
  140. @Bean(name = "invoiceManager")
  141. public InvoiceManager getInvoiceManager() {
  142. return new InvoiceManager();
  143. }
  144. /**
  145. * Return invoice grid
  146. *
  147. * @return
  148. */
  149. @Bean(name = "invoiceGrid")
  150. public JqGridInvoice getInvoiceGrid() {
  151. return new JqGridInvoice();
  152. }
  153. /**
  154. * Return invoice items grid
  155. *
  156. * @return
  157. */
  158. @Bean(name = "invoiceLineGrid")
  159. public JqGridInvoiceLine getInvoiceLineGrid() {
  160. return new JqGridInvoiceLine();
  161. }
  162. /**
  163. * Return working period
  164. *
  165. * @return
  166. */
  167. @Bean(name = "workingPeriod")
  168. public WorkingPeriod getWorkingPeriod() {
  169. return new WorkingPeriod();
  170. }
  171. }