From f539ebe4431138246ebf078178ea163d2c3fd133 Mon Sep 17 00:00:00 2001 From: Sanders Date: Fri, 19 Jan 2024 15:50:53 +0300 Subject: [PATCH] 6 --- .../java/org/example/config/SpringConfig.java | 18 +++++++++++------- src/main/resources/database.properties | 4 ++++ src/main/resources/database.properties.origin | 4 ++++ 3 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 src/main/resources/database.properties create mode 100644 src/main/resources/database.properties.origin diff --git a/src/main/java/org/example/config/SpringConfig.java b/src/main/java/org/example/config/SpringConfig.java index 4fb6a81..7a59b8e 100644 --- a/src/main/java/org/example/config/SpringConfig.java +++ b/src/main/java/org/example/config/SpringConfig.java @@ -5,6 +5,8 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @@ -14,7 +16,7 @@ import org.thymeleaf.spring5.SpringTemplateEngine; import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; import org.thymeleaf.spring5.view.ThymeleafViewResolver; import javax.sql.DataSource; -import java.sql.DriverManager; +import java.util.Objects; /** * @author Neil Alishev @@ -22,13 +24,15 @@ import java.sql.DriverManager; @Configuration @ComponentScan("org.example") @EnableWebMvc +@PropertySource("classpath:database.properties") public class SpringConfig implements WebMvcConfigurer { private final ApplicationContext applicationContext; - + private final Environment environment; @Autowired - public SpringConfig(ApplicationContext applicationContext) { + public SpringConfig(ApplicationContext applicationContext, Environment environment) { this.applicationContext = applicationContext; + this.environment = environment; } @Bean @@ -58,10 +62,10 @@ public class SpringConfig implements WebMvcConfigurer { @Bean public DataSource dataSource(){ DriverManagerDataSource dataSource = new DriverManagerDataSource(); - dataSource.setDriverClassName("org.postgresql.Driver"); - dataSource.setUrl("jdbc:postgresql://192.168.0.10:5432/java"); - dataSource.setUsername("sanders"); - dataSource.setPassword("sanders"); + dataSource.setDriverClassName(Objects.requireNonNull(environment.getProperty("driver"))); + dataSource.setUrl(Objects.requireNonNull(environment.getProperty("url"))); + dataSource.setUsername(Objects.requireNonNull(environment.getProperty("username"))); + dataSource.setPassword(Objects.requireNonNull(environment.getProperty("password"))); return dataSource; } diff --git a/src/main/resources/database.properties b/src/main/resources/database.properties new file mode 100644 index 0000000..4d01ec6 --- /dev/null +++ b/src/main/resources/database.properties @@ -0,0 +1,4 @@ +driver=org.postgresql.Driver +url=jdbc:postgresql://192.168.0.10:5432/java +username=sanders +password=sanders \ No newline at end of file diff --git a/src/main/resources/database.properties.origin b/src/main/resources/database.properties.origin new file mode 100644 index 0000000..abf2e7f --- /dev/null +++ b/src/main/resources/database.properties.origin @@ -0,0 +1,4 @@ +driver= +url= +username= +password= \ No newline at end of file