How to configure JNDI data source in Spring Boot?
To configure a JNDI datasource in Spring Boot, the following steps are required:
- properties file for an application
spring.datasource.jndi-name=jdbc/myDataSource
The JNDI name jdbc/myDataSource can be modified according to the actual situation.
- – Project Object Model file
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
This will introduce Spring Boot’s support for JPA.
- Activate JPA repositories
- Scan for entities
@SpringBootApplication
@EnableJpaRepositories(basePackages = "com.example.repository")
@EntityScan(basePackages = "com.example.entity")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
com.example.repository is the package path for the repository interface, and com.example.entity is the package path for the entity class, make changes according to the actual situation.
- Ensure that the application is deployed on a server that supports JNDI, and configure a data source in the server’s configuration file. The specific configuration methods may vary depending on the server, so consult the server’s documentation for guidance.
After completing the steps above, the Spring Boot application will use a JNDI data source for accessing the database.