Tag: 春天

Java vs Kotlin – 同一个类中的Spring @Async方法

我发现在Java中,当您尝试从同一个类中调用@Async方法时,您实际上在同一个线程中运行方法,但是在Kotlin中它以异步方式运行。 看起来像代理包装工作不同。 例: @Service class BasicService { @Scheduled(fixedRate = 1000) fun asyncCall() { log.info("Async call") doAsync() } @Async("myAsyncExecutor") open fun doAsync() { log.info("DO ASYNC AND SLEEP. Thread: ${Thread.currentThread().name}") Thread.sleep(7000) log.info("Finsh async call") } … } 当您使用Kotlin doAsync()通过myAsyncExecutor异步运行时, myAsyncExecutor在Java中运行在相同的线程上,并且调度器将每7秒等待一次,然后再次启动。 那么科特林如何解决这个问题呢?

使用kotlin-spring插件,还是得不到类的打开错误

尽管添加了kotlin-spring插件,我正在获得一个不能最终决定的类,需要开放。 插件的全部目的是不为每个类手动添加open关键字。 请指导我让Kotling-Spring插件使用下面的代码。 的build.gradle buildscript { ext.kotlin_version = "1.1.2" repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version" } } apply plugin: "kotlin" apply plugin: "kotlin-spring" apply plugin: "kotlin-noarg" apply plugin: "idea" repositories { mavenCentral() } dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version" compile"org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" compile "org.springframework:spring-context:4.3.8.RELEASE" testCompile "org.springframework:spring-test:4.3.8.RELEASE" testCompile "junit:junit:4.11" } AppConfig.java @Configuration class AppConfig […]

Spring 5和Kotlin 1.1协程:类型rx.Scheduler不存在

我正在使用Kotlin 1.1.4-3和Spring-context 5.0.0.RELEASE。 在启动项目,我得到这个错误: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'coroutineAnnotationBeanPostProcessor' defined in class path resource [org/springframework/kotlin/experimental/coroutine/context/ProxyCoroutineConfiguration.class]: Unsatisfied dependency expressed through method 'coroutineAnnotationBeanPostProcessor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'globalCoroutineContextResolver' defined in class path resource [org/springframework/kotlin/experimental/coroutine/context/CoroutineContextResolverConfiguration.class]: Unexpected exception during bean creation; nested exception is java.lang.reflect.InvocationTargetException at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:458) at […]

JUnit,@ControllerAdvice和缺少Kotlin中检查的异常

我在Kotlin中写了一个验证顾问 ,当验证失败时抛出EntityValidationException : @Aspect @Named class ValidationAdvisor @Inject constructor(val validator: EntityValidator) { @Around(EVERY_SAVE_AND_UPDATE_TO_DATABASE) fun validate(point: ProceedingJoinPoint): Any { val result: List<ConstraintViolation<Any>> = validator.validate(getEntity(point)) if (isEntityValid(result)) return point.proceed() throw EntityValidationException( violationInfos = result as List<ConstraintViolationInfo> ) } private fun getEntity(point: ProceedingJoinPoint): Any { return point.args[0] } private fun isEntityValid(result: List<ConstraintViolation<Any>>): Boolean { return result.isEmpty() } companion […]

Kotlin春天类方面的启动

我正在尝试在java8的spring项目中使用kotlin。 我通过逐个替换类(java-> kotlin)来做到这一点。 我在Finder的一门课: Finder.java具有这样的结构: @Compoment class Finder { private SomeObject someObject; Finder() { someObject = new SomeObject(); } public void doSomething() { //aspect looks here someObject.do(); } } 我用Finder.kt取代了它 @Compoment open public class Finder { private val someObject : SomeObject constructor() { someObject = SomeObject() } public fun doSomething() { //aspect looks here someObject.do() […]

为什么@Primary有时候不适用于Kotlin类?

我有最怪的问题。 我有Java类A ,我也有Kotlin类KA扩展A ,都是@Component s, KA也用@Primary注释。 在一些组件中, KA是自动装配的,而在其他组件中是A 实际上它比这更奇怪,对于不同的应用程序启动之间的相同的依赖bean,有时候KA会自动装入,有时是A 如果我用Java重写KA ,那么一切都按预期工作。 所有相关类中的属性名称/构造函数参数名称是相同的: @Autowired A a; 。 也不要紧,如果我的Kotlin实现实现一个通用的接口或扩展基类。 所有Kotlin和Java类都生活在src/main/java 。 Kotlin版本是1.1.2-5 ,我用的是jvm8。

使用Spring Boot和Kotlin无法提供动态Web内容

基于Spring Boot 教程来提供动态网页内容,我想在Kotlin中做同样的事情。 我的Kotlin项目是基于这个教程 。 我没有运行这两个教程的代码的问题。 从我的理解,我只需要添加一个控制器,将返回一个模板的引用。 这里HelloController.kt (位于src / main / kotlin / foo / controller下): package foo.controller import org.slf4j.LoggerFactory import org.springframework.stereotype.Controller import org.springframework.web.bind.annotation.RequestMapping @Controller class HelloController { private val log = LoggerFactory.getLogger(HelloController::class.java) @RequestMapping("/") fun hello(): String { log.info("foo") return "index" } } 以下是我想要访问的简单“模板” index.html (位于src / main / resources / templates / index.html下): […]

在@Service中使用Kotlin的Spring Boot @Autowired始终为空

目前我尝试用Kotlin重写我的Java Spring Boot应用程序。 我遇到了一个问题,即在所有使用@Service注释的类中,依赖注入都无法正常工作(所有实例均为null )。 这里是一个例子: @Service @Transactional open class UserServiceController @Autowired constructor(val dsl: DSLContext, val teamService: TeamService) { //dsl and teamService are null in all methods } 在Java中做同样的工作没有任何问题: @Service @Transactional public class UserServiceController { private DSLContext dsl; private TeamService teamService; @Autowired public UserServiceController(DSLContext dsl, TeamService teamService) { this.dsl = dsl; this.teamService = teamService; } […]

春天的数据mongodb和kotlin

使用弹簧数据mongodb使用kotlin时,我遇到了一个问题。 当我尝试从mongodb读取对象时,我得到一个错误,抱怨说我的数据类没有默认的无参数构造函数。 我可以通过给我的数据类中的每个字段的值来解决这个问题,所以编译器会生成一个默认的无参数构造函数。 当然,我不是真的想这样做。 我知道有一个杰克逊kotlin模块,它包含在我的maven文件中。 它适用于反序列化对象,我得到了HTTP,所以我知道春天拿起它。 但似乎春天的数据mongodb不使用杰克逊对象映射器? 有没有办法我可以在春天的数据mongodb中使用杰克逊对象映射器或修复没有非参数构造函数的问题?

在Kotlin函数中使用@Qualifier

在Kotlin中,函数参数始终是阻止在参数级使用@Qualifier()的值。 如果有人想在同一个应用程序中经常访问的不同数据库的配置类中创建多个DataSource ,那么组织这个的推荐方法是什么? 在Kotlin中不允许执行以下在Java中相当常见的操作。 @Configuration class DatabaseAdminConfig { @Bean @ConfigurationProperties(prefix = "spring.ds_admin") fun adminDataSource(): DataSource { return DataSourceBuilder.create().build() } @Bean fun adminJdbcTemplate(@Qualifier("adminDataSource") dsAdminDb: DataSource): JdbcTemplate { return JdbcTemplate(dsAdminDb) } @ConfigurationProperties(prefix = "spring.ds_widget") fun widgetDataSource(): DataSource { return DataSourceBuilder.create().build() } @Bean fun widgetJdbcTemplate(@Qualifier("widgetDataSource") widgetDataSource: DataSource): JdbcTemplate { return JdbcTemplate(widgetDataSource) } }