From bb944171a6f0656fa72b1f13a8d919de601bbf02 Mon Sep 17 00:00:00 2001 From: amit2103 Date: Sat, 2 Nov 2019 16:05:41 +0530 Subject: [PATCH 1/2] [BAEL-17317] - Check article code - https://www.baeldung.com/spring-boot-annotations --- spring-boot-mvc/pom.xml | 14 +++ .../MySQLAutoconfiguration.java | 118 ++++++++++++++++++ .../src/main/resources/mysql.properties | 5 + .../org/baeldung/SpringContextLiveTest.java | 19 +++ 4 files changed, 156 insertions(+) create mode 100644 spring-boot-mvc/src/main/java/com/baeldung/autoconfiguration/MySQLAutoconfiguration.java create mode 100644 spring-boot-mvc/src/main/resources/mysql.properties create mode 100644 spring-boot-mvc/src/test/java/org/baeldung/SpringContextLiveTest.java diff --git a/spring-boot-mvc/pom.xml b/spring-boot-mvc/pom.xml index a414ed7bb2..c03a8e04ac 100644 --- a/spring-boot-mvc/pom.xml +++ b/spring-boot-mvc/pom.xml @@ -34,7 +34,21 @@ org.springframework.boot spring-boot-starter-thymeleaf + + org.springframework.boot + spring-boot-starter-data-jpa + + + mysql + mysql-connector-java + + + + org.hsqldb + hsqldb + runtime + diff --git a/spring-boot-mvc/src/main/java/com/baeldung/autoconfiguration/MySQLAutoconfiguration.java b/spring-boot-mvc/src/main/java/com/baeldung/autoconfiguration/MySQLAutoconfiguration.java new file mode 100644 index 0000000000..295e0d74c9 --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/autoconfiguration/MySQLAutoconfiguration.java @@ -0,0 +1,118 @@ +package com.baeldung.autoconfiguration; + +import java.util.Arrays; +import java.util.Properties; + +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.AutoConfigureOrder; +import org.springframework.boot.autoconfigure.condition.ConditionMessage; +import org.springframework.boot.autoconfigure.condition.ConditionMessage.Style; +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.condition.ConditionalOnResource; +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.Ordered; +import org.springframework.core.env.Environment; +import org.springframework.core.type.AnnotatedTypeMetadata; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.util.ClassUtils; + +@Configuration +@ConditionalOnClass(DataSource.class) +@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) +@PropertySource("classpath:mysql.properties") +public class MySQLAutoconfiguration { + + @Autowired + private Environment env; + + @Bean + @ConditionalOnProperty(name = "usemysql", havingValue = "local") + @ConditionalOnMissingBean + public DataSource dataSource() { + final DriverManagerDataSource dataSource = new DriverManagerDataSource(); + + dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); + dataSource.setUrl("jdbc:mysql://localhost:3306/myDb?createDatabaseIfNotExist=true&&serverTimezone=UTC"); + dataSource.setUsername("mysqluser"); + dataSource.setPassword("mysqlpass"); + + return dataSource; + } + + @Bean(name = "dataSource") + @ConditionalOnProperty(name = "usemysql", havingValue = "custom") + @ConditionalOnMissingBean + public DataSource dataSource2() { + final DriverManagerDataSource dataSource = new DriverManagerDataSource(); + + dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); + dataSource.setUrl(env.getProperty("mysql.url")); + dataSource.setUsername(env.getProperty("mysql.user") != null ? env.getProperty("mysql.user") : ""); + dataSource.setPassword(env.getProperty("mysql.pass") != null ? env.getProperty("mysql.pass") : ""); + + return dataSource; + } + + @Bean + @ConditionalOnBean(name = "dataSource") + @ConditionalOnMissingBean + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { + final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); + em.setDataSource(dataSource()); + em.setPackagesToScan("com.baeldung.autoconfiguration.example"); + em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); + if (additionalProperties() != null) { + em.setJpaProperties(additionalProperties()); + } + return em; + } + + @Bean + @ConditionalOnMissingBean(type = "JpaTransactionManager") + JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) { + final JpaTransactionManager transactionManager = new JpaTransactionManager(); + transactionManager.setEntityManagerFactory(entityManagerFactory); + return transactionManager; + } + + @ConditionalOnResource(resources = "classpath:mysql.properties") + @Conditional(HibernateCondition.class) + final Properties additionalProperties() { + final Properties hibernateProperties = new Properties(); + + hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("mysql-hibernate.hbm2ddl.auto")); + hibernateProperties.setProperty("hibernate.dialect", env.getProperty("mysql-hibernate.dialect")); + hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("mysql-hibernate.show_sql") != null ? env.getProperty("mysql-hibernate.show_sql") : "false"); + + return hibernateProperties; + } + + static class HibernateCondition extends SpringBootCondition { + + private static final String[] CLASS_NAMES = { "org.hibernate.ejb.HibernateEntityManager", "org.hibernate.jpa.HibernateEntityManager" }; + + @Override + public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { + ConditionMessage.Builder message = ConditionMessage.forCondition("Hibernate"); + + return Arrays.stream(CLASS_NAMES).filter(className -> ClassUtils.isPresent(className, context.getClassLoader())).map(className -> ConditionOutcome.match(message.found("class").items(Style.NORMAL, className))).findAny() + .orElseGet(() -> ConditionOutcome.noMatch(message.didNotFind("class", "classes").items(Style.NORMAL, Arrays.asList(CLASS_NAMES)))); + } + + } +} diff --git a/spring-boot-mvc/src/main/resources/mysql.properties b/spring-boot-mvc/src/main/resources/mysql.properties new file mode 100644 index 0000000000..74f1ee1373 --- /dev/null +++ b/spring-boot-mvc/src/main/resources/mysql.properties @@ -0,0 +1,5 @@ +usemysql=local + +mysql-hibernate.dialect=org.hibernate.dialect.MySQL5Dialect +mysql-hibernate.show_sql=true +mysql-hibernate.hbm2ddl.auto=create-drop \ No newline at end of file diff --git a/spring-boot-mvc/src/test/java/org/baeldung/SpringContextLiveTest.java b/spring-boot-mvc/src/test/java/org/baeldung/SpringContextLiveTest.java new file mode 100644 index 0000000000..d3ced53483 --- /dev/null +++ b/spring-boot-mvc/src/test/java/org/baeldung/SpringContextLiveTest.java @@ -0,0 +1,19 @@ +package org.baeldung; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +import com.baeldung.autoconfiguration.MySQLAutoconfiguration; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = MySQLAutoconfiguration.class) +@WebAppConfiguration +public class SpringContextLiveTest { + + @Test + public void whenSpringContextIsBootstrapped_thenNoExceptions() { + } +} \ No newline at end of file From 123a5cfd55e11ba1679cb46b6fc554f64c2cbf27 Mon Sep 17 00:00:00 2001 From: "amit.pandey" Date: Fri, 27 Dec 2019 12:48:31 +0530 Subject: [PATCH 2/2] renamed the package name --- spring-boot-mvc/pom.xml | 6 ------ .../MySQLAutoconfiguration.java | 2 +- .../src/test/java/org/baeldung/SpringContextLiveTest.java | 2 +- 3 files changed, 2 insertions(+), 8 deletions(-) rename spring-boot-mvc/src/main/java/com/baeldung/{autoconfiguration => springbootannotations}/MySQLAutoconfiguration.java (99%) diff --git a/spring-boot-mvc/pom.xml b/spring-boot-mvc/pom.xml index c03a8e04ac..77b1c09605 100644 --- a/spring-boot-mvc/pom.xml +++ b/spring-boot-mvc/pom.xml @@ -97,12 +97,6 @@ ${spring.fox.version} - - org.apache.tomcat.embed - tomcat-embed-jasper - provided - - org.aspectj diff --git a/spring-boot-mvc/src/main/java/com/baeldung/autoconfiguration/MySQLAutoconfiguration.java b/spring-boot-mvc/src/main/java/com/baeldung/springbootannotations/MySQLAutoconfiguration.java similarity index 99% rename from spring-boot-mvc/src/main/java/com/baeldung/autoconfiguration/MySQLAutoconfiguration.java rename to spring-boot-mvc/src/main/java/com/baeldung/springbootannotations/MySQLAutoconfiguration.java index 295e0d74c9..62b1329a54 100644 --- a/spring-boot-mvc/src/main/java/com/baeldung/autoconfiguration/MySQLAutoconfiguration.java +++ b/spring-boot-mvc/src/main/java/com/baeldung/springbootannotations/MySQLAutoconfiguration.java @@ -1,4 +1,4 @@ -package com.baeldung.autoconfiguration; +package com.baeldung.springbootannotations; import java.util.Arrays; import java.util.Properties; diff --git a/spring-boot-mvc/src/test/java/org/baeldung/SpringContextLiveTest.java b/spring-boot-mvc/src/test/java/org/baeldung/SpringContextLiveTest.java index d3ced53483..069dd41b8d 100644 --- a/spring-boot-mvc/src/test/java/org/baeldung/SpringContextLiveTest.java +++ b/spring-boot-mvc/src/test/java/org/baeldung/SpringContextLiveTest.java @@ -6,7 +6,7 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; -import com.baeldung.autoconfiguration.MySQLAutoconfiguration; +import com.baeldung.springbootannotations.MySQLAutoconfiguration; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = MySQLAutoconfiguration.class)