SSM框架进行单元测试测试Service业务层(No bean named ‘userService‘ available)

成功的单元测试截图:

在这里插入图片描述

问题排查:

1.在业务逻辑层指定:@service(xxxService)注解//交给Spring去创建对象 IOC

在这里插入图片描述

2.在spring配置文件配置包扫描:

<!--添加包扫描 注入bean(Service)-->
    <context:component-scan base-package="springmvc.com.service.Impl"/>

<!--    告知spring注解位置,保证注解有效性,下面注解扫描表示Controller不扫,给service和dao扫描-->
    <context:component-scan base-package="springmvc.com">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
    </context:component-scan>

业务层相关的配置文件:applicationContext_service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">


    <import resource="classpath:applicationContext_mapper.xml"></import>
    <!--添加包扫描 注入bean(Service)-->
    <context:component-scan base-package="springmvc.com.service.Impl"/>

<!--    告知spring注解位置,保证注解有效性,下面注解扫描表示Controller不扫,给service和dao扫描-->
    <context:component-scan base-package="springmvc.com">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
    </context:component-scan>

    <!--添加事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--配置数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--@Transactional 告诉spring,定制事务是基于DataSourceTransactionManager-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    <!--配置事务切面-->
    <tx:advice id="myadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*select*" read-only="true"/>
            <tx:method name="*get*" read-only="true"/>
            <tx:method name="*find*" read-only="true"/>
            <tx:method name="*serach*" read-only="true"/>
            <tx:method name="*insert*" propagation="REQUIRED"/>
            <tx:method name="*add*" propagation="REQUIRED"/>
            <tx:method name="*save*" propagation="REQUIRED"/>
            <tx:method name="*set*" propagation="REQUIRED"/>
            <tx:method name="*update*" propagation="REQUIRED"/>
            <tx:method name="*change*" propagation="REQUIRED"/>
            <tx:method name="*modify*" propagation="REQUIRED"/>
            <tx:method name="*delete*" propagation="REQUIRED"/>
            <tx:method name="*drop*" propagation="REQUIRED"/>
            <tx:method name="*clear*" propagation="REQUIRED"/>
            <tx:method name="*" propagation="SUPPORTS"/>
        </tx:attributes>
    </tx:advice>
    <!--配置切入点+绑定-->

    <aop:config>
        <aop:pointcut id="mycut" expression="execution(* springmvc.com.service.Impl.*.*(..))"/>
        <aop:advisor advice-ref="myadvice" pointcut-ref="mycut"/>
    </aop:config>
</beans>

持久层相关的配置文件:applicationContext_mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd ">
    <!--读取属性文件-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
    </bean>
    <!--配置SqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入连接池-->
        <property name="dataSource" ref="dataSource"/>
        <!--<property name="mapperLocations" value="classpath:springmvc/com/mapper/*Mapper.xml"/>-->
        <!--配置SqlMapperConfig.xml核心配置-->
        <!--使得实体类中用类名作为别名-->
        <property name="typeAliasesPackage" value="springmvc.com.entity"></property>
    </bean>


<!--
    注册mapper.xml文件
-->
   <!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="springmvc.com.mapper"></property>
       <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
   </bean>-->
    <!--Dao 需要 MapperScannerConfigurer支持  配置UserDao接口对应resources文件夹下的com.demo.dao数据包-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="springmvc.com.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
    <!--告知spring注解位置,保证注解有效性,下面注解扫描表示Controller不扫,给service和dao扫描-->
    <context:component-scan base-package="springmvc.com">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
    </context:component-scan>
    <!--引入一个事务管理器-->





</beans>

SpringMVC.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

  <!--添加包扫描-->
    <context:component-scan base-package="springmvc.com.controller"></context:component-scan>
    <!--添加视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <!--配置后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--必须要添加注解驱动,专门用来处理ajax请求的  加了注解驱动@ResponseBody才可以用-->
    <mvc:annotation-driven></mvc:annotation-driven>
<!--解决ajax请求响应数据后页面乱码的代码-->
  <mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
      <bean class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
      </bean>
    </mvc:message-converters>
  </mvc:annotation-driven>

    <!--注册拦截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/login.do"/>
            <mvc:exclude-mapping path="/toLogin.do"/>
            <mvc:exclude-mapping path="/list.do"/>
            <mvc:exclude-mapping path="/LoginByNameAndPassword"/>
            <mvc:exclude-mapping path="/admin/test"/>
            <mvc:exclude-mapping path="/login1.jsp"/>
            <mvc:exclude-mapping path="/login.jsp"/>
            <mvc:exclude-mapping path="/register.html"/>
            <mvc:exclude-mapping path="/addUsers.jsp"/>
            <mvc:exclude-mapping path="/addUsers"/>
            <mvc:exclude-mapping path="/"/>
            <bean class="springmvc.com.Interceptor.LoginInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

  <!--过滤静态资源-->
      <mvc:default-servlet-handler></mvc:default-servlet-handler>
      <mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
      <mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
      <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
      <!--<mvc:resources mapping="/lib/**" location="/lib/"></mvc:resources>
      <mvc:resources mapping="/api/**" location="/api/"></mvc:resources>-->
</beans>

web.xml文件:一定要记得导入SpringMVC.xml文件和spring.xml文件(就是applicationContext_service.xml和applicationContext_mapper.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">


    <!--中文编码过滤器配置-->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!--
            参数配置;
            private String encoding;
            private boolean forceRequestEncoding;
            private boolean forceResponseEncoding;
        -->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--注册SpringMVC  核心控制器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--
            指定拦截什么请求
            http://localhost:8080/one
            http://localhost:8080/one.jsp
            http://localhost:8080/getUsers.do
            <a href="${pageContext.request.contextPath}/getUsers.do">访问服务器</a>
        -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

<!--    注册spring框架,目的就是启动spring容器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext_*.xml</param-value>
    </context-param>
</web-app>

Test测试类:

 @Test
    public void testSelectUserPage1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext_service.xml");
        UserService userService =(UserService) applicationContext.getBean("userService");
        System.out.println("userMapper="+userService);
        List<User> users = userService.selectUserPage(null, null, 15);
        users.forEach(user -> System.out.println(user));
    }

Mybatis配置文件:SqlMybatisConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--注意修改数据库名称-->

    <!--读取jdbc.properties属性文件-->
    <!--  <properties resource="db.properties"></properties>-->
    <!--控制台打印sql输出语句-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>


</configuration>

![(https://img-blog.csdnimg.cn/bdaa8b386a9c4f49b6e3b7e4ee334d81.png)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值