Tag: 弹簧

在运行时生成的类中使用Kotlin对象

我正在使用ByteBuddy重新绑定另一个库的类,以便向其添加Spring依赖注入。 问题是我不能实例化用作拦截器的类,这意味着我不能使用Spring将ApplicationContext注入到拦截器中。 为了解决这个问题,我创建了一个对象StaticAppContext ,它通过实现ApplicationContextAware来获得注入的ApplicationContextAware : @Component object StaticAppContext : ApplicationContextAware { private val LOGGER = getLogger(StaticAppContext::class) @Volatile @JvmStatic lateinit var context: ApplicationContext override fun setApplicationContext(applicationContext: ApplicationContext?) { context = applicationContext!! LOGGER.info("ApplicationContext injected") } } 这是得到注入就好(我可以看到日志消息),但是当我尝试从拦截器访问ApplicationContext ,我得到kotlin.UninitializedPropertyAccessException: lateinit property context has not been initialized 。 在这个类中定义了重定义类和incerceptor的类: package nu.peg.discord.d4j import net.bytebuddy.ByteBuddy import net.bytebuddy.dynamic.ClassFileLocator import net.bytebuddy.dynamic.loading.ClassLoadingStrategy import net.bytebuddy.implementation.MethodDelegation […]

异步春季启动使用Kotlin无法正常工作

我正在尝试创建一个异步执行操作的Spring服务,并返回一个ListenableFuture 。 我希望在操作失败时触发失败回调 – 我尝试这样做是使用AsyncResult.forExecutionException ,如下所示: @Service open class UserClientService { @Async fun fetchUser(email: String): ListenableFuture<User> { val uri = buildUri(email) val headers = buildHeaders() try { val result = restTemplate.exchange(uri, HttpMethod.GET, HttpEntity<Any>(headers), User::class.java) return AsyncResult.forValue(result.body) } catch (e: RestClientException) { return AsyncResult.forExecutionException(e) } } } 入门点: @SpringBootApplication @EnableAsync open class UserProxyApplication fun main(args: Array<String>) […]

如何在Kotlin类中注入JOOQ的DSLcontext

我用Kotlin,Spring和PostgreSQL使用JOOQ。 我能够运行JOOQ生成器和查询数据。 问题是我无法在资源库类的构造函数中注入DSLcontext。 它抛出以下异常: dslContext中的字段dslContext需要一个无法找到的类型为org.jooq.DSLContext的bean。 注射看起来像这样: @Repository class SomeRepositoryImpl(private val dslContext: DSLContext): SomeRepository { } 干杯,翁德里

IntelliJ Idea for Kotlin @ConfigurationProperties类中不生成spring-configuration-metadata.json文件

我试图为我的基于Spring Boot的项目生成spring-configuration-metadata.json文件。 如果我使用Java @ConfigurationProperties类,它会自动正确生成: @ConfigurationProperties("myprops") public class MyProps { private String hello; public String getHello() { return hello; } public void setHello(String hello) { this.hello = hello; } } 但是,如果我使用Kotlin类,则不会生成spring-configuration-metadata.json文件(我已经尝试了gradle build和Idea Rebuild Project )。 @ConfigurationProperties("myprops") class MyProps { var hello: String? = null } AFAIK Kotlin与构造函数,getter和setter生成相同的类,并且应该像普通的Java bean一样。 任何想法为什么春季启动配置处理器不适用于Kotlin类?

使用Spring HATEOAS ControllerLinkBuilder和Kotlin抛出IllegalArgumentException

我正在使用Kotlin制作一个小型的HATEOAS春季启动应用程序。 我想使用ControllerLinkBuilder从处理它们的类自动创建我的HAL响应链接,如下所示: user.add(linkTo(methodOn(UserController::class.java).findByName(userName)).withSelfRel()) 但是这抛出: java.lang.IllegalArgumentException: Cannot subclass final class UserController 因为Kotlin中的所有类都默认是最终的。 我不喜欢(而且也不想养成这样的习惯)为了克服这个限制而open我所有的课程。 我也不是真的想手动建立我的链接,因为他们可能会改变(和ControllerLinkBuilder是非常方便的填充参数等 目前我想我只是要接受我的控制器类必须打开。 有没有其他的方法呢?

Kotlin协程和Spring Framework 5反应类型

Kotlin协程允许通过返回Deferred值来执行非阻塞代码。 这对于在使用阻塞方法(例如从库)中生成非阻塞代码非常有用。 Spring 5允许在框架中使用Mono和Flux 。 我所看到的最大兴趣是能够序列化这两种类型的实例,并在有人呼叫控制器端点时作为响应发回。 Spring 5的一个重要特点是对Kotlin(路由器,bean声明,…)有特定的支持,但是我找不到有关Kotlin协同程序和Spring 5反应类型之间可能的相互作用的信息。 有什么办法可以将这些功能的优点结合起来吗? 将Deferred转换为Mono / Flux ? 有一个Deferred作为响应类型的Spring控制器方法? 如果否,那么在这种情况下,如果我们有Spring 5反应类型,那么协程是否有意义呢?

为springmvc使用kotlin无法实例化bean类的dataclass

我使用kotlin来ceate简单ArticlesService,我创建一个文章dataclass data class Articles(var artid: Int, var artTitle: String, var artContent: String, var artAut: String, var artTime: Date) 但类无法实例化bean类:找不到默认的构造函数; 嵌套异常是java.lang.NoSuchMethodException:com.zxl.blog.server.Articles。() @Controller class mainServer() { @Autowired val artSer: ArticlesService? = null @RequestMapping("/i") fun fuwuqi(name: String, model: ModelMap): String { model.put("name", name) return "i" } @RequestMapping(value = "/saveArt", method = arrayOf(RequestMethod.POST)) fun saveArt(art: Articles): String { return […]

从Java 6迁移到Kotlin

我有一些在Java 6和Spring应用服务器上的项目。 由于我的基础架构和治理规划的限制,我无法迁移到较新的Java 7或8。 我想我可以在这些项目中添加Kotlin,以使用功能性编程等功能。 这是一个很好的途径吗? 谢谢

在Kotlin中使用@Service时引发异常

我在Kotlin编写了一个SSM项目,只要我尝试用@Service注释一个类(一个服务实现),就会发生这种情况。 试图用Java来编写这个实现类,它工作得很好。 试图只保留必要的压倒一切的方法。 open已经添加。 context:component-scan配置context:component-scan 。 java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:107) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) […]

Spring Boot Kotlin Jersey ModelValidationException

我正在尝试让Spring Boot + Jersey与Kotlin合作。 我配置我通常会在Java中的方式,但获得以下堆栈跟踪。 它似乎表明控制器正在实例化两次,但它不应该。 这是我的JerseyConfig类: package com.joescodeshack.igdb.config import com.fasterxml.jackson.module.kotlin.KotlinModule import com.joescodeshack.igdb.controller.GenreController import org.glassfish.jersey.server.ResourceConfig import org.springframework.context.annotation.Bean import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder import org.springframework.stereotype.Component import javax.ws.rs.ApplicationPath @Component @ApplicationPath("/api") open class JerseyConfig : ResourceConfig { constructor() { register(GenreController()); } @Bean open fun objectMapperBuilder(): Jackson2ObjectMapperBuilder = Jackson2ObjectMapperBuilder().modulesToInstall(KotlinModule()) } GenreController(JAX-RS端点) package com.joescodeshack.igdb.controller import com.joescodeshack.igdb.domain.Genre import javax.ws.rs.GET import javax.ws.rs.Path import javax.ws.rs.Produces import […]