001/* 002 * Copyright 2024-2025, Warm-Flow (290631660@qq.com). 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 * https://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 */ 016package org.dromara.warm.flow.spring.boot.config; 017 018import org.dromara.warm.flow.core.constant.ExceptionCons; 019import org.dromara.warm.flow.core.exception.FlowException; 020import org.dromara.warm.flow.orm.utils.FlowJpaConfigCons; 021import org.dromara.warm.plugin.modes.sb.config.BeanConfig; 022import org.dromara.warm.plugin.modes.sb.utils.SpringUtil; 023import org.slf4j.Logger; 024import org.slf4j.LoggerFactory; 025import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 026import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 027import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; 028import org.springframework.context.annotation.Bean; 029import org.springframework.context.annotation.Configuration; 030import org.springframework.context.annotation.DependsOn; 031import org.springframework.core.env.Environment; 032import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 033 034import javax.persistence.spi.PersistenceProvider; 035import javax.sql.DataSource; 036 037/** 038 * 工作流bean注册配置 039 * 040 * @author warm 041 * @since 2023/6/5 23:01 042 */ 043@Configuration 044@ConditionalOnProperty(value = "warm-flow.enabled", havingValue = "true", matchIfMissing = true) 045public class FlowAutoConfig extends BeanConfig { 046 047 private static final Logger log = LoggerFactory.getLogger(FlowAutoConfig.class); 048 049 @Bean 050 @ConditionalOnMissingBean 051 public JpaProperties jpaProperties() { 052 return new JpaProperties(); 053 } 054 055 @SuppressWarnings({"unchecked"}) 056 @Bean(name = "entityManagerFactoryWarmFlow") 057 @DependsOn("warmFlowSpringUtil") 058 public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(DataSource dataSource, JpaProperties jpaProperties) throws ClassNotFoundException { 059 LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); 060 factory.setDataSource(dataSource); 061 factory.setJpaPropertyMap(jpaProperties.getProperties()); 062 factory.setMappingResources("META-INF/warm-flow-orm.xml"); 063 factory.setPersistenceXmlLocation("classpath:/META-INF/warm-flow-persistence.xml"); 064 String jpaPersistenceProvider = SpringUtil.getBean(Environment.class).getProperty(FlowJpaConfigCons.JPA_PERSISTENCE_PROVIDER); 065 log.info("warm-flow use jpa persistence provider to be: {}", jpaPersistenceProvider); 066 if (jpaPersistenceProvider == null) { 067 throw new FlowException(ExceptionCons.JPA_PERSISTENCE_PROVIDER_NOT_FOUND); 068 } 069 factory.setPersistenceProviderClass((Class<? extends PersistenceProvider>) Class.forName(jpaPersistenceProvider)); 070 factory.setPersistenceUnitName("warm-flow-jpa"); 071 return factory; 072 } 073}