Spring Boot 配置 : JpaBaseConfiguration

概述

JpaBaseConfigurationSpring Boot提供的对JPA进行配置的抽象基类。针对不同的JPA实现,会有不同的具体实现类。比如Spring Boot内置支持基于HibernateJPA,所以它提供了相应的实现HibernateJpaConfiguration。而如果开发人员想使用其他JPA实现,比如EclipseLink或者OpenJPA,就要继承JpaBaseConfiguration提供相应的具体实现。

虽然针对不同的JPA提供商实现需要有不同的JpaBaseConfiguration实现子类,但这些子类主要是将JPA提供商实现信息包装成统一的格式供JpaBaseConfiguration使用,而Spring Data JPA主要工作组件的配置任务,还是主要落在JpaBaseConfiguration身上。

JpaBaseConfiguration主要是确保以下bean的定义 :

  • PlatformTransactionManager transactionManager

    仅在该类型的bean未被定义的情况下才定义

  • LocalContainerEntityManagerFactoryBean entityManagerFactory

    仅在该类型的bean或者bean EntityManagerFactory未被定义的情况下才定义

  • JpaVendorAdapter jpaVendorAdapter

    仅在该类型的bean未被定义的情况下才定义;
    bean LocalContainerEntityManagerFactoryBean entityManagerFactory定义的基础。

  • EntityManagerFactoryBuilder entityManagerFactoryBuilder

    仅在该类型的bean未被定义的情况下才定义;
    bean LocalContainerEntityManagerFactoryBean entityManagerFactory定义的基础。

可以这么理解,一个JpaBaseConfiguration(当然具体类是它的某个实现子类)应用之后,我们就可以注入bean EntityManagerFactory了。

源代码

源代码版本 : spring-boot-autoconfigure-2.1.3.RELEASE

package org.springframework.boot.autoconfigure.orm.jpa;

// 省略 imports


@Configuration
// 确保前缀为 spring.jpa 的属性被加载到 bean JpaProperties
@EnableConfigurationProperties(JpaProperties.class)
// 导入 DataSourceInitializedPublisher.Registrar, 用于向容器注册 bean DataSourceInitializedPublisher,
// DataSourceInitializedPublisher 是一个 BeanPostProcessor
@Import(DataSourceInitializedPublisher.Registrar.class)
public abstract class JpaBaseConfiguration implements BeanFactoryAware {

	private final DataSource dataSource;

	private final JpaProperties properties;

	private final JtaTransactionManager jtaTransactionManager;

	private final TransactionManagerCustomizers transactionManagerCustomizers;

	private ConfigurableListableBeanFactory beanFactory;

    // dataSource 在其他地方定义 : 
    // jtaTransactionManager 在其他地方定义 : 
    // transactionManagerCustomizers 在其他地方定义 : 
    // JpaProperties properties 来源参考本类注解
	protected JpaBaseConfiguration(DataSource dataSource, JpaProperties properties,
			ObjectProvider<JtaTransactionManager> jtaTransactionManager,
			ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
		this.dataSource = dataSource;
		this.properties = properties;
		this.jtaTransactionManager = jtaTransactionManager.getIfAvailable();
		this.transactionManagerCustomizers = transactionManagerCustomizers
				.getIfAvailable();
	}

    // 定义 bean 事务处理器 PlatformTransactionManager transactionManager,
    // 缺省情况下的实现类使用 JpaTransactionManager
	@Bean
    // 仅在该 bean 未被定义的情况下才定义
	@ConditionalOnMissingBean
	public PlatformTransactionManager transactionManager() {
		JpaTransactionManager transactionManager = new JpaTransactionManager();
		if (this.transactionManagerCustomizers != null) {
			this.transactionManagerCustomizers.customize(transactionManager);
		}
		return transactionManager;
	}

    // 定义 bean JpaVendorAdapter jpaVendorAdapter, JPA供应商实现适配器
	@Bean
    // 仅在该 bean 未被定义的情况下才定义
	@ConditionalOnMissingBean   
	public JpaVendorAdapter jpaVendorAdapter() {
		AbstractJpaVendorAdapter adapter = createJpaVendorAdapter();
		adapter.setShowSql(this.properties.isShowSql());
		adapter.setDatabase(this.properties.determineDatabase(this.dataSource));
		adapter.setDatabasePlatform(this.properties.getDatabasePlatform());
		adapter.setGenerateDdl(this.properties.isGenerateDdl());
		return adapter;
	}

    // 定义 bean EntityManagerFactoryBuilder entityManagerFactoryBuilder
    // 该 bean 会使用其他地方定义的 JpaVendorAdapter, PersistenceUnitManager
    // 构建一个 EntityManagerFactoryBuilder, 这是一个 EntityManagerFactory 构建器
    // 此 bean 可以理解成底层组件提供者 JpaVendorAdapter, PersistenceUnitManager 跟
    // 上层 LocalContainerEntityManagerFactoryBean 使用者之间的一个纽带
	@Bean
    // 仅在该 bean 未被定义的情况下才定义
	@ConditionalOnMissingBean
	public EntityManagerFactoryBuilder entityManagerFactoryBuilder(
			JpaVendorAdapter jpaVendorAdapter,
			ObjectProvider<PersistenceUnitManager> persistenceUnitManager,
			ObjectProvider<EntityManagerFactoryBuilderCustomizer> customizers) {
		EntityManagerFactoryBuilder builder = new EntityManagerFactoryBuilder(
				jpaVendorAdapter, this.properties.getProperties(),
				persistenceUnitManager.getIfAvailable());
		customizers.orderedStream()
				.forEach((customizer) -> customizer.customize(builder));
		return builder;
	}

    // 定义 bean LocalContainerEntityManagerFactoryBean entityManagerFactory
    // 该 bean 定义方法使用上面定义的 EntityManagerFactoryBuilder bean 执行其构建过程,
    // 产出物为一个 LocalContainerEntityManagerFactoryBean , 这其实是一个
    // EntityManagerFactory, EntityManager 的工厂组件 bean
	@Bean
	@Primary
    // 仅在同类型 bean 和 类型为 EntityManagerFactory 的 bean 未被定义的情况下才定义
	@ConditionalOnMissingBean({ LocalContainerEntityManagerFactoryBean.class,
			EntityManagerFactory.class })
	public LocalContainerEntityManagerFactoryBean entityManagerFactory(
			EntityManagerFactoryBuilder factoryBuilder) {
		Map<String, Object> vendorProperties = getVendorProperties();
		customizeVendorProperties(vendorProperties);
		return factoryBuilder.dataSource(this.dataSource).packages(getPackagesToScan())
				.properties(vendorProperties).mappingResources(getMappingResources())
				.jta(isJta()).build();
	}

	protected abstract AbstractJpaVendorAdapter createJpaVendorAdapter();

	protected abstract Map<String, Object> getVendorProperties();

	/**
	 * Customize vendor properties before they are used. Allows for post processing (for
	 * example to configure JTA specific settings).
	 * @param vendorProperties the vendor properties to customize
	 */
	protected void customizeVendorProperties(Map<String, Object> vendorProperties) {
	}

	protected String[] getPackagesToScan() {
		List<String> packages = EntityScanPackages.get(this.beanFactory)
				.getPackageNames();
		if (packages.isEmpty() && AutoConfigurationPackages.has(this.beanFactory)) {
			packages = AutoConfigurationPackages.get(this.beanFactory);
		}
		return StringUtils.toStringArray(packages);
	}

	private String[] getMappingResources() {
		List<String> mappingResources = this.properties.getMappingResources();
		return (!ObjectUtils.isEmpty(mappingResources)
				? StringUtils.toStringArray(mappingResources) : null);
	}

	/**
	 * Return the JTA transaction manager.
	 * @return the transaction manager or null
	 */
	protected JtaTransactionManager getJtaTransactionManager() {
		return this.jtaTransactionManager;
	}

	/**
	 * Returns if a JTA PlatformTransactionManager is being used.
	 * @return if a JTA transaction manager is being used
	 */
	protected final boolean isJta() {
		return (this.jtaTransactionManager != null);
	}

	/**
	 * Return the JpaProperties.
	 * @return the properties
	 */
	protected final JpaProperties getProperties() {
		return this.properties;
	}

	/**
	 * Return the DataSource.
	 * @return the data source
	 */
	protected final DataSource getDataSource() {
		return this.dataSource;
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
	}


    // 内部配置类, 对 Open Session In View 模式的支持
	@Configuration
	@ConditionalOnWebApplication(type = Type.SERVLET)
	@ConditionalOnClass(WebMvcConfigurer.class)
	@ConditionalOnMissingBean({ OpenEntityManagerInViewInterceptor.class,
			OpenEntityManagerInViewFilter.class })
	@ConditionalOnMissingFilterBean(OpenEntityManagerInViewFilter.class)
	@ConditionalOnProperty(prefix = "spring.jpa", name = "open-in-view", 
		havingValue = "true", matchIfMissing = true)
	protected static class JpaWebConfiguration {

		// Defined as a nested config to ensure WebMvcConfigurerAdapter is not read when
		// not on the classpath
		@Configuration
		protected static class JpaWebMvcConfiguration implements WebMvcConfigurer {

			private static final Log logger = LogFactory
					.getLog(JpaWebMvcConfiguration.class);

			private final JpaProperties jpaProperties;

			protected JpaWebMvcConfiguration(JpaProperties jpaProperties) {
				this.jpaProperties = jpaProperties;
			}

			@Bean
			public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() {
				if (this.jpaProperties.getOpenInView() == null) {
					logger.warn("spring.jpa.open-in-view is enabled by default. "
							+ "Therefore, database queries may be performed during view "
							+ "rendering. Explicitly configure "
							+ "spring.jpa.open-in-view to disable this warning");
				}
				return new OpenEntityManagerInViewInterceptor();
			}

			@Override
			public void addInterceptors(InterceptorRegistry registry) {
				registry.addWebRequestInterceptor(openEntityManagerInViewInterceptor());
			}

		}

	}

}

参考文章

![图片说明](https://img-ask.csdn.net/upload/201905/06/1557155946_451269.png) ![图片说明](https://img-ask.csdn.net/upload/201905/06/1557155952_880014.png) Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-05-06 23:19:40.169 ERROR 11948 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactoryBean' defined in class path resource [com/jordan/xunwuproject/config/JPAConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactoryBean' threw exception; nested exception is java.lang.IllegalStateException: @Bean method JPAConfig.dataSource called as bean reference for type [javax.activation.DataSource] but overridden by non-compatible bean instance of type [com.zaxxer.hikari.HikariDataSource]. Overriding bean of same name declared in: class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:456) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1305) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1144) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE] at com.jordan.xunwuproject.XunwuProjectApplication.main(XunwuProjectApplication.java:10) [classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_162] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_162] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_162] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_162] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.3.RELEASE.jar:2.1.3.RELEASE] Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactoryBean' threw exception; nested exception is java.lang.IllegalStateException: @Bean method JPAConfig.dataSource called as bean reference for type [javax.activation.DataSource] but overridden by non-compatible bean instance of type [com.zaxxer.hikari.HikariDataSource]. Overriding bean of same name declared in: class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] ... 24 common frames omitted Caused by: java.lang.IllegalStateException: @Bean method JPAConfig.dataSource called as bean reference for type [javax.activation.DataSource] but overridden by non-compatible bean instance of type [com.zaxxer.hikari.HikariDataSource]. Overriding bean of same name declared in: class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.resolveBeanReference(ConfigurationClassEnhancer.java:418) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:366) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE] at com.jordan.xunwuproject.config.JPAConfig$$EnhancerBySpringCGLIB$$6c882e7f.dataSource(<generated>) ~[classes/:na] at com.jordan.xunwuproject.config.JPAConfig.entityManagerFactoryBean(JPAConfig.java:37) ~[classes/:na] at com.jordan.xunwuproject.config.JPAConfig$$EnhancerBySpringCGLIB$$6c882e7f.CGLIB$entityManagerFactoryBean$2(<generated>) ~[classes/:na] at com.jordan.xunwuproject.config.JPAConfig$$EnhancerBySpringCGLIB$$6c882e7f$$FastClassBySpringCGLIB$$50c56f90.invoke(<generated>) ~[classes/:na] at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE] at com.jordan.xunwuproject.config.JPAConfig$$EnhancerBySpringCGLIB$$6c882e7f.entityManagerFactoryBean(<generated>) ~[classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_162] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_162] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_162] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_162] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] ... 25 common frames omitted
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页