- Spring 5.0 Projects
- Nilang Patel
- 449字
- 2021-07-02 12:34:56
Defining the JDBC connection properties
We will define the JDBC connection properties in an application.properties file and place it in src/main/resources. The properties we define are as follows:
dataSourceClassName=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/worldgdp
dataSource.user=root
dataSource.password=test
The preceding properties are with the assumptions that MySQL is running on port 3306 and the database username and password are root and test respectively. You can change these properties as per your local configuration. The next step is to define a properties resolver that will be able to resolve the properties when used from within the code. We will use the @PropertySource annotation, along with an instance of PropertySourcesPlaceholderConfigurer, as shown in the following code:
@Configuration
@PropertySource("classpath:application.properties")
public class PropertiesWithJavaConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer
propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
This class reads all the properties from the application.properties file stored in classpath (src/main/resources). Next up is to configure a javax.sql.DataSource object that will connect to the database using the properties defined in the application.properties file. We will use the HikariCP connection pooling library for creating our DataSource instance. This DataSource instance is then used to instantiate NamedParameterJdbcTemplate. We will use NamedParameterJdbcTemplate to execute all our SQL queries. At this point, we need to add a necessary dependency for the HikariCP library as follows:
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>${hikari.version}</version>
</dependency>
The DBConfiguration data source configuration class should look as follows:
@Configuration
public class DBConfiguration {
@Value("${jdbcUrl}") String jdbcUrl;
@Value("${dataSource.user}") String username;
@Value("${dataSource.password}") String password;
@Value("${dataSourceClassName}") String className;
@Bean
public DataSource getDataSource() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl(jdbcUrl);
ds.setUsername(username);
ds.setPassword(password);
ds.setDriverClassName(className);
return ds;
}
@Bean
public NamedParameterJdbcTemplate namedParamJdbcTemplate() {
NamedParameterJdbcTemplate namedParamJdbcTemplate =
new NamedParameterJdbcTemplate(getDataSource());
return namedParamJdbcTemplate;
}
}
Let's have a quick introduction to a few new things used in this code:
- @Configuration: This is to indicate to Spring Framework that this class creates Java objects that contain some configuration
- @Bean: This is method-level annotation, used to indicate to Spring Framework that the method returns Java objects whose life cycle is managed by Spring Framework and injected into places where its dependency is declared
- @Value: This is used to refer to the properties defined in the application.properties, which are resolved by the PropertySourcesPlaceholderConfigurer bean defined in the PropertiesWithJavaConfig class
It is always good practice to write unit test cases in JUnit. We will write test cases for our application. For that, we need to create the corresponding configuration classes for running our JUnit tests. In the next section, we will look at setting up the test environment.
- 6G潛在關鍵技術(下冊)
- Spring Cloud微服務架構進階
- Mastering TypeScript 3
- 互聯(lián)網(wǎng)+思維與創(chuàng)新:通往未來的+號
- 網(wǎng)絡AI+:2030后的未來網(wǎng)絡
- 高級網(wǎng)絡技術
- 局域網(wǎng)組成實踐
- 設備監(jiān)控技術詳解
- Intelligent Mobile Projects with TensorFlow
- 5G智慧交通
- Migrating to Drupal7
- 視聽變革:廣電的新媒體戰(zhàn)略
- 結網(wǎng)@改變世界的互聯(lián)網(wǎng)產(chǎn)品經(jīng)理(修訂版)
- 互聯(lián)網(wǎng)視覺設計(全彩慕課版)
- Twilio Cookbook(Second Edition)