Tag: aop

与Kotlin的AOP

我用Java很多AOP。 看起来像传统的java方法可以很容易地与Kotlin重用。 鉴于Kotlin强调不变性JDK代理似乎是Kotlin中最可行的解决方案,前提是您遵循相同的接口(首先是Kotlin中的特征优先)编程风格,例如: trait MyService { fun add(a: Int, b: Int): Int } class MyServiceImpl: MyService {…} 所以现在可以很容易地写一个方面,例如Spring / AOP,并将其应用到MyServiceImpl的实例。 应该提到的是,基于Java接口的生成代理可能更受Kotlin开发人员青睐,因为cglib需要摆脱最后(即在Kotlin中求助于开放类),并且为每个应该由AOP代理包装的类使用无参数公共构造函数。 与此同时,基于Java接口的生成代理不幸地带来了显着的性能损失,所以我想知道在某些情况下,AOP编程是否可以更直观地或本地地完成到Kotlin。 所以,考虑下面的例子,当我想使用AOP的时候: 记录从服务中暴露的每个方法的参数和返回结果。 在函数调用之前启动事务并在函数调用完成后关闭它(提交/回滚,取决于在进行特定的服务调用时是否抛出异常)。 上述MyService / MyServiceImpl示例中最为有效但不幸的是最详细的蛮力解决方案可能如下所示: class MyServiceLoggingWrapper(val delegate: MyService): MyService { val logger = LoggerFactory.getLogger(this.javaClass) override fun add(a: Int, b: Int): Int { val result = delegate.add(a, b); logger.info("MyService.add({}, {}) = {}", […]

使用Aspect将响应标题添加到所有控制器

我希望我所有的Spring MVC控制器都包含一些常见的响应头文件。 我可以使用以下(Kotlin)做到这一点: @ModelAttribute open fun responseHeaders(response: HttpServletResponse) { //identify the node that serviced the call response.setHeader("host-name", InetAddress.getLocalHost().hostName); //etc } 。 。 因为它适用于所有,我可以使用基类。 但是,有没有建议的方式来使用一个方面?

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 […]

Aspectj不能和kotlin一起工作

我想在kotlin中使用aspectj aop,这里是我的代码: 我在annotation.lazy_list中的注释: 科特林: package anotation @Retention(AnnotationRetention.RUNTIME) @Target(AnnotationTarget.FUNCTION) annotation class lazy_list 我的课程: @Aspect class ActiveListAop{ @Pointcut("execution(@annotation.lazy_list * *(..))") fun profile() { } @Before("profile()") fun testModeOnly(joinPoint: JoinPoint) { println("123") } } 我的用法: @lazy_list fun all():List<T>{ return lazy_obj?.all() as List<T> } 当我打电话all()函数,没有错误,但不会打印“123”,为什么?