-
-
Notifications
You must be signed in to change notification settings - Fork 161
JedisPool is null when trying to inject in another class than the controllers #157
Comments
In order for @Inject to work elsewhere, that class need to also participate in the dependency injection tree. Play2 uses Guice internally so this is what you need to do: In application.conf:
create a new class
This way when your Dao class is instantiated by Guice, it'll look through all annotations and inject the Dao's dependencies as needed. If you need to learn more about Dependency Injection in general or Guice in particular, watch Bob Lee's video on Guice |
So I need to create an interface for every DAO class I have and then create a DAOModule where I add a bind for every DAO class? |
Yes. The module is application-wide as far as I know, where all your binding lives. Not just for the DAOs but any other class where you want to use Guice @Inject |
For Play 2.4.2, the current readme causes an exception to be thrown upon setting up @Inject on JedisPool. The correct dependency is play-plugin-redis 2.4.1 see playframework#156 and playframework#157
i tried the solution but still gives me null. |
@mmadian why dont you share some code |
@mmadian The problem is likely that you don't inject the class because then it doesn't matter if you have an @Inject. Here is my working code: UserDAO: public class UserDAO extends AbstractDAO<User, User_> {
JedisPool jedisPool;
@Inject
public UserDAO(final JPAApi jpaApi, JedisPool jedisPool) {
super(jpaApi, User.class, User_.class);
this.jedisPool = jedisPool;
}
/**
* Get the last time the user accessed the service.
*
* @param user the {@link models.User}
* @return the last time as a {@link org.joda.time.DateTime}
*/
public DateTime getLastAccess(User user) {
if(jedisPool == null) {
Logger.error("UserDAO - getLastAccess - Redis is not available");
return null;
}
Jedis j = jedisPool.getResource();
DateTime dateTime = null;
try {
String dtString = j.hget("userLastAccess", user.id.toString());
dateTime = new DateTime(Long.valueOf(dtString));
} catch(NumberFormatException ex) {
dateTime = null;
} finally {
jedisPool.returnResource(j);
}
return dateTime;
}
} And then you load UserDAO like this: UserDAO userDAO = play.Play.application().injector().instanceOf(UserDAO.class); |
I am able to inject the JedisPool in my controllers but not in models, daos or other classes. An example is in my UserDAO. Isn't this supported or why does the exact same code work in the Application controller?
The text was updated successfully, but these errors were encountered: