How to inject an attribute using spring in a type handler?

1k views Asked by At

I'm setting up an application which uses mybatis to map objects to/from the database.

In my mybatis file, I use a typehandler to map one of the objects being sent to the database.

In the typeHandler, I am injecting an attribute using spring @resource.

However, when the typehandler is called, the injected property is always null.

From my research, I have discovered that mybatis sets its configuration before spring loads. That means the bean is cannot be injected into the handler as it is created after.

Does anyone know a solution to this?

1

There are 1 answers

0
Rocky Yu On

Should let spring manage customized type handler, like this:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="typeHandlers">
      <array>
        <bean class="com.example.YourCustomTypeHandler">
          <!-- inject -->
          <property name="property" ref="bean"/>
        </bean>
      </array>
    </property>
</bean>