简体   繁体   English

如何在类型处理程序中使用spring注入属性?

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

I'm setting up an application which uses mybatis to map objects to/from the database. 我正在设置一个使用mybatis将对象映射到数据库或从数据库映射对象的应用程序。

In my mybatis file, I use a typehandler to map one of the objects being sent to the database. typehandler文件中,我使用typehandler来映射要发送到数据库的对象之一。

In the typeHandler, I am injecting an attribute using spring @resource . 在typeHandler中,我使用spring @resource注入属性。

However, when the typehandler is called, the injected property is always null . 然而,当typehandler被调用时,注入的属性始终是null

From my research, I have discovered that mybatis sets its configuration before spring loads. 从我的研究中,我发现mybatis在弹簧加载之前设置其配置。 That means the bean is cannot be injected into the handler as it is created after. 这意味着在创建之后无法将Bean注入到处理程序中。

Does anyone know a solution to this? 有人知道解决方案吗?

Should let spring manage customized type handler, like this: 应该让spring管理定制的类型处理程序,像这样:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM