maven集成SSM,提示Failed to load ApplicationContext

每次运行都报的Failed to load ApplicationContext...

因为没有头绪,所以只能把代码都贴上来,麻烦各位大神多费眼,感激不尽

运行环境

工具:Eclipse photon

JDK版本:1.8

Spring版本:5.0.2

Mybatis:3.4.5

整个代码结构如下图:

图片说明

测试类代码片段:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.Leo.Mapper.UserMapper;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class App {
    @Autowired
    private UserMapper usermapper;
    @Test
    public void testSelectAll() throws Exception{
        usermapper.selectAll().forEach(System.out::println);
    }
}

applicationContext.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    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">

    <!-- 核心:1.classpath的根路径去加载db.properties文件 -->
    <context:property-placeholder location="classpath:db.properties" system-properties-mode="NEVER" />

    <!-- 核心:2.配置一个druid的连接池 -->
    <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}"/>
    </bean>


    <!--3. 配置SqlSessionFactory -->
        <bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 1.关联连接池 -->
        <property name="dataSource" ref="myDataSource" />
        <!-- 2.配置mybatis属性 -->
        <!-- 有xml文件 -->
         <!--  <property name="configLocation" value="classpath:MyBatis-config.xml" />-->

        <!-- 不配置mybatis的XML -->
        <property name="configurationProperties">
            <value>
                lazyLoadingEnabled=true
                     <!-- 取消关联查询积极性 -->
                 aggressiveLazyLoading=false
                     <!-- 那些方法出发关系查询 -->
                 lazyLoadTriggerMethods=clone
            </value>
        </property> 
        <!-- 核心:3.配置别名 -->
        <property name="typeAliasesPackage" value="com.leo.domain"/>
        <!-- 核心:4.关联映射文件 -->
        <property name="mapperLocations" value="classpath:Mappers/*Mapper.xml" />
        </bean>
    <!-- 4.配置MapperFactoryBean,让Spring帮我们创建代理对象 -->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="mySqlSessionFactory" />
        <property name="mapperInterface" value="com.leo.Mapper.UserMapper" />
    </bean>
</beans>

Maven的pom.xml配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.leo</groupId>
  <artifactId>ssmDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <!-- 项目编码和依赖框架版本 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>5.0.2.RELEASE</spring.version>
        <mybatis.version>3.4.5</mybatis.version>
    </properties>

    <dependencies>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <!-- mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>

        <!-- MySQL驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
            <scope>compile</scope>
        </dependency>
        <!-- druid连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.26</version>
        </dependency>

        <!-- aspectJ织入 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>

        <!-- JSON处理 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.5</version>
        </dependency>
        <!-- JUnit4测试工具 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>

        <!-- Servlet-API -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>compile</scope>
        </dependency>


        <!-- JSTL标签库 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>


        <!-- 日志 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.21</version>
        </dependency>

        <!-- lombok插件 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.6</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!--Java编译器插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!--Tomcat7插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <uriEncoding>UTF-8</uriEncoding>
                    <path>/</path>
                    <port>80</port>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

3个回答

应该是UserMapper没有注入到applicationcontext,有没有详细的报错提示?applicationcontext.xml加上配置,

<dubbo:reference id=“userMapper" interface=“com.Leo.Mapper.UserMapper”/>

但一般都是配置对外暴露的接口,mapper类不建议以外部接口的方式提供出去,一般封装在其他类的方法中;

louhou_zhui
落后_追 这几天忙着其他作业,重新写了一遍精简了导入的jar包又可以, 不过我猜测应该和你说的一样,估计Mapper的导入的错误
12 个月之前 回复
louhou_zhui
落后_追 谢谢哈
12 个月之前 回复

src/main/webapp/WEB-INF/web.xml
增加

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
louhou_zhui
落后_追 因为原来的类我删除了,没办法尝试,谢谢您的建议哈
12 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐