Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dependency injection in custom type handlers #493

Open
TranNgocKhoa opened this issue Jul 10, 2020 · 2 comments
Open

Support dependency injection in custom type handlers #493

TranNgocKhoa opened this issue Jul 10, 2020 · 2 comments

Comments

@TranNgocKhoa
Copy link

I know there is already an issue at #218 and solution is:

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

But I think bellow is better because now is 2020 and we should use Java Configuration.

public class JsonObjectArrayHandler extends BaseTypeHandler<List<Gallery>> {
    private final ObjectMapper objectMapper;

    public JsonObjectArrayHandler(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

And then in application.yml of Spring Boot application:

mybatis:
  type-handlers-package: com.example.handler

Please add support for this.
Thank you so much!

@suraj092kumar
Copy link

It throws error on startup It keeps asking for default constructor.

@superxiao
Copy link

superxiao commented Sep 11, 2024

I have successfully injected a Spring dependency into custom type handler, using @EnableSpringConfigured (on an @Configuration class) and @Configurable (on the type handler class) from org.springframework:spring-aspects. You would also need to @Autowired a field or a setter inside the type handler.

See Using AspectJ to Dependency Inject Domain Objects with Spring

Edit: AspectJ weaving is a prerequisite for this to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants