spring中注解不起作用

我在一个servlet中使用spring注解获取dao对象,一直报空指针
使用ClassPathXmlApplicationContext()则可以直接获取对象

下面是目录结构以及相关的截图
图片说明

图片说明
图片说明

![![图片说明](https://img-ask.csdn.net/upload/201703/09/1489074773_76333.png)图片说明](https://img-ask.csdn.net/upload/201703/09/1489074767_908364.png)图片说明

http://blog.csdn.net/wwd0501/article/details/48442383

调用这个myDao的类是service吗?
请参考下下面代码。这样myDao是能取到,不是空指针。

@Service
public class ************* {

@Autowired
    MyDao myDao;

}

這個鏈接裏介紹了從servlet取得spring bean。https://kodejava.org/how-do-i-get-a-springs-bean-from-a-servlet/

鏈接裏面的代碼。

package org.kodejava.example.servlet;

import org.kodejava.example.servlet.dao.UserDao;
import org.kodejava.example.servlet.model.User;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class SpringBeanServletExample extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        ServletContext context = getServletContext();
        WebApplicationContext ctx =
                WebApplicationContextUtils
                        .getWebApplicationContext(context);
        UserDao dao = ctx.getBean("userDao", UserDao.class);

        Long userId = Long.valueOf(req.getParameter("user_id"));
        User user = dao.getUser(userId);

        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        pw.print("User Details: " + user.toString());
        pw.flush();
    }
}