专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

spring整合(Junit、web)

1、整合Junit

(1)整合前的测试类代码

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new
                ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService =(AccountService) applicationContext.getBean("accountService");
        accountService.transfer("zhai","zhang",10);
    }
}

需要先加载配置文件,获得spring容器,然后从容器中获得对象即可调用相应的类中的方法。

(2)整合后的代码:

需要先导入jar包:

基础包:4+1

测试包:

122_1.png

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Test {
    @Autowired//与JUnit整合,不需要在spring的xml配置文件中配置扫描
    private AccountService accountService;
    public static void main(String[] args) {

        ApplicationContext applicationContext=new
                ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService =(AccountService) applicationContext.getBean("accountService");
        accountService.transfer("zhai","zhang",10);
    }
}

加载配置文件:

@ContextConfiguration(locations = "classpath:applicationContext.xml")

classpath 的作用是告诉我们配置文件的位置是src目录下。

2、整合web

(1)导入jar包:

122_2.png

(2)tomcat启动时加载配置文件的方式:

servlet init(ServletConfig)

filter init(FilterConfig) web.xml注册过滤器自动调用初始化

listener ServletContextListenter ServletContext 对象监听(spring运用的是这个)

spring提供监听器 ContextLoaderListener web.xml

(3)对web.xml文件进行配置(加载配置文件):

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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/web-app_4_0.xsd"
         version="4.0">
    <!--确定配置文件的位置,默认情况在WEB-INF目录下-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!--配置spring监听器,加载配置文件-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

(4)从servletContext作用域获得spring容器

public class TestServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        //获得spring容器,手动从applicationContext作用域获取
        ApplicationContext applicationContext=
                (ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        //通过工具获取
        ApplicationContext applicationContext1=
                WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        AccountService accountService =(AccountService) applicationContext.getBean("accountService");
        accountService.transfer("zhai","zhang",10);
    }
}

获取ApplicationContext 的对象有两种方式。

书写一个页面点击后访问servlet:

<%--
  Created by IntelliJ IDEA.
  User: zhai
  Date: 2020/4/17/0017
  Time: 10:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <a href="${pageContext.request.contextPath}/test">获得spring容器</a>
  </body>
</html>
未经允许不得转载:搜云库技术团队 » spring整合(Junit、web)

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们