tokuhirom's blog

mutable なクラスがどのぐらい DI 対象になってるか調べたい

BeanPostProcessor で探せる。

import org.springframework.beans.factory.config.BeanPostProcessor
import org.springframework.context.annotation.Profile
import org.springframework.stereotype.Component
import java.lang.reflect.Modifier

@Component
@Profile("!real")
class MyProcessor : BeanPostProcessor {
    override fun postProcessAfterInitialization(bean: Any, beanName: String): Any? {
        val fields = bean.javaClass.declaredFields
        val packageName = bean.javaClass.packageName
        if ((!packageName.startsWith("org.springframework"))
            && fields.filter { !Modifier.isFinal(it.modifiers) }.count() > 0) {
            println("$bean has mutable field.")
        }
        return bean;
    }
}
Created: 2019-12-10 15:57:28 +0900
Updated: 2019-12-10 15:57:28 +0900