Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.cloudjumper.db.config;
- import org.hibernate.cfg.Configuration;
- import org.json.JSONObject;
- import org.springframework.context.annotation.Scope;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.stereotype.Component;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.stream.Collectors;
- /**
- * Created by cloudjumper on 11/7/16.
- */
- @Component
- @Scope(value = "singleton")
- public class HibernateConfig {
- private Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
- public HibernateConfig() {
- JSONObject jsonConfig;
- String file;
- try {
- // file = new String(Files.readAllBytes(Paths.get("src/main/resources/Config.json")), StandardCharsets.UTF_8);
- // Resource resource = new ClassPathResource("Config.json");
- // BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
- // file = bufferedReader.lines().collect(Collectors.joining("\n"));
- file = new BufferedReader(new InputStreamReader(new ClassPathResource("Config.json").getInputStream()))
- .lines()
- .collect(Collectors.joining("\n"));
- jsonConfig = new JSONObject(file);
- configuration.setProperty("hibernate.connection.url","jdbc:mysql://localhost:3306/void");
- configuration.setProperty("hibernate.connection.username",jsonConfig.getString("db_user"));
- configuration.setProperty("hibernate.connection.password",jsonConfig.getString("db_pass"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public Configuration getConfiguration() {
- return configuration;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement