0

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:-

enter image description here

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 !

Community
  • 1
  • 1
Shinchan
  • 331
  • 1
  • 3
  • 13
  • Why would you need to construct your datasource manually... I really don't see the use for that. Work with the framework instead of around it... – M. Deinum Apr 24 '17 at 12:46
  • Thanks for responding! So what could be the possible solution?Do you mean to say I should externalize my configurations? – Shinchan Apr 24 '17 at 13:10
  • Spring boot already supports that... I really don't see anything in here for which you need anything special. – M. Deinum Apr 24 '17 at 13:12
  • yes! But then how do I remove stuff from application-local.properties ? – Shinchan Apr 24 '17 at 13:49
  • Just put it in `application.properties`... Remove the `@PropertySource` and use Spring Boot features... – M. Deinum Apr 24 '17 at 13:53
  • I am sorry I did not mention that I am not supposed to put my stuff into any of the__.properties file.I would be running this application in different environments.Possible I would be using Docker and stuff. – Shinchan Apr 24 '17 at 13:57
  • Have you actually READ the spring boot reference guide? See the [Externalized Configuration](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config)... One of the things of spring boot is that it makes to do what you want easy and supported out of the box... Regardless where the properties come from. – M. Deinum Apr 24 '17 at 14:00
  • Thanks for reminding me about the documentation.I have READ the documentation.Hence in my first reply to your comment I made a mention of externalizing my configurations.According to the documentation,we can use properties files, YAML files, environment variables and command-line arguments to externalize configuration.I want to use environment variables to achieve the objective. http://stackoverflow.com/questions/35531661/using-env-variable-in-spring-boots-application-properties – Shinchan Apr 24 '17 at 14:09
  • And I restate again that works out-of-the-box... You don't need to change anything... Just add `spring.datasource.url` as an environment variable and it will be picked up. It doesn't matter where the properties come from it can be either of the list mentioned in the documentation. You can even provide defaults in the `application.properties` (or yaml) and override with environment properties. You can mix and match... That was the whole point and as mentioned you don't need anything special for that. – M. Deinum Apr 24 '17 at 17:42
  • Thanks for your guidance! – Shinchan Apr 25 '17 at 04:24

0 Answers0