-2

Im tryin to inject a interface with an implementation but it is always null.

I use the dependency

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>

And this is the code

LoginServlet class

@Named
@RequestScoped
@WebServlet(urlPatterns = "/login.do")
public class LoginServlet extends HttpServlet {

    @Inject
    UserDao userDao;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = userDao.findByUsername("USERNAME");

        PrintWriter out = response.getWriter();
        out.println(user.getName());
    }
}

UserDao interface

public interface UserDao {

    User findByUsername(String username);
}

UserDaoImpl class

public class UserDaoImpl implements UserDao {

    @Override
    public User findByUsername(String username) {
        User user = new User();
        user.setName("HEEEY");
        return user;
    }
}

And the user class

public class User {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

I do not get this to work.. and do not understand why.

aurelius
  • 3,946
  • 7
  • 40
  • 73
Jeroen
  • 180
  • 1
  • 2
  • 11

4 Answers4

0

Try this:

@Inject
UserDaoImpl userDaoImpl;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    User user = userDaoImpl.findByUsername("USERNAME");

    PrintWriter out = response.getWriter();
    out.println(user.getName());
}

I think, this should work.

0

Add @RequestScoped annotation on UserDaoImpl class

@RequestScoped
public class UserDaoImpl implements UserDao {

    @Override
    public User findByUsername(String username) {
        User user = new User();
        user.setName("HEEEY");
        return user;
    }
}
aurelius
  • 3,946
  • 7
  • 40
  • 73
0

Either you are missing a beans.xml or you have no cdi library available. (Tomcat should have it so it is likely a missing beans.xml)

An empty file named beans.xml in the locations described by:

Where should beans.xml be placed?

is enough.

Community
  • 1
  • 1
k5_
  • 5,450
  • 2
  • 19
  • 27
  • I'm not using using the spring framework.. but is this also required for javax injections? – Jeroen Jun 17 '16 at 11:45
  • Depends on version of Tomcat, but any recent version should have it. Did adding a beans.xml change the behavior? – k5_ Jun 17 '16 at 14:12
  • I dit create the `beans.xml` in my WEB-INF but it still is not working.. Do i need to add something in pom.xml file to build it? This is what i have in my beans.xml: ` ` – Jeroen Jun 17 '16 at 15:17
  • depending on how you are building with maven it sould be in src/main/webapp/WEB-INF or src/main/resources/META-INF. – k5_ Jun 17 '16 at 17:46
0

It seems that your beans are not scanned by CDI scanner. Add this empty beans.xml file under src/main/resources/META_INF

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                           http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="all">

</beans>

this will activate the scanning.

aurelius
  • 3,946
  • 7
  • 40
  • 73