Spring Boot对JPA的自动配置

一 点睛

Spring Boot对JPA的自动配置放置在下面路径下:

Spring Boot对JPA的自动配置_第1张图片

二 源码分析

1  HibernateJpaAutoConfiguration类分析

//Spring Boot默认JPA的实现者是Hibernate
@Configuration
@ConditionalOnClass({ LocalContainerEntityManagerFactoryBean.class,
        EnableTransactionManagement.class, EntityManager.class })
@Conditional(HibernateEntityManagerCondition.class)
//HibernateJpaAutoConfiguration依赖于DataSourceAutoConfiguration
@AutoConfigureAfter({ DataSourceAutoConfiguration.class })
public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
......
}

2 JpaProperties类分析

//配置JPA可以使用spring.jpa为前缀的属性在application.properties中配置
@ConfigurationProperties(prefix = "spring.jpa")
public class JpaProperties {
......
}

3 JpaBaseConfiguration源码分析

@EnableConfigurationProperties(JpaProperties.class)
@Import(DataSourceInitializedPublisher.Registrar.class)
public abstract class JpaBaseConfiguration implements BeanFactoryAware {

    private static final String[] NO_PACKAGES = new String[0];

    private ConfigurableListableBeanFactory beanFactory;

    @Autowired
    private DataSource dataSource;

    @Autowired(required = false)
    private PersistenceUnitManager persistenceUnitManager;

    @Autowired
    private JpaProperties jpaProperties;

    @Autowired(required = false)
    private JtaTransactionManager jtaTransactionManager;

    //配置了JpaTransactionManager Bean
    @Bean
    @ConditionalOnMissingBean(PlatformTransactionManager.class)
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager();
    }

    //配置了JpaVendorAdapter Bean
    @Bean
    @ConditionalOnMissingBean
    public JpaVendorAdapter jpaVendorAdapter() {
        AbstractJpaVendorAdapter adapter = createJpaVendorAdapter();
        adapter.setShowSql(this.jpaProperties.isShowSql());
        adapter.setDatabase(this.jpaProperties.getDatabase());
        adapter.setDatabasePlatform(this.jpaProperties.getDatabasePlatform());
        adapter.setGenerateDdl(this.jpaProperties.isGenerateDdl());
        return adapter;
    }

    //配置了EntityManagerFactoryBuilder Bean
    @Bean
    @ConditionalOnMissingBean
    public EntityManagerFactoryBuilder entityManagerFactoryBuilder(
            JpaVendorAdapter jpaVendorAdapter) {
        EntityManagerFactoryBuilder builder = new EntityManagerFactoryBuilder(
                jpaVendorAdapter, this.jpaProperties.getProperties(),
                this.persistenceUnitManager);
        builder.setCallback(getVendorCallback());
        return builder;
    }

    @Bean
    @Primary
    @ConditionalOnMissingBean({ LocalContainerEntityManagerFactoryBean.class,
            EntityManagerFactory.class })
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            EntityManagerFactoryBuilder factoryBuilder) {
        Map vendorProperties = getVendorProperties();
        customizeVendorProperties(vendorProperties);
        return factoryBuilder.dataSource(this.dataSource).packages(getPackagesToScan())
                .properties(vendorProperties).jta(isJta()).build();
    }

    protected abstract AbstractJpaVendorAdapter createJpaVendorAdapter();

    protected abstract Map getVendorProperties();

    protected void customizeVendorProperties(Map vendorProperties) {
    }

    protected EntityManagerFactoryBuilder.EntityManagerFactoryBeanCallback getVendorCallback() {
        return null;
    }

    //自动扫描注解有@Entity的实体类
    protected String[] getPackagesToScan() {
        if (AutoConfigurationPackages.has(this.beanFactory)) {
            List basePackages = AutoConfigurationPackages.get(this.beanFactory);
            return basePackages.toArray(new String[basePackages.size()]);
        }
        return NO_PACKAGES;
    }

    protected JtaTransactionManager getJtaTransactionManager() {
        return this.jtaTransactionManager;
    }

    protected final boolean isJta() {
        return (this.jtaTransactionManager != null);
    }

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

    @Configuration
    @ConditionalOnWebApplication
    @ConditionalOnClass(WebMvcConfigurerAdapter.class)
    @ConditionalOnMissingBean({ OpenEntityManagerInViewInterceptor.class,
            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 extends WebMvcConfigurerAdapter {

            //自动配置OpenEntityManagerInViewInterceptor这个Bean
            @Bean
            public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() {
                return new OpenEntityManagerInViewInterceptor();
            }

            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //将OpenEntityManagerInViewInterceptor这个Bean注册到Spring MVC的拦截器中
                registry.addWebRequestInterceptor(openEntityManagerInViewInterceptor());
            }

        }

    }

}

 

你可能感兴趣的:(Spring,Boot)