Created
May 21, 2022 05:14
-
-
Save hanrw/4f55fe95e395bfbbae686979a61594aa to your computer and use it in GitHub Desktop.
Spring create domain entity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> | |
<!-- Entity mappings - override for extensibility --> | |
<bean id="xxx.domain.Wallet" class="xxx.domain.DefaultWallet" scope="prototype"/> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:aop="http://www.springframework.org/schema/aop" | |
xmlns:tx="http://www.springframework.org/schema/tx" | |
xmlns:util="http://www.springframework.org/schema/util" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd | |
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd | |
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd | |
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> | |
<bean id="mergedEntityContexts" class="org.springframework.beans.factory.config.ListFactoryBean"> | |
<property name="sourceList"> | |
<list> | |
<value>classpath:applicationContext-entity.xml</value> | |
</list> | |
</property> | |
</bean> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.logging.LogFactory | |
import org.springframework.beans.BeansException | |
import org.springframework.context.ApplicationContext | |
import org.springframework.context.ApplicationContextAware | |
import org.springframework.context.annotation.ImportResource | |
import org.springframework.context.support.GenericXmlApplicationContext | |
import org.springframework.core.io.Resource | |
import org.springframework.stereotype.Component | |
import javax.annotation.PostConstruct | |
@Component("blEntityConfiguration") | |
@ImportResource("classpath:/applicationContext-persistence.xml") | |
class EntityConfiguration : ApplicationContextAware { | |
private lateinit var webApplicationContext: ApplicationContext | |
private val entityMap = HashMap<String, Class<*>>(50) | |
private lateinit var applicationcontext: ApplicationContext | |
@javax.annotation.Resource(name = "mergedEntityContexts") | |
protected lateinit var mergedEntityContexts: Set<String> | |
var entityContexts: Array<Resource>? = null | |
@Throws(BeansException::class) | |
override fun setApplicationContext(applicationContext: ApplicationContext) { | |
webApplicationContext = applicationContext | |
} | |
@PostConstruct | |
fun configureMergedItems() { | |
val temp: MutableSet<Resource> = LinkedHashSet() | |
if (mergedEntityContexts != null && !mergedEntityContexts!!.isEmpty()) { | |
for (location in mergedEntityContexts) { | |
temp.add(webApplicationContext.getResource(location)) | |
} | |
} | |
if (entityContexts != null) { | |
for (resource in entityContexts!!) { | |
temp.add(resource) | |
} | |
} | |
entityContexts = temp.toTypedArray() | |
applicationcontext = GenericXmlApplicationContext(*entityContexts!!) | |
} | |
fun lookupEntityClass(beanId: String): Class<*> { | |
val clazz: Class<*> | |
if (entityMap.containsKey(beanId)) { | |
clazz = entityMap[beanId]!! | |
} else { | |
val `object` = applicationcontext!!.getBean(beanId) | |
clazz = `object`.javaClass | |
entityMap[beanId] = clazz | |
} | |
if (LOG.isDebugEnabled) { | |
LOG.debug("Returning class (" + clazz.name + ") configured with bean id (" + beanId + ')') | |
} | |
return clazz | |
} | |
val entityBeanNames: Array<String> | |
get() = applicationcontext.beanDefinitionNames | |
fun <T> lookupEntityClass(beanId: String, resultClass: Class<T>?): Class<T> { | |
val clazz: Class<T> | |
if (entityMap.containsKey(beanId)) { | |
clazz = entityMap[beanId] as Class<T> | |
} else { | |
val `object` = applicationcontext.getBean(beanId) | |
clazz = `object`.javaClass as Class<T> | |
entityMap[beanId] = clazz | |
} | |
if (LOG.isDebugEnabled) { | |
LOG.debug("Returning class (" + clazz.name + ") configured with bean id (" + beanId + ')') | |
} | |
return clazz | |
} | |
fun createEntityInstance(beanId: String): Any { | |
val bean = applicationcontext.getBean(beanId) | |
if (LOG.isDebugEnabled) { | |
LOG.debug("Returning instance of class (" + bean.javaClass.name + ") configured with bean id (" + beanId + ')') | |
} | |
return bean | |
} | |
fun <T> createEntityInstance(beanId: String, resultClass: Class<T>): T { | |
val bean = applicationcontext.getBean(beanId) as T | |
if (LOG.isDebugEnabled) { | |
LOG.debug("Returning instance of class (" + bean!!::class.java + ") configured with bean id (" + beanId + ')') | |
} | |
return bean | |
} | |
companion object { | |
private val LOG = LogFactory.getLog( | |
EntityConfiguration::class.java | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment