SSM之Spring web模块

AutoWired与Qualifier

  • @AutoWired:Spring会自动创建实现类对象,并且将实现类对象赋值给dao,如果项目中该接口只有一个实现类,可以使用AutoWired;但是,接口有两个或者两个以上的实现类,此时就会抛异常
  • @Qualifier:可以手动指定将哪一个实现类的对象赋值给IPersonDao接口
  • 两个必须合在一起使用
@Repository
public class PersonDaoImpl implements IPersonDao {
    
    
    public boolean findByUserNameAndPassword(Person person) {
    
    
        return true;
    }
}

@Repository
public class StudentDaoImpl implements IPersonDao {
    
    
    public boolean findByUserNameAndPassword(Person person) {
    
    
        return true;
    }
}
//类跟接口是实现关系
@Service
public class PersonServiceImpl implements IPersonService {
    
    

    //private IPersonDao dao = new PersonDaoImpl();
    @Autowired
    @Qualifier("personDaoImpl")
    private IPersonDao dao;

Spring和web结合

  • 导入依赖
 <!--servlet相关的3个jar包-->
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.2.1</version>
            <scope>provided</scope>
        </dependency>
  • LoginServlet
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    
    
    private static final Logger logger = LoggerFactory.getLogger(LoginServlet.class);
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
            doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        //获取账号密码
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        logger.debug(username);
        logger.debug(password);
        //调用登录方法
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        IPersonService personService = (IPersonService) context.getBean("personServiceImpl");
        Person person = new Person(username,password);

        boolean flag = personService.login(person);
        logger.debug(flag+" doGet ");
        //结果
        response.getWriter().println(flag?"success":"error");
    }
}

代码优化一

PersonServiceImpl service = (PersonServiceImpl)context.getBean("PersonServiceImppl");

代码优化二

  • ServletContext有什么用
    支持多个Servlet共享数据且整个项目只有一个实例

ContextLoaderListener介绍

  • ContextLoaderListener是什么
    Spring编写了一个监听器,该监听器监听ServletContext对象的创建
  • 作用:
    会自动读取contextConfigLocation配置,并创建IOC容器,将IOC容器保存在ServletContext中
  • 如何使用
    依赖spring-web
    在web.xml中配置监听器
    在这里插入图片描述

pom.xml

 		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

web.xml

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <listener>
    <!--
      监听ServletContext对象创建,然后将spring容器放到该ServletContext中缓存起来
    -->
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

LoginServlet

// ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(p.getProperty("config"));
        //1:从ServletContext域对象获取Spring容器
        WebApplicationContext context = (WebApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

猜你喜欢

转载自blog.csdn.net/xinxin_____/article/details/109032650