整合spring、mybatis时使用原生servlet处理请求,用到@Autowired注入属性取不到值问题

整合spring、mybatis时使用原生servlet处理请求,用到@Autowired注入属性取不到值问题

前言

原生servlet使用@Autowired注入属性取不到值,没有使用springmvc的前提下

代码展示

package com.itheima.servlet;
import com.itheima.bean.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Controller
public class userServlet extends HttpServlet {

    @Autowired
    UserService userService;


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(userService);

        String user = req.getParameter("user");
        String password = req.getParameter("password");
        User user1 = new User(user, password);
        System.out.println(userService);
        boolean login = userService.login(user1);
        System.out.println(login);
        if (login) {
            req.getRequestDispatcher("/WEB-INF/views/success.jsp").forward(req, resp);
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败");
            HttpSession session = req.getSession();
            session.setAttribute("msg", "登录失败");
            req.getRequestDispatcher("/index.jsp").forward(req, resp);

        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

在这里插入图片描述

可以看到这里拿到的值为null,这里看到是拿不值以为是配置文件的问题,找了好久,后来发现一开始的思路就错了,其实因为有了springmvc基本上没有人这么写

如果非要这么写其实有两种办法解决
第一种:采用硬编码的方式

ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        userServiceImpl bean = classPathXmlApplicationContext.getBean(userServiceImpl.class);

通过这种方式,获取你想要的对象,并调用方法,其实这个方法只是避免了使用@Autowired获取不到值的问题.

第二种:实现接口方法
先编写一个myServlet 实现Servlet接口,重写方法

package com.itheima.servlet;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import java.io.IOException;

public class myServlet   implements Servlet  {

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

        }


    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}

再在Servlet方法中,重写init方法即可

@Controller
public class userServlet extends HttpServlet {

    @Autowired
    UserService userService;


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(userService);
        String user = req.getParameter("user");
        String password = req.getParameter("password");
        User user1 = new User(user, password);
        System.out.println(userService);
        boolean login = userService.login(user1);
        System.out.println(login);
        if (login) {
            req.getRequestDispatcher("/WEB-INF/views/success.jsp").forward(req, resp);
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败");
            HttpSession session = req.getSession();
            session.setAttribute("msg", "登录失败");
            req.getRequestDispatcher("/index.jsp").forward(req, resp);

        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,config.getServletContext());
    }
}

总结

通过第二种方法,你就可以看到问题迎刃而解了,最后送给大家一句话勉励自己“Talk is cheap,show me the code!”

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值