Mentawai has a complete and powerful Inversion of Control (IoC) container fully integrated with the framework. Therefore to use the best IoC and Dependency Injection (DI) practices you need not use any other framework. Mentawai supports injection by setter and constructor, smart auto-wiring and more. Let's see how easy this is:
Setting up the application manager:
@Override public void loadFilters() { filter(new MentaContainerFilter()); }
NOTE: The IoC, DI and Auto-wiring capabilities of Mentawai were extracted in a separate project called MentaContainer, which remains seamlessly integrated with the framework.
Setting up IoC:
@Override public void setupIoC() { ioc(UserDAO.class, JdbcUserDAO.class); // for my UserDAO interface, I want to use the JdbcUserDAO implementation }
Performing DI:
Now that you have indicated the implementation (JdbcUserDAO) you want to use in your web application, the dependency injection will happen automatically and seamlessly. You need not worry about anything. For example:
public class UserAction extends BaseAction { private final UserDAO userDAO; public UserAction(UserDAO userDAO) { // <==== Constructor DI this.userDAO = userDAO; } // (...) }
It also supports setter DI if you prefer:
public class UserAction extends BaseAction { private UserDAO userDAO; public UserAction() { } public void setUserDAO(UserDAO userDAO) { // <===== Setter DI this.userDAO = userDAO; } // (...) }
Auto-wiring:
Wiring of dependencies will also happen automatically and seamlessly. Again you need not worry about anything. Here is an example:
@Override public void setupIoC() { ioc(BeanSession.class, MySQLBeanSession.class); ioc(UserDAO.class, JdbcUserDAO.class); }
As you can see below, your JdbcUserDAO depends on the BeanSession:
public class JdbcUserDAO implements UserDAO { private final BeanSession session; private final Connection conn; public JdbcUserDAO(BeanSession session) { this.session = session; this.conn = session.getConnection(); } // (...) }
Mentawai will automatically detect this dependency and wire the objects together, in other words, it will pass the BeanSession implementation you specified (MySQLBeanSession) to the JdbcUserDAO's constructor, without you having to do anything.
TIP: Inversion of Control also makes it easier to test your objects with unit or functional testing.