1.1.2 优化代码
This commit is contained in:
@@ -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}]的集合")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user