mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Re-order spring.factories so vanilla JPA users get component scan detection
This commit is contained in:
parent
50f3a2c614
commit
369b0834a0
@ -34,7 +34,7 @@ import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
|
|||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass(JpaRepository.class)
|
@ConditionalOnClass(JpaRepository.class)
|
||||||
@ConditionalOnMissingBean(JpaRepositoryFactoryBean.class)
|
@ConditionalOnMissingBean(JpaRepositoryFactoryBean.class)
|
||||||
@Import({ JpaComponentScanDetector.class, JpaRepositoriesAutoConfigureRegistrar.class })
|
@Import(JpaRepositoriesAutoConfigureRegistrar.class)
|
||||||
public class JpaRepositoriesAutoConfiguration {
|
public class JpaRepositoriesAutoConfiguration {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration for a Commons DBCP database pool. The DBCP pool is popular but not
|
* Configuration for a Commons DBCP database pool. The DBCP pool is popular but not
|
||||||
* recommended in high volume environments.
|
* recommended in high volume environments (the Tomcat DataSource is more reliable).
|
||||||
*
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
*
|
*
|
||||||
|
@ -82,7 +82,7 @@ public class DataSourceAutoConfiguration {
|
|||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@Conditional(DataSourceAutoConfiguration.SomeDatabaseCondition.class)
|
@Conditional(DataSourceAutoConfiguration.SomeDatabaseCondition.class)
|
||||||
// FIXME: make this @ConditionalOnBean(DataSorce.class)
|
// FIXME: make this @ConditionalOnBean(DataSource.class)
|
||||||
protected static class JdbcTemplateConfiguration {
|
protected static class JdbcTemplateConfiguration {
|
||||||
|
|
||||||
@Autowired(required = false)
|
@Autowired(required = false)
|
||||||
|
@ -20,16 +20,19 @@ import java.util.LinkedHashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.hibernate.ejb.HibernateEntityManager;
|
import org.hibernate.ejb.HibernateEntityManager;
|
||||||
import org.springframework.bootstrap.autoconfigure.jdbc.EmbeddedDatabaseConfiguration;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.bootstrap.context.annotation.ConditionalOnClass;
|
import org.springframework.bootstrap.context.annotation.ConditionalOnClass;
|
||||||
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||||
import org.springframework.orm.jpa.JpaVendorAdapter;
|
import org.springframework.orm.jpa.JpaVendorAdapter;
|
||||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
|
import org.springframework.orm.jpa.vendor.Database;
|
||||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link EnableAutoConfiguration Auto-configuration} for Hibernate JPA.
|
* {@link EnableAutoConfiguration Auto-configuration} for Hibernate JPA.
|
||||||
@ -39,6 +42,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass(HibernateEntityManager.class)
|
@ConditionalOnClass(HibernateEntityManager.class)
|
||||||
@EnableTransactionManagement
|
@EnableTransactionManagement
|
||||||
|
@Import(JpaComponentScanDetector.class)
|
||||||
public class HibernateJpaAutoConfiguration extends JpaAutoConfiguration {
|
public class HibernateJpaAutoConfiguration extends JpaAutoConfiguration {
|
||||||
|
|
||||||
private static final Map<EmbeddedDatabaseType, String> EMBEDDED_DATABASE_DIALECTS;
|
private static final Map<EmbeddedDatabaseType, String> EMBEDDED_DATABASE_DIALECTS;
|
||||||
@ -48,24 +52,36 @@ public class HibernateJpaAutoConfiguration extends JpaAutoConfiguration {
|
|||||||
"org.hibernate.dialect.HSQLDialect");
|
"org.hibernate.dialect.HSQLDialect");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Value("${spring.jpa.databasePlatform:${spring.jpa.database_platform:}}")
|
||||||
|
private String databasePlatform;
|
||||||
|
|
||||||
|
@Value("${spring.jpa.database:DEFAULT}")
|
||||||
|
private Database database = Database.DEFAULT;
|
||||||
|
|
||||||
|
@Value("${spring.jpa.showSql:${spring.jpa.show_sql:false}}")
|
||||||
|
private boolean showSql;
|
||||||
|
|
||||||
|
@Value("${spring.jpa.generateDdl:${spring.jpa.generate_ddl:false}}")
|
||||||
|
private boolean generateDdl;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Override
|
@Override
|
||||||
public JpaVendorAdapter jpaVendorAdapter() {
|
public JpaVendorAdapter jpaVendorAdapter() {
|
||||||
return new HibernateJpaVendorAdapter();
|
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
|
||||||
|
adapter.setShowSql(this.showSql);
|
||||||
|
adapter.setGenerateDdl(this.generateDdl);
|
||||||
|
if (StringUtils.hasText(this.databasePlatform)) {
|
||||||
|
adapter.setDatabasePlatform(this.databasePlatform);
|
||||||
|
}
|
||||||
|
adapter.setDatabase(this.database);
|
||||||
|
return adapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(
|
protected void configure(
|
||||||
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean) {
|
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean) {
|
||||||
Map<String, Object> properties = entityManagerFactoryBean.getJpaPropertyMap();
|
Map<String, Object> properties = entityManagerFactoryBean.getJpaPropertyMap();
|
||||||
if (isAutoConfiguredDataSource()) {
|
// FIXME: detect EhCache
|
||||||
properties.put("hibernate.hbm2ddl.auto", "create-drop");
|
|
||||||
String dialect = EMBEDDED_DATABASE_DIALECTS
|
|
||||||
.get(EmbeddedDatabaseConfiguration.getEmbeddedDatabaseType());
|
|
||||||
if (dialect != null) {
|
|
||||||
properties.put("hibernate.dialect", dialect);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
properties.put("hibernate.cache.provider_class",
|
properties.put("hibernate.cache.provider_class",
|
||||||
"org.hibernate.cache.HashtableCacheProvider");
|
"org.hibernate.cache.HashtableCacheProvider");
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.springframework.bootstrap.autoconfigure.data;
|
package org.springframework.bootstrap.autoconfigure.orm.jpa;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -30,6 +30,7 @@ import org.springframework.beans.factory.config.BeanDefinition;
|
|||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||||
|
import org.springframework.bootstrap.autoconfigure.data.JpaRepositoriesAutoConfiguration;
|
||||||
import org.springframework.bootstrap.context.annotation.AutoConfigurationUtils;
|
import org.springframework.bootstrap.context.annotation.AutoConfigurationUtils;
|
||||||
import org.springframework.cglib.proxy.Enhancer;
|
import org.springframework.cglib.proxy.Enhancer;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
@ -1,10 +1,10 @@
|
|||||||
org.springframework.bootstrap.context.annotation.EnableAutoConfiguration=\
|
org.springframework.bootstrap.context.annotation.EnableAutoConfiguration=\
|
||||||
org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration,\
|
org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration,\
|
||||||
org.springframework.bootstrap.autoconfigure.MessageSourceAutoConfiguration,\
|
org.springframework.bootstrap.autoconfigure.MessageSourceAutoConfiguration,\
|
||||||
org.springframework.bootstrap.autoconfigure.data.JpaRepositoriesAutoConfiguration,\
|
|
||||||
org.springframework.bootstrap.autoconfigure.jdbc.DataSourceAutoConfiguration,\
|
org.springframework.bootstrap.autoconfigure.jdbc.DataSourceAutoConfiguration,\
|
||||||
org.springframework.bootstrap.autoconfigure.batch.BatchAutoConfiguration,\
|
|
||||||
org.springframework.bootstrap.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
|
org.springframework.bootstrap.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
|
||||||
|
org.springframework.bootstrap.autoconfigure.data.JpaRepositoriesAutoConfiguration,\
|
||||||
|
org.springframework.bootstrap.autoconfigure.batch.BatchAutoConfiguration,\
|
||||||
org.springframework.bootstrap.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
|
org.springframework.bootstrap.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
|
||||||
org.springframework.bootstrap.autoconfigure.web.EmbeddedContainerCustomizerConfiguration,\
|
org.springframework.bootstrap.autoconfigure.web.EmbeddedContainerCustomizerConfiguration,\
|
||||||
org.springframework.bootstrap.autoconfigure.web.EmbeddedJettyAutoConfiguration,\
|
org.springframework.bootstrap.autoconfigure.web.EmbeddedJettyAutoConfiguration,\
|
||||||
|
@ -18,6 +18,8 @@ package org.springframework.bootstrap.autoconfigure.data;
|
|||||||
import javax.persistence.EntityManagerFactory;
|
import javax.persistence.EntityManagerFactory;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||||
|
import org.springframework.bootstrap.autoconfigure.data.test.City;
|
||||||
import org.springframework.bootstrap.autoconfigure.data.test.CityRepository;
|
import org.springframework.bootstrap.autoconfigure.data.test.CityRepository;
|
||||||
import org.springframework.bootstrap.autoconfigure.jdbc.EmbeddedDatabaseConfiguration;
|
import org.springframework.bootstrap.autoconfigure.jdbc.EmbeddedDatabaseConfiguration;
|
||||||
import org.springframework.bootstrap.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
import org.springframework.bootstrap.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
||||||
@ -40,9 +42,9 @@ public class JpaRepositoriesAutoConfigurationTests {
|
|||||||
public void testDefaultRepositoryConfiguration() throws Exception {
|
public void testDefaultRepositoryConfiguration() throws Exception {
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
this.context = new AnnotationConfigApplicationContext();
|
||||||
this.context.register(TestConfiguration.class,
|
this.context.register(TestConfiguration.class,
|
||||||
EmbeddedDatabaseConfiguration.class,
|
EmbeddedDatabaseConfiguration.class, HibernateJpaAutoConfiguration.class,
|
||||||
JpaRepositoriesAutoConfiguration.class,
|
JpaRepositoriesAutoConfiguration.class,
|
||||||
HibernateJpaAutoConfiguration.class);
|
PropertyPlaceholderAutoConfiguration.class);
|
||||||
this.context.refresh();
|
this.context.refresh();
|
||||||
assertNotNull(this.context.getBean(CityRepository.class));
|
assertNotNull(this.context.getBean(CityRepository.class));
|
||||||
assertNotNull(this.context.getBean(PlatformTransactionManager.class));
|
assertNotNull(this.context.getBean(PlatformTransactionManager.class));
|
||||||
@ -50,7 +52,7 @@ public class JpaRepositoriesAutoConfigurationTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan("org.springframework.bootstrap.autoconfigure.data.test")
|
@ComponentScan(basePackageClasses = City.class)
|
||||||
protected static class TestConfiguration {
|
protected static class TestConfiguration {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user