依赖注入创建实例

时间:2013-11-24 23:59:27

标签: java dependency-injection

让我们上课

public class MessageB implements Message{
    private String msg;
    public MessageB(){ };
    public MessageB(String msg){
        this.msg=msg;
    }

    @Override
    public String get(){
        return "Message B";
    }
}

和HttpServlet

public class NewServlet extends HttpServlet{
    @Inject private MessageB message;

    public void doGet(HttpServlet request, HttpServlet response) throws IOException{
        response.getWriter().write(message.get());
    } 
}

在官方教程中说

CDI runtime looks for classes that implements the Message inteface, find the MessageB

class,创建它的新实例,并在运行时将其注入servlet。

在运行时我们如何知道必须调用什么构造函数?

0 个答案:

没有答案