1.1.2 优化代码
This commit is contained in:
@@ -17,7 +17,7 @@ buildscript {
|
||||
|
||||
subprojects {
|
||||
group 'com.synebula'
|
||||
version '1.1.0'
|
||||
version '1.1.2'
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
|
||||
@@ -47,7 +47,7 @@ class ServiceProxy(
|
||||
*/
|
||||
override fun exec(proxy: Any, method: Method, args: Array<Any>): Any? {
|
||||
try {
|
||||
val proxyMethod = this.service.javaClass.getDeclaredMethod(method.name, *method.parameterTypes)
|
||||
val proxyMethod = this.service.javaClass.getMethod(method.name, *method.parameterTypes)
|
||||
return proxyMethod.invoke(this.service, *args)
|
||||
} catch (ex: NoSuchMethodException) {
|
||||
throw NoSuchMethodException("method [${method.toGenericString()}] not implements in class [${this.service::class.java}], you must implements interface [${this.supertype.name}] ")
|
||||
|
||||
@@ -1,59 +1,41 @@
|
||||
package com.synebula.gaea.mongodb.autoconfig
|
||||
|
||||
import com.synebula.gaea.domain.repository.IRepository
|
||||
import com.synebula.gaea.log.ILogger
|
||||
import com.synebula.gaea.mongodb.query.MongodbQuery
|
||||
import com.synebula.gaea.mongodb.repository.MongodbRepository
|
||||
import com.synebula.gaea.query.IQuery
|
||||
import com.synebula.gaea.reflect.getGenericInterface
|
||||
import com.synebula.gaea.spring.autoconfig.Proxy
|
||||
import org.springframework.beans.factory.BeanFactory
|
||||
import org.springframework.data.mongodb.core.MongoTemplate
|
||||
import java.io.InvalidClassException
|
||||
import java.lang.reflect.Method
|
||||
import java.lang.reflect.ParameterizedType
|
||||
|
||||
class MongodbRepoProxy(
|
||||
private var supertype: Class<*>, private var beanFactory: BeanFactory, implementBeanNames: Array<String> = arrayOf()
|
||||
) : Proxy() {
|
||||
|
||||
private var repo: IRepository<*, *>? = null
|
||||
|
||||
private var query: IQuery<*, *>? = null
|
||||
private var mongodbRepo: Any
|
||||
|
||||
init {
|
||||
// 判断接口类型
|
||||
val clazz: Class<*> // 代理服务类型
|
||||
val interfaceClazz: Class<*> // 代理服务接口
|
||||
if (this.supertype.interfaces.any { it == IRepository::class.java }) {
|
||||
// 如果是IRepository子接口
|
||||
if (implementBeanNames.isEmpty()) {
|
||||
val genericInterfaces = this.supertype.genericInterfaces.find {
|
||||
it.typeName.startsWith(IRepository::class.java.typeName)
|
||||
}!!
|
||||
val constructor = MongodbRepository::class.java.getConstructor(
|
||||
Class::class.java, MongoTemplate::class.java
|
||||
)
|
||||
this.repo = constructor.newInstance(
|
||||
(genericInterfaces as ParameterizedType).actualTypeArguments[0],
|
||||
this.beanFactory.getBean(MongoTemplate::class.java)
|
||||
)
|
||||
} else {
|
||||
this.repo = this.beanFactory.getBean(implementBeanNames[0]) as IRepository<*, *>
|
||||
}
|
||||
clazz = MongodbRepository::class.java
|
||||
interfaceClazz = IRepository::class.java
|
||||
} else {
|
||||
// 否则是IQuery子接口
|
||||
if (implementBeanNames.isEmpty()) {
|
||||
val genericInterfaces = this.supertype.genericInterfaces.find {
|
||||
it.typeName.startsWith(IQuery::class.java.typeName)
|
||||
}!!
|
||||
val constructor = MongodbQuery::class.java.getConstructor(
|
||||
Class::class.java, MongoTemplate::class.java, ILogger::class.java
|
||||
)
|
||||
this.query = constructor.newInstance(
|
||||
(genericInterfaces as ParameterizedType).actualTypeArguments[0],
|
||||
this.beanFactory.getBean(MongoTemplate::class.java),
|
||||
this.beanFactory.getBean(ILogger::class.java),
|
||||
)
|
||||
} else {
|
||||
this.query = this.beanFactory.getBean(implementBeanNames[0]) as IQuery<*, *>
|
||||
}
|
||||
clazz = MongodbQuery::class.java
|
||||
interfaceClazz = IQuery::class.java
|
||||
}
|
||||
|
||||
if (implementBeanNames.isEmpty()) {
|
||||
val constructor = clazz.getConstructor(Class::class.java, MongoTemplate::class.java)
|
||||
this.mongodbRepo = constructor.newInstance(
|
||||
this.supertype.getGenericInterface(interfaceClazz)!!.actualTypeArguments[0],
|
||||
this.beanFactory.getBean(MongoTemplate::class.java)
|
||||
)
|
||||
} else {
|
||||
this.mongodbRepo = this.beanFactory.getBean(implementBeanNames[0])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,18 +48,11 @@ class MongodbRepoProxy(
|
||||
* @return 方法执行结果
|
||||
*/
|
||||
override fun exec(proxy: Any, method: Method, args: Array<Any>): Any? {
|
||||
val proxyClazz = if (this.repo != null) {
|
||||
this.repo!!.javaClass
|
||||
} else if (this.query != null) {
|
||||
this.query!!.javaClass
|
||||
} else
|
||||
throw InvalidClassException("class [${this.supertype.name}] proxy object property [repo] and [query] are both null")
|
||||
|
||||
try {
|
||||
val proxyMethod: Method = proxyClazz.getDeclaredMethod(method.name, *method.parameterTypes)
|
||||
return proxyMethod.invoke(this.repo, *args)
|
||||
val proxyMethod: Method = this.mongodbRepo.javaClass.getMethod(method.name, *method.parameterTypes)
|
||||
return proxyMethod.invoke(this.mongodbRepo, *args)
|
||||
} catch (ex: NoSuchMethodException) {
|
||||
throw NoSuchMethodException("method [${method.toGenericString()}] not implements in class [${proxyClazz}], you must implements interface [${this.supertype.name}] ")
|
||||
throw NoSuchMethodException("method [${method.toGenericString()}] not implements in class [${this.mongodbRepo.javaClass}], you must implements interface [${this.supertype.name}] ")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.synebula.gaea.mongodb.query
|
||||
|
||||
import com.synebula.gaea.ext.fieldNames
|
||||
import com.synebula.gaea.ext.firstCharLowerCase
|
||||
import com.synebula.gaea.log.ILogger
|
||||
import com.synebula.gaea.mongodb.order
|
||||
import com.synebula.gaea.mongodb.select
|
||||
import com.synebula.gaea.mongodb.where
|
||||
@@ -20,7 +19,7 @@ import org.springframework.data.mongodb.core.query.Query
|
||||
* @param template MongodbRepo对象
|
||||
*/
|
||||
|
||||
open class MongodbQuery<TView, ID>(override var clazz: Class<TView>, var template: MongoTemplate, var logger: ILogger) :
|
||||
open class MongodbQuery<TView, ID>(override var clazz: Class<TView>, var template: MongoTemplate) :
|
||||
IQuery<TView, ID> {
|
||||
|
||||
/**
|
||||
@@ -85,18 +84,13 @@ open class MongodbQuery<TView, ID>(override var clazz: Class<TView>, var templat
|
||||
/**
|
||||
* 获取collection
|
||||
*/
|
||||
protected fun collection(clazz: Class<TView>): String {
|
||||
val table: Table? = clazz.getDeclaredAnnotation(
|
||||
Table::class.java
|
||||
)
|
||||
return if (table != null) return table.name
|
||||
fun <TView> collection(clazz: Class<TView>): String {
|
||||
val table = clazz.getDeclaredAnnotation(Table::class.java)
|
||||
return if (table != null) table.name
|
||||
else {
|
||||
this.logger.info(this, "视图类没有标记[Collection]注解,无法获取Collection名称。尝试使用View<${clazz.name}>名称解析集合")
|
||||
val name = clazz.simpleName.removeSuffix("View").firstCharLowerCase()
|
||||
if (!validViewCollection || this.template.collectionExists(name)) name
|
||||
else {
|
||||
throw RuntimeException("找不到名为[${clazz.name}]的集合")
|
||||
}
|
||||
else throw RuntimeException("找不到名为[${clazz.name}]的集合")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.synebula.gaea.mongodb.query
|
||||
|
||||
import com.synebula.gaea.ext.fieldNames
|
||||
import com.synebula.gaea.ext.firstCharLowerCase
|
||||
import com.synebula.gaea.log.ILogger
|
||||
import com.synebula.gaea.mongodb.order
|
||||
import com.synebula.gaea.mongodb.select
|
||||
import com.synebula.gaea.mongodb.where
|
||||
@@ -20,7 +19,7 @@ import org.springframework.data.mongodb.core.query.Query
|
||||
* 实现IQuery的Mongodb查询类
|
||||
* @param template MongodbRepo对象
|
||||
*/
|
||||
open class MongodbUniversalQuery(var template: MongoTemplate, var logger: ILogger) : IUniversalQuery {
|
||||
open class MongodbUniversalQuery(var template: MongoTemplate) : IUniversalQuery {
|
||||
|
||||
/**
|
||||
* 使用View解析是collection时是否校验存在,默认不校验
|
||||
@@ -85,19 +84,12 @@ open class MongodbUniversalQuery(var template: MongoTemplate, var logger: ILogge
|
||||
* 获取collection
|
||||
*/
|
||||
fun <TView> collection(clazz: Class<TView>): String {
|
||||
val table: Table? = clazz.getDeclaredAnnotation(
|
||||
Table::class.java
|
||||
)
|
||||
return if (table != null)
|
||||
return table.name
|
||||
val table = clazz.getDeclaredAnnotation(Table::class.java)
|
||||
return if (table != null) table.name
|
||||
else {
|
||||
this.logger.info(this, "视图类没有标记[Collection]注解,无法获取Collection名称。尝试使用View<${clazz.name}>名称解析集合")
|
||||
val name = clazz.simpleName.removeSuffix("View").firstCharLowerCase()
|
||||
if (!validViewCollection || this.template.collectionExists(name))
|
||||
name
|
||||
else {
|
||||
throw RuntimeException("找不到名为[${clazz.name}]的集合")
|
||||
}
|
||||
if (!validViewCollection || this.template.collectionExists(name)) name
|
||||
else throw RuntimeException("找不到名为[${clazz.name}]的集合")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
*/
|
||||
package com.synebula.gaea.bus
|
||||
|
||||
import com.synebula.gaea.reflect.Types
|
||||
import com.synebula.gaea.reflect.supertypes
|
||||
import java.lang.reflect.Method
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.CopyOnWriteArraySet
|
||||
@@ -184,7 +184,7 @@ open class SubscriberRegistry<T : Any>(private val bus: IBus<T>) {
|
||||
fun flattenHierarchy(clazz: Class<*>): Set<Class<*>> {
|
||||
var supertypes = flattenHierarchyCache[clazz]
|
||||
if (supertypes == null) {
|
||||
supertypes = Types.supertypes(clazz)
|
||||
supertypes = clazz.supertypes()
|
||||
flattenHierarchyCache[clazz] = supertypes
|
||||
}
|
||||
return supertypes
|
||||
|
||||
@@ -11,7 +11,7 @@ import com.synebula.gaea.domain.repository.IRepository
|
||||
* 该类依赖仓储接口 @see IGenericsRepository, 需要显式提供聚合根的class对象
|
||||
*
|
||||
* @param clazz 聚合根类对象
|
||||
* @param repo 仓储对象
|
||||
* @param repository 仓储对象
|
||||
* @param mapper 对象转换组件
|
||||
* @author alex
|
||||
* @version 0.1
|
||||
@@ -19,11 +19,10 @@ import com.synebula.gaea.domain.repository.IRepository
|
||||
*/
|
||||
open class Service<TRoot : IAggregateRoot<ID>, ID>(
|
||||
protected open var clazz: Class<TRoot>,
|
||||
protected open var repo: IRepository<TRoot, ID>,
|
||||
protected open var repository: IRepository<TRoot, ID>,
|
||||
protected open var mapper: IObjectMapper,
|
||||
) : IService<ID> {
|
||||
|
||||
|
||||
/**
|
||||
* 增加对象
|
||||
*
|
||||
@@ -32,7 +31,7 @@ open class Service<TRoot : IAggregateRoot<ID>, ID>(
|
||||
override fun add(command: ICommand): DataMessage<ID> {
|
||||
val msg = DataMessage<ID>()
|
||||
val root = this.map(command)
|
||||
this.repo.add(root)
|
||||
this.repository.add(root)
|
||||
msg.data = root.id
|
||||
return msg
|
||||
}
|
||||
@@ -44,7 +43,7 @@ open class Service<TRoot : IAggregateRoot<ID>, ID>(
|
||||
*/
|
||||
override fun add(commands: List<ICommand>) {
|
||||
val roots = commands.map { this.map(it) }
|
||||
this.repo.add(roots)
|
||||
this.repository.add(roots)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +55,7 @@ open class Service<TRoot : IAggregateRoot<ID>, ID>(
|
||||
override fun update(id: ID, command: ICommand) {
|
||||
val root = this.map(command)
|
||||
root.id = id
|
||||
this.repo.update(root)
|
||||
this.repository.update(root)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,7 +65,7 @@ open class Service<TRoot : IAggregateRoot<ID>, ID>(
|
||||
*/
|
||||
override fun update(commands: List<ICommand>) {
|
||||
val roots = commands.map { this.map(it) }
|
||||
this.repo.update(roots)
|
||||
this.repository.update(roots)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,7 +73,7 @@ open class Service<TRoot : IAggregateRoot<ID>, ID>(
|
||||
* @param id 对象ID
|
||||
*/
|
||||
override fun remove(id: ID) {
|
||||
this.repo.remove(id)
|
||||
this.repository.remove(id)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,21 +1,28 @@
|
||||
package com.synebula.gaea.reflect
|
||||
|
||||
object Types {
|
||||
/**
|
||||
* 获取类的所有父类型。
|
||||
*/
|
||||
fun supertypes(clazz: Class<*>): Set<Class<*>> {
|
||||
val supertypes = mutableSetOf<Class<*>>()
|
||||
supertypes.add(clazz)
|
||||
if (clazz.interfaces.isNotEmpty()) {
|
||||
supertypes.addAll(clazz.interfaces.map { supertypes(it) }.reduce { r, c ->
|
||||
val all = r.toMutableSet()
|
||||
all.addAll(c)
|
||||
all
|
||||
})
|
||||
}
|
||||
if (clazz.superclass != null)
|
||||
supertypes.addAll(supertypes(clazz.superclass))
|
||||
return supertypes
|
||||
import java.lang.reflect.ParameterizedType
|
||||
|
||||
/**
|
||||
* 获取类的所有父类型。
|
||||
*/
|
||||
fun Class<*>.supertypes(): Set<Class<*>> {
|
||||
val supertypes = mutableSetOf<Class<*>>()
|
||||
supertypes.add(this)
|
||||
if (this.interfaces.isNotEmpty()) {
|
||||
supertypes.addAll(this.interfaces.map { it.supertypes() }.reduce { r, c ->
|
||||
val all = r.toMutableSet()
|
||||
all.addAll(c)
|
||||
all
|
||||
})
|
||||
}
|
||||
}
|
||||
if (this.superclass != null)
|
||||
supertypes.addAll(this.superclass.supertypes())
|
||||
return supertypes
|
||||
}
|
||||
|
||||
fun Class<*>.getGenericInterface(interfaceClazz: Class<*>): ParameterizedType? {
|
||||
val type = this.genericInterfaces.find { it.typeName.startsWith(interfaceClazz.typeName) }
|
||||
return if (type == null) null
|
||||
else type as ParameterizedType
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user