Using environment properties in TypeHandler: BaseTypeHandler#configuration

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Using environment properties in TypeHandler: BaseTypeHandler#configuration


I came across a use case where environment variables are required for type conversion in TypeHandler. Basically I want to modify data retrieved from DB differently, depending on what environment my application is running in.

I noticed that BaseTypeHandler already has Configuration property with protected scope, however it seems to be never set by MyBatis initialization code. Is there a reason for that? Why it was added in first place?

Currently as a workaround I'm manually injecting *existing* Configuration object from SqlSessionFactory to all TypeHandler's available from TypeHanderRegistry that inherit BaseTypeHandler during app startup, but it feels like something that should be done by framework. Are there any pitfalls I'm missing?


Code snippet:

for (TypeHandler handler : sqlSessionFactory.getConfiguration().getTypeHandlerRegistry().getTypeHandlers()) {
        if (handler instanceof BaseTypeHandler) {
                ((BaseTypeHandler) handler).setConfiguration(sqlSessionFactory.getConfiguration());