Tag: 春季

当被Spring代理类访问时,Kotlin实例变量为null

我有一个由Spring代理的服务类,如下所示: @Service @Transactional open class MyService { … } 如果我删除了open修饰符,Spring会抱怨它需要代理这个类来应用@Transactional注解调整。 但是,当在代理服务上调用一个函数时会导致问题,该代理服务试图访问一个变量: @Service @Transactional open class MyService { protected val internalVariable = … fun doWork() { internalVariable.execute() // NullPointerException } } internalVariable作为其声明的一部分进行分配,没有任何注释(如@Autowired等),并且当我删除 @Transactional注释和Spring代理类的要求时工作正常 。 为什么当Spring代理/继承我的服务类时,这个变量为null?

Spring Boot:Boot Run不能添加addResources = true

在Kotlin上,在Intellij上使用Gradle来运行Spring Boot的任务bootRun,我得到如下错误: BUILD FAILED Total time: 0.085 secs Could not set unknown property 'addResources' for task ':bootRun' of type org.springframework.boot.gradle.tasks.run.BootRun. 我正在使用kotlinVersion ='1.1.4-3',springBootVersion ='2.0.0.M3'和我的bootrun任务看起来像下面在我的gradle.build bootRun { addResources = true } 我的bootRun按预期工作,没有调整bootRun任务

WebFlux功能:如何检测一个空的通量并返回404?

我有以下简化的处理函数(Spring WebFlux和使用Kotlin的功能性API)。 但是,我需要一个提示如何检测一个空的Flux,然后使用noContent()为404,当Flux为空时。 fun findByLastname(request: ServerRequest): Mono<ServerResponse> { val lastnameOpt = request.queryParam("lastname") val customerFlux = if (lastnameOpt.isPresent) { service.findByLastname(lastnameOpt.get()) } else { service.findAll() } // How can I detect an empty Flux and then invoke noContent() ? return ok().body(customerFlux, Customer::class.java) }

如何在Spek测试中使用依赖注入

我正在使用Kotlin,Spring和Spek实现简单的微服务。 我想测试我的存储库,但我不知道如何注入到测试案例的回购。 每个例子或教程基于创建新的参考,如下所示: object SampleTest : Spek({ describe("a calculator") { val calculator = SampleCalculator() it("should return the result of adding the first number to the second number") { val sum = calculator.sum(2, 4) assertEquals(6, sum) } it("should return the result of subtracting the second number from the first number") { val subtract = calculator.subtract(4, 2) […]

从`.properties`文件中检索值 lateinit属性尚未初始化

我试图创建一个弹簧启动应用程序,我的班级将从文件src/main/resources/application.properties读取。 但由于某种原因,我不能让我的Kotlin使用这些值(返回一个lateinit property url has not been initialized 。 src / main / resources / application.properties (注意,不是在任何地方显式调用的?) spring.datasource.url=someUrl spring.datasource.username=root spring.datasource.password=root spring.datasource.driverClassName=org.postgresql.Driver 科特林 @Component open class BaseDAO() { @Autowired lateinit var datasource: DataSource; } 新的错误 kotlin.UninitializedPropertyAccessException: lateinit property datasource has not been initialized at quintor.rest.persistence.BaseDAO.getDatasource(BaseDAO.kt:18) ~[classes/:na] at quintor.rest.persistence.EventDAO.getMultipleEvents(EventDAO.kt:45) ~[classes/:na] at quintor.rest.persistence.EventDAO.getComingOpenEvents(EventDAO.kt:98) ~[classes/:na] at quintor.rest.persistence.EventService.getComingEvents(EventService.kt:23) ~[classes/:na] at […]

Spring&kotlin:构造函数和lateinit注入有什么区别?

我的应用程序使用spring kotlin时遇到了问题。 一开始我有一个控制器和一个这样的服务: 这是服务: @Service class StuffService { @Inject lateinit var environment : Environment fun doStuff() = [HERE FUNCTION CODE USING environment VARIABLE] } 这里是控制器: @RestController class StuffController { @Inject lateinit var stuffService : StuffService @RequestMapping("/doStuff") fun doStuff() = stuffService.doStuff() } 不幸的是,当我启动springboot时,这给了我这个错误: kotlin.UninitializedPropertyAccessException: lateinit property environment has not been initialized 所以我试图通过构造函数注入: @Service class StuffService(val environment […]

使用Kotlin的Spring Boot 2.0.0.M2应用程序的UnsatisfiedDependencyException

我试图用Kotlin重写JHipster应用程序的DataConfiguration.java实现。 我遇到的问题是: @Bean public SpringLiquibase liquibase(@Qualifier("taskExecutor") TaskExecutor taskExecutor, DataSource dataSource, LiquibaseProperties liquibaseProperties) { // Use liquibase.integration.spring.SpringLiquibase if you don't want Liquibase to start asynchronously SpringLiquibase liquibase = new AsyncSpringLiquibase(taskExecutor, env); liquibase.setDataSource(dataSource); liquibase.setChangeLog("classpath:config/liquibase/master.xml"); liquibase.setContexts(liquibaseProperties.getContexts()); liquibase.setDefaultSchema(liquibaseProperties.getDefaultSchema()); liquibase.setDropFirst(liquibaseProperties.isDropFirst()); if (env.acceptsProfiles(JHipsterConstants.SPRING_PROFILE_NO_LIQUIBASE)) { liquibase.setShouldRun(false); } else { liquibase.setShouldRun(liquibaseProperties.isEnabled()); log.debug("Configuring Liquibase"); } return liquibase; } 我改写为: @Bean fun liquibase(@Qualifier("taskExecutor") taskExecutor: TaskExecutor, […]

如何使用Spring Boot和Spring WebFlux的“功能bean定义Kotlin DSL”?

在https://github.com/spring-projects/spring-framework/blob/master/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt ,注释显示了如何定义Spring Beans通过新的“功能bean定义Kotlin DSL”。 我也发现https://github.com/sdeleuze/spring-kotlin-functional 。 但是,这个例子使用的只是普通的 Spring而不是Spring Boot 。 任何提示如何与Spring Boot一起使用DSL是值得赞赏的。

Kotlin – Mockito不能模拟/间谍(Spring REST API)

由于Kotlin中的所有课程都是默认的最终课程,所以Mockito不能窥探最后的课程: Cannot mock/spy class bye.persistence.jdbcTrial Mockito cannot mock/spy following: – final classes – anonymous classes – primitive types 而这个指南 (7月6日,丹尼·布鲁斯勒(Danny Preussler))说,一个框架是解决这个问题的必要条件。 现在我想知道,是否有可能测试一个REST API(使用Spring MockMvc)。 以下是我的测试代码: package byeTest.persistenceTest import bye.domain.User import bye.persistence.jdbcTrial import bye.spring.GreetingController import byeTest.persistenceTest.RestAPITest.RootConfig import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mockito.BDDMockito.given import org.mockito.Mockito import org.springframework.beans.factory.annotation.Autowired import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.http.MediaType import org.springframework.test.context.ContextConfiguration […]

Kotlin编译器抱怨在属性定义中使用SPeL表达式。 为什么?

当我尝试使用SPeL表达式来注入一个值时,它可以在Java中使用,但不能在Kotlin中使用。 编译器说 错误:(13,25)Kotlin:注释参数必须是编译时常量 码: @SpringBootApplication open class DeDup(@Value("#{new java.io.File('${roots}')}") val roots: Set<File>, @Value("algo") val hashAlgo: String, @Value("types")val fileTypes: List<String>) { } fun main(args: Array<String>) { SpringApplication.run(DeDup::class.java, *args) } 嗯…新闻Flash Kotlin编译器:这是一个常数! 编译器清楚地知道这是一个SPeL表达式,并不喜欢它。 我的问题: Kotlin为什么不喜欢SPeL? 这是一个施工注射(或是否),不违反不变性。 这是一个编译器错误? 这个消息是无可辩驳的错误。