Search code examples
javaspring-bootmemory-leaksnew-operator

`new` keyword in bean initialization in Spring Boot will cause a memory leak?


We don't use new keyword to initialize new bean. But can I initialize new bean with new keyword like this

@Configuration
public class MyConfiguration {

   @Bean(name = "qaDataSource")
   public JdbcTemplate customJdbcTemplate()  {

    DataSource ds = DataSourceBuilder.create()
        .url("jdbc:postgresql://myserver:1111/dbName")
        .username(env.getProperty("spring.secondDatasource.username"))                
        .password(env.getProperty("spring.secondDatasource.password"))
        .driverClassName(env.getProperty("spring.secondDatasource.driverClassName"))
        .build();
    return new JdbcTemplate(ds);
}

Will new in this case cause any memory leaks?


Solution

  • No, there will be no memory leak in this case.