|
1 | 1 | package com.baeldung.autoconfiguration; |
2 | 2 |
|
3 | | -import java.util.Arrays; |
4 | | -import java.util.Properties; |
5 | | - |
6 | | -import javax.persistence.EntityManagerFactory; |
7 | | -import javax.sql.DataSource; |
8 | | - |
9 | 3 | import org.springframework.beans.factory.annotation.Autowired; |
10 | 4 | import org.springframework.boot.autoconfigure.AutoConfigureOrder; |
11 | | -import org.springframework.boot.autoconfigure.condition.ConditionMessage; |
| 5 | +import org.springframework.boot.autoconfigure.condition.*; |
12 | 6 | import org.springframework.boot.autoconfigure.condition.ConditionMessage.Style; |
13 | | -import org.springframework.boot.autoconfigure.condition.ConditionOutcome; |
14 | | -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
15 | | -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
16 | | -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
17 | | -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
18 | | -import org.springframework.boot.autoconfigure.condition.ConditionalOnResource; |
19 | | -import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
20 | | -import org.springframework.boot.autoconfigure.condition.SpringBootCondition; |
21 | | -import org.springframework.context.annotation.Bean; |
22 | | -import org.springframework.context.annotation.ConditionContext; |
23 | | -import org.springframework.context.annotation.Conditional; |
24 | | -import org.springframework.context.annotation.Configuration; |
25 | | -import org.springframework.context.annotation.PropertySource; |
| 7 | +import org.springframework.context.annotation.*; |
26 | 8 | import org.springframework.core.Ordered; |
27 | 9 | import org.springframework.core.env.Environment; |
28 | 10 | import org.springframework.core.type.AnnotatedTypeMetadata; |
|
32 | 14 | import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
33 | 15 | import org.springframework.util.ClassUtils; |
34 | 16 |
|
| 17 | +import javax.persistence.EntityManagerFactory; |
| 18 | +import javax.sql.DataSource; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Properties; |
| 21 | + |
35 | 22 | @Configuration |
36 | 23 | @ConditionalOnClass(DataSource.class) |
37 | 24 | @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) |
38 | 25 | @PropertySource("classpath:mysql.properties") |
39 | 26 | public class MySQLAutoconfiguration { |
| 27 | + |
40 | 28 | @Autowired |
41 | 29 | private Environment env; |
42 | 30 |
|
@@ -74,7 +62,7 @@ public DataSource dataSource2() { |
74 | 62 | public LocalContainerEntityManagerFactoryBean entityManagerFactory() { |
75 | 63 | final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); |
76 | 64 | em.setDataSource(dataSource()); |
77 | | - em.setPackagesToScan(new String[] { "com.baeldung.autoconfiguration.example" }); |
| 65 | + em.setPackagesToScan("com.baeldung.autoconfiguration.example"); |
78 | 66 | em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); |
79 | 67 | if (additionalProperties() != null) { |
80 | 68 | em.setJpaProperties(additionalProperties()); |
@@ -104,17 +92,21 @@ final Properties additionalProperties() { |
104 | 92 |
|
105 | 93 | static class HibernateCondition extends SpringBootCondition { |
106 | 94 |
|
107 | | - private static String[] CLASS_NAMES = { "org.hibernate.ejb.HibernateEntityManager", "org.hibernate.jpa.HibernateEntityManager" }; |
| 95 | + private static final String[] CLASS_NAMES = { |
| 96 | + "org.hibernate.ejb.HibernateEntityManager", |
| 97 | + "org.hibernate.jpa.HibernateEntityManager" }; |
108 | 98 |
|
109 | 99 | @Override |
110 | 100 | public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { |
111 | 101 | ConditionMessage.Builder message = ConditionMessage.forCondition("Hibernate"); |
112 | | - for (String className : CLASS_NAMES) { |
113 | | - if (ClassUtils.isPresent(className, context.getClassLoader())) { |
114 | | - return ConditionOutcome.match(message.found("class").items(Style.NORMAL, className)); |
115 | | - } |
116 | | - } |
117 | | - return ConditionOutcome.noMatch(message.didNotFind("class", "classes").items(Style.NORMAL, Arrays.asList(CLASS_NAMES))); |
| 102 | + |
| 103 | + return Arrays.stream(CLASS_NAMES) |
| 104 | + .filter(className -> ClassUtils.isPresent(className, context.getClassLoader())) |
| 105 | + .map(className -> ConditionOutcome |
| 106 | + .match(message.found("class").items(Style.NORMAL, className))) |
| 107 | + .findAny() |
| 108 | + .orElseGet(() -> ConditionOutcome |
| 109 | + .noMatch(message.didNotFind("class", "classes").items(Style.NORMAL, Arrays.asList(CLASS_NAMES)))); |
118 | 110 | } |
119 | 111 |
|
120 | 112 | } |
|
0 commit comments