Tag: spring

Spring代理类和Kotlin中的空指针exception

我和spring一起面临着kotlin的一些问题。 我有一个控制器bean(没有接口btw),它有一个自动连线的服务bean通过主构造函数。 它工作完美,除非我使用控制器缓存注释。 显然弹簧缓存生成一个委托类下的缓存处理。 我的代码如下所示: @RestController @RequestMapping(“/regions/”) open class RegionController @Autowired constructor(val service: RegionService) { @RequestMapping(“{id}”, method = arrayOf(RequestMethod.GET)) @Cacheable(cacheNames = arrayOf(“regions”)) fun get(@PathVariable id: Long): RegionResource { return this.service.get(id) } } 现在的问题是执行该方法时出现空指针exception,实际上this.service为null ,在技术上是不可能的,因为它是kotlin中的非nullvariables。 我假设Spring生成的类代理使用空值而不是autowired bean初始化类。 这一定是使用kotlin和spring的常见错误。 你是怎么回避这个问题的?

如何在Kotlin中使用QueryDSL并生成文件

我使用下面的maven pom.xml文件,不能生成querydsl文件。 我find了一个questrion: Kotlin-Kapt注释处理器与maven不兼容 我想从kotlin实体类生成jpa querydsl文件。 有一个非常好的例子在线如何使用gradle生成dsl文件https://github.com/JetBrains/kotlin-examples/blob/master/gradle/kotlin-querydsl/build.gradle 。 然而,我试图在maven中实现这一点,并没有运气。 我目前的pom在下面。 有谁知道这个问题可能是什么? 提前致谢。 它使用querydsl3,我使用4 4.0.0 cn.techcave.chat jpa 0.0.1-SNAPSHOT jar jpa Kotlin Demo project for Spring Boot JPA org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE true UTF-8 UTF-8 1.8 1.2.10 org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-data-rest org.springframework.data spring-data-rest-hal-browser org.jetbrains.kotlin kotlin-stdlib-jdk8 ${kotlin.version} org.jetbrains.kotlin kotlin-reflect ${kotlin.version} mysql mysql-connector-java runtime io.springfox springfox-swagger-ui 2.8.0 io.springfox springfox-swagger2 2.8.0 […]

用Kotlin注释泉水

Kotlin不能在编译时注入注释,如现有的库Lombok 。 有没有什么体面的方式在运行时注入Spring框架?

Spring Boot + Kotlin AutoProxyRegistrar导致Nullpointerexception

Hej大家,我正在尝试从java 8迁移一个小的spring引导项目到kotlin。 我碰到一个问题,我有以下配置类 @EnableCaching @Configuration open class CacheConfiguration : CachingConfigurer { @Bean override fun cacheManager(): CacheManager { return ConcurrentMapCacheManager() } @Bean override fun cacheResolver(): CacheResolver { return SimpleCacheResolver(cacheManager()) } /** * Simple Key Generator * @return not null */ @Bean override fun keyGenerator(): KeyGenerator { return SimpleKeyGenerator() } @Bean override fun errorHandler(): CacheErrorHandler { return […]

在Grails中可以使用Kotlin吗?

一些基本的事实导致我这个问题: Groovy具有完整的Java互操作性 Kotlin具有完整的Java互操作性 Kotlin也编译到Java 因此可以在Grails应用程序中编写Kotlin代码吗? 我在Grails 2.x上做了很多工作,最近在和Kotlin,Spring和Struts一起工作。 我真的很喜欢Kotlin的null-safety和type推理function,而Kotlin的function编程特性比Groovy更自然,更易于使用(最后一部分是纯粹的意见)。 是否有可能使用Grails来处理这样的事情: ORM 将请求映射到控制器/操作 JSP / GSP视图解析/呈现 而是使用Kotlin来编写域类,控制器动作,服务,对象工厂等的实际逻辑。 可能不太可能,因为我猜测Grails的一些核心function是通过动态输入实现的,但也许可以通过Gradle插件或直接的Grails插件来实现。 我喜欢Grails的convention-over-configuration范例提供的极端简单,但是我更喜欢Kotlin的静态types和types推断。 如果我可以在Grails环境中编写Kotlin的业务逻辑,对我来说,这将是最终的Web应用程序框架!

@Param不能在Spring Data JPA中工作

我正在建立一个Spring Data JPA Repo来处理postgresql数据库中的序列。 我假设这将是非常简单的: @Query(nativeQuery = true, value = "CREATE SEQUENCE IF NOT EXISTS ':seq_name' START WITH :startAt") fun createSequence(@Param("seq_name") seq_name: String, @Param("startAt") startAt: Long = 0) @Query(nativeQuery = true, value = "SELECT nextval(':seq_name')") fun nextSerial(@Param("seq_name") seq_name: String) : Long @Query(nativeQuery = true, value = "DROP SEQUENCE IF EXISTS ':seq_name'") fun dropSequence(@Param("seq_name") seq_name: String) […]

Spring数据JPA中的动态查询

我正在寻找一个解决方案来动态构建使用Spring Data JPA的查询。 我有一个GameController,它有一个REST风格的服务端点/游戏,它需要4个可选参数:流派,平台,年份,标题。 这些API可能不会被传递,全部4个,以及它们之间的每个组合。 如果有任何参数未被传递,则默认为null。 我需要一个在仓库中的方法,将建立适当的查询,理想情况下,仍然允许Spring Data JPA分页,但我不知道这是可能的。 我发现这篇文章,但这似乎并不是我所需要的,除非我是误解。 http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/ 我知道JPA有一个查询标准的API,但真的不知道如何实现这一点。 我意识到我可以为每个可能的场景创建一个方法,但这似乎是非常糟糕的做法和大量不必要的代码。 GameRepository: package net.jkratz.igdb.repository; import net.jkratz.igdb.model.Game; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; public interface GameRepository extends JpaRepository<Game, Long> { @Query("select g from Game g, GamePlatformMap gpm, Platform p where g = gpm.game and gpm.platform = p and p.id = […]

春天懒加载嵌套事务内部不工作

我用kotlin。 这是我的实体类: @Entity class Category( @Id @GeneratedValue(strategy = IDENTITY) val id: Int = 0, @NotBlank @Column(unique = true) var name: String, @OneToMany(mappedBy = "category") val subcategories: MutableSet<Subcategory> = mutableSetOf() ) @Entity class Subcategory( @Id @GeneratedValue(strategy = IDENTITY) val id: Int = 0, @NotBlank var name: String, @ManyToOne val category: Category ) 服务方式: @Transactional fun addSubcategory(model: […]

逐渐填充InputStream并用SpringMVC返回

考虑我在数据库中有大量的csv字符串,我需要通过他的请求传递给客户端。 一个天真的方式来做到这一点: @RequestMapping(value = "/{user}/ejournal", method = RequestMethod.GET, produces = "text/csv") public ResponseEntity<ByteArrayResource> getEJournal(@PathVariable Long userId) { final byte[] documentBody = EJournal .findByUser(userId) .stream() .collect(Collectors.joining("\n")) .getBytes(); return new ResponseEntity( new ByteArrayResource(documentBody), HttpStatus.OK); } 从客户端直接(kotlin代码示例): val inputStream = eJournalService.getJournal(userId) inputStream.reader().forEachLine { line -> … } 但是我不得不把所有的数据加载到内存中,然后传递给流,显然效率不高。 所以我需要以某种方式缓冲它使用自定义阅读器分页读取数据和发送缓冲区到客户端。 我想实现我自己的InputStream但InputStream#read()返回int而不是String ,这有点复杂。 任何最佳实践来做到这一点? 我试图搜索,但只有例子传递一个图片使用已经在内存中的流,而不是缓冲分页后批次发送顺序数据库查询。 提前致谢。

动态加载spring xml配置

在一个春天的应用程序的启动时,我想扫描计算机上的路径,找到jar文件,并从他们的XML配置文件构建一个春天的应用程序上下文。 将jar文件添加到类路径并创建一个ApplicationContext都是可以的。 但是我从新的上下文中找不到任何bean。 所有需要的依赖关系都可以在计算机上的特定路径(通过maven复制插件)的jar文件中获得,这些依赖项在基本的spring项目(例如spring依赖项本身)中是需要的。 代码是(在Kotlin语言中): var loader = URLClassLoader(arrayOf(entry.toFile().toURL()), Thread.currentThread().contextClassLoader) … val context = ClassPathXmlApplicationContext("classpath*:/$name")//name is xml file. I'm sure the address in classpath is right. context is not creating when the address in wrong. for example: classpath://$name val services = context.getBeanNamesForType(IService::class.java)//services is empty 我已经尝试了许多其他的方式来加载XML,但没有一个是成功的。 例如: val beans = DefaultListableBeanFactory(applicationContext) val reader = XmlBeanDefinitionReader(beans) reader.beanClassLoader […]