001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.hazelcast; 018 019import javax.persistence.EntityManagerFactory; 020 021import com.hazelcast.core.HazelcastInstance; 022 023import org.springframework.boot.autoconfigure.AutoConfigureAfter; 024import org.springframework.boot.autoconfigure.condition.AllNestedConditions; 025import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 026import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 027import org.springframework.boot.autoconfigure.data.jpa.EntityManagerFactoryDependsOnPostProcessor; 028import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; 029import org.springframework.context.annotation.Bean; 030import org.springframework.context.annotation.Conditional; 031import org.springframework.context.annotation.Configuration; 032import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean; 033import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 034 035/** 036 * Additional configuration to ensure that {@link EntityManagerFactory} beans depend on 037 * the {@code hazelcastInstance} bean. 038 * 039 * @author Stephane Nicoll 040 * @since 1.3.2 041 */ 042@Configuration 043@ConditionalOnClass({ HazelcastInstance.class, 044 LocalContainerEntityManagerFactoryBean.class }) 045@AutoConfigureAfter({ HazelcastAutoConfiguration.class, 046 HibernateJpaAutoConfiguration.class }) 047public class HazelcastJpaDependencyAutoConfiguration { 048 049 @Bean 050 @Conditional(OnHazelcastAndJpaCondition.class) 051 public static HazelcastInstanceJpaDependencyPostProcessor hazelcastInstanceJpaDependencyPostProcessor() { 052 return new HazelcastInstanceJpaDependencyPostProcessor(); 053 } 054 055 private static class HazelcastInstanceJpaDependencyPostProcessor 056 extends EntityManagerFactoryDependsOnPostProcessor { 057 058 HazelcastInstanceJpaDependencyPostProcessor() { 059 super("hazelcastInstance"); 060 } 061 062 } 063 064 static class OnHazelcastAndJpaCondition extends AllNestedConditions { 065 066 OnHazelcastAndJpaCondition() { 067 super(ConfigurationPhase.REGISTER_BEAN); 068 } 069 070 @ConditionalOnBean(name = "hazelcastInstance") 071 static class HasHazelcastInstance { 072 073 } 074 075 @ConditionalOnBean(AbstractEntityManagerFactoryBean.class) 076 static class HasJpa { 077 078 } 079 080 } 081 082}