Kotlin @LastModifiedBy不';不要在协同程序中工作
在我的应用程序中,我使用了Kotlin @LastModifiedBy不';不要在协同程序中工作,kotlin,spring-data-jpa,kotlin-coroutines,Kotlin,Spring Data Jpa,Kotlin Coroutines,在我的应用程序中,我使用了@LastModifiedBy,它工作得很好,直到我决定为mather使用联合例程 现在,由于对my repositories的调用位于将来的内部,并在协同程序中执行,因此在持久化时,标记为@LastModifiedBy或@LastModifiedDate的字段不再填充 当我调试并在执行到协同程序的代码内部停止时,SecurityContextHolder为空,而它在协同程序外部填充 我的代码如下所示: @RestController class Controller(
@LastModifiedBy
,它工作得很好,直到我决定为mather使用联合例程
现在,由于对my repositories的调用位于将来的内部,并在协同程序中执行,因此在持久化时,标记为@LastModifiedBy
或@LastModifiedDate
的字段不再填充
当我调试并在执行到协同程序的代码内部停止时,SecurityContextHolder
为空,而它在协同程序外部填充
我的代码如下所示:
@RestController
class Controller( val service : MyService){
( ... )
@PutMapping(...)
fun saveStuff( val allStuff : List<Stuff>) : String{
return service.saveStuff(allStuff )
}
}
我是这样做的:
@Service
class MyService( val repo: MyRepository){
fun saveStuff( allStuff: List<Stuff>){
val deferred: List<Deferred<Unit>> =
allStuff.forEach{
GlobalScope.async { repo.save(stuff)}
}
val threadLocal = ThreadLocal<SecurityContext>() // declare thread-local variable
//pass security context to all thread which will execute my code
GlobalScope.launch(Dispatchers.Default + threadLocal.asContextElement(SecurityContextHolder.getContext())) {
//set up context for spring
SecurityContextHolder.setContext(threadLocal.get())
val count = deferred.map { it.await() }.count()
log.info(" all the $count future(s) have finish ")
}
}
}
@服务
类MyService(val repo:MyRepository){
有趣的存储工具(所有工具:列表){
val延迟:列表=
所有的东西{
GlobalScope.async{repo.save(stuff)}
}
val threadLocal=threadLocal()//声明线程局部变量
//将安全上下文传递给将执行我的代码的所有线程
GlobalScope.launch(Dispatchers.Default+threadLocal.asContextElement(SecurityContextHolder.getContext())){
//为spring设置上下文
SecurityContextHolder.setContext(threadLocal.get())
val count=deferred.map{it.await()}.count()
log.info(“所有$count未来都已完成”)
}
}
}
它就像一个符咒:)
谢谢@sidgate的链接 我是这样做的:
@Service
class MyService( val repo: MyRepository){
fun saveStuff( allStuff: List<Stuff>){
val deferred: List<Deferred<Unit>> =
allStuff.forEach{
GlobalScope.async { repo.save(stuff)}
}
val threadLocal = ThreadLocal<SecurityContext>() // declare thread-local variable
//pass security context to all thread which will execute my code
GlobalScope.launch(Dispatchers.Default + threadLocal.asContextElement(SecurityContextHolder.getContext())) {
//set up context for spring
SecurityContextHolder.setContext(threadLocal.get())
val count = deferred.map { it.await() }.count()
log.info(" all the $count future(s) have finish ")
}
}
}
@服务
类MyService(val repo:MyRepository){
有趣的存储工具(所有工具:列表){
val延迟:列表=
所有的东西{
GlobalScope.async{repo.save(stuff)}
}
val threadLocal=threadLocal()//声明线程局部变量
//将安全上下文传递给将执行我的代码的所有线程
GlobalScope.launch(Dispatchers.Default+threadLocal.asContextElement(SecurityContextHolder.getContext())){
//为spring设置上下文
SecurityContextHolder.setContext(threadLocal.get())
val count=deferred.map{it.await()}.count()
log.info(“所有$count未来都已完成”)
}
}
}
它就像一个符咒:)
谢谢@sidgate的链接
@Service
class MyService( val repo: MyRepository){
fun saveStuff( allStuff: List<Stuff>){
val deferred: List<Deferred<Unit>> =
allStuff.forEach{
GlobalScope.async { repo.save(stuff)}
}
val threadLocal = ThreadLocal<SecurityContext>() // declare thread-local variable
//pass security context to all thread which will execute my code
GlobalScope.launch(Dispatchers.Default + threadLocal.asContextElement(SecurityContextHolder.getContext())) {
//set up context for spring
SecurityContextHolder.setContext(threadLocal.get())
val count = deferred.map { it.await() }.count()
log.info(" all the $count future(s) have finish ")
}
}
}