I have a sample spring boot application.I am using PostgreSQL DB.I am new to the world of spring,spring boot etc
The sample application performs CRUD operations.I am using Java8 and intelliJ IDEA.
My application.properties contains the following:-
spring.profiles.active=local
While I have the following stuff in my application-local.properties :-
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres123
spring.datasource.initial-size=5
spring.datasource.max-active=10
spring.datasource.max-idle=5
spring.datasource.min-idle=2
As it is evident that my application.properties also has some connection pooling stuff.
The structure of my project happens to be as follows:-
As it might be evident from the project structure that I have a model folder which of course contains the class for employee table. I have got a DAO that has a class consisting of methods to work with employee related data. I have a service layer that consists of EmployeeSerive class.It has
@Autowired
EmployeeDao employeeDao;
Hence the DAO methods are implemented in service.
I need to move whatever I have in my application.properties
I think the best way to do this would be to configure datasource programatically:-
My Application.java class looks something like this:-
@SpringBootApplication
@PropertySource("application-${spring.profiles.active}.properties")
public class Application implements CommandLineRunner {
@Autowired
DataSource dataSource;
private static final Logger logger =
LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
logger.info("-------Message at INFO level from Application.main()-------
");
}
@Override
public void run(String... args) throws Exception {
logger.info("-------Message at INFO level from Application.run()-------
");
logger.info(String.valueOf(dataSource));
}
}
In order to configure a bean should I introduce something like below in Application.java class:-
@Bean
public DataSource dataSource() {
return DataSourceBuilder
.create()
.username("")
.password("")
.url("")
.driverClassName("")
.build();
}
In the above code snippet I would pass whatever is available in application.properties file.
I am following the below link:-
Configure DataSource programmatically in Spring Boot
Do I need to introduce additional classes in my application possibly in DAO to use the above.
Or just introducing the above is sufficient. What should I do for connection pooling stuff?
Thanks in advance !