Skip to content

Commit cb4ea4e

Browse files
committed
Add a static method to get the yaml file inside web root config
1 parent 72b006d commit cb4ea4e

File tree

7 files changed

+69
-5
lines changed

7 files changed

+69
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# Note System `Spring-Boot-JPA`
1+
# Back End For Note System `Spring-Boot-JPA`

note-system-back-end.iml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
<configuration>
2020
<setting name="validation-enabled" value="true" />
2121
<setting name="provider-name" value="Hibernate" />
22-
<datasource-mapping />
22+
<datasource-mapping>
23+
<factory-entry name="entityManagerFactory" />
24+
<factory-entry name="note-system-back-end" />
25+
</datasource-mapping>
2326
<naming-strategy-map />
2427
</configuration>
2528
</facet>
@@ -79,5 +82,8 @@
7982
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.8.2" level="project" />
8083
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.29" level="project" />
8184
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:3.19.4" level="project" />
85+
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:4.0.3" level="project" />
86+
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.30" level="project" />
87+
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.30" level="project" />
8288
</component>
8389
</module>

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676
<artifactId>HikariCP</artifactId>
7777
<version>4.0.3</version>
7878
</dependency>
79+
<!-- https://mvnrepository.com/artifact/com.github.bmoliveira/snake-yaml -->
80+
<!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml -->
81+
82+
7983
</dependencies>
8084

8185
<build>

src/main/java/lk/ijse/dep/note/config/JpaConfig.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
package lk.ijse.dep.note.config;
22

3+
import com.zaxxer.hikari.HikariDataSource;
34
import org.springframework.beans.factory.annotation.Autowired;
45
import org.springframework.context.annotation.Bean;
56
import org.springframework.context.annotation.Configuration;
67
import org.springframework.context.annotation.PropertySource;
78
import org.springframework.core.env.Environment;
9+
import org.springframework.orm.jpa.JpaTransactionManager;
810
import org.springframework.orm.jpa.JpaVendorAdapter;
911
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
1012
import org.springframework.orm.jpa.vendor.Database;
1113
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
14+
import org.springframework.transaction.PlatformTransactionManager;
15+
import org.springframework.transaction.annotation.EnableTransactionManagement;
1216

17+
import javax.persistence.EntityManagerFactory;
1318
import javax.sql.DataSource;
1419
import javax.xml.crypto.Data;
1520

1621
@Configuration
1722
@PropertySource("classpath:application.yml")
23+
@EnableTransactionManagement
1824
public class JpaConfig {
1925
@Autowired
2026
private Environment env;
@@ -35,4 +41,19 @@ public JpaVendorAdapter jpaVendorAdapter(){
3541
jpaVendorAdapter.setDatabase(Database.MYSQL);
3642
return jpaVendorAdapter;
3743
}
44+
45+
@Bean
46+
public DataSource dataSource(){
47+
final HikariDataSource hikariDataSource = new HikariDataSource();
48+
hikariDataSource.setJdbcUrl(env.getRequiredProperty("hikari.jdbc-url"));
49+
hikariDataSource.setUsername(env.getRequiredProperty("hikari.username"));
50+
hikariDataSource.setPassword(env.getRequiredProperty("hikari.password"));
51+
hikariDataSource.setDriverClassName(env.getRequiredProperty("hikari.driver-classname"));
52+
hikariDataSource.setMaximumPoolSize(env.getRequiredProperty("hikari.max-pool-size",Integer.class));
53+
return hikariDataSource;
54+
}
55+
56+
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
57+
return new JpaTransactionManager(emf);
58+
}
3859
}

src/main/java/lk/ijse/dep/note/config/WebAppConfig.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,29 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import lk.ijse.dep.note.WebAppInitializer;
5+
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
56
import org.springframework.context.annotation.Bean;
67
import org.springframework.context.annotation.ComponentScan;
78
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
9+
import org.springframework.core.env.ConfigurableEnvironment;
10+
import org.springframework.core.env.PropertiesPropertySource;
11+
import org.springframework.core.io.ClassPathResource;
812
import org.springframework.stereotype.Component;
913
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
1014

15+
import java.util.Properties;
16+
1117
@Component
1218
@EnableWebMvc
1319
@ComponentScan(basePackageClasses = WebAppInitializer.class)
1420
public class WebAppConfig {
1521

22+
23+
1624
@Bean
1725
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
18-
return new PropertySourcesPlaceholderConfigurer();
26+
return new PropertySourcesPlaceholderConfigurer();
27+
1928
}
2029
@Bean
2130
public ObjectMapper objectMapper(){
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
package lk.ijse.dep.note.config;
22

3+
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
4+
import org.springframework.context.annotation.Bean;
35
import org.springframework.context.annotation.Configuration;
46
import org.springframework.context.annotation.Import;
7+
import org.springframework.core.env.ConfigurableEnvironment;
8+
import org.springframework.core.env.PropertiesPropertySource;
9+
import org.springframework.core.io.ClassPathResource;
10+
11+
import java.util.Properties;
512

613
@Configuration
714
@Import(JpaConfig.class)
815
public class WebRootConfig {
16+
@Bean
17+
public static YamlPropertiesFactoryBean yamlPropertiesFactoryBean(ConfigurableEnvironment env ){
18+
YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
19+
yamlPropertiesFactoryBean.setResources( new ClassPathResource("application.yml"));
20+
Properties yamlProps = yamlPropertiesFactoryBean.getObject();
21+
env.getPropertySources().addLast(new PropertiesPropertySource("yaml",yamlProps));
22+
return yamlPropertiesFactoryBean;
23+
24+
25+
}
926
}

src/main/resources/application.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
jpa
1+
jpa:
22
show-sql:true
33
generate-ddl:true
4-
dialect:org.hibernate.dialect.MySQL8Dialect
4+
dialect:org.hibernate.dialect.MySQL8Dialect
5+
6+
hikari:
7+
jdbc-url:jdbc:mysql//localhost:3306/dep8_notes?createDatabaseIfNotExist=true
8+
username:root
9+
password:12345678
10+
driver-classname:com.mysql.cj.jdbc.Driver
11+
max-pool-size:10

0 commit comments

Comments
 (0)