IDEA: spring+mybatis+springMVC SSM框架(一) 搭建spring环境

首先创建个maven项目
IDEA: spring+mybatis+springMVC SSM框架(一) 搭建spring环境
给项目命名

IDEA: spring+mybatis+springMVC SSM框架(一) 搭建spring环境

修改pom文件 添加对spring的依赖

<!--添加spring依赖-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.20.RELEASE</version>
</dependency>

在src目录下新建java、resources 并在module中配置java为source,resources 为resources
IDEA: spring+mybatis+springMVC SSM框架(一) 搭建spring环境

创建一个类作为测试用
IDEA: spring+mybatis+springMVC SSM框架(一) 搭建spring环境

通过注解让spring来替我们加载这个类,@Configuration 相当于在spring文件中创建一个<bean>
IDEA: spring+mybatis+springMVC SSM框架(一) 搭建spring环境
创建一个配置文件名为:ApplicationContext.xml 来配置spring

<?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">

	<!-- 扫描包去发现spring的注解 来替我们注册这个bean -->
    <context:component-scan base-package="com.ssm"></context:component-scan>

</beans>

先配置test类的环境
IDEA: spring+mybatis+springMVC SSM框架(一) 搭建spring环境

然后创建个test类进行测试spring能否帮我们注册这个TestBean类

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestGetBean {

    @Test
    public void testGetBean() {
    	//加载classpath:/ApplicationContext.xml 获取 ApplicationContext对象 applicationContext
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/ApplicationContext.xml");
        //从applicationContext中提取testBean
        System.out.println(applicationContext.getBean("testBean")); //不为null 说明ApplicationContext.xml 的context:component-scan 扫描到了 TestBean的注解 为null 查看context:component-scan 扫描的包是否是TestBean的父级包
    }
}

测试没问题,我们就通过配置web.xml来让spring在web容器加载时来装载

在pom文件中添加spring对web容器的依赖

<!-- https://mvnrepository.com/artifact/org.springframework/org.springframework.web -->
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>org.springframework.web</artifactId>
   <version>3.1.4.RELEASE</version>
</dependency>

然后再web.xml中 配置 ContextLoaderListener 监听器,来装配spring相关配置

<context-param>
<param-name>contextConfigLocation</param-name>
  <!--相对于resources的路径-->
  <param-value>classpath:ApplicationContext.xml</param-value>
</context-param>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

启动项目时会报错CGLIB is required to process @Configuration classes
原因:Spring3中 使用 @Configuration 需要添加CGLIB的依赖
在pom文件中添加 CGLIB的依赖

<!--CGLIB is required to process @Configuration classes-->
<dependency>
  <groupId>cglib</groupId>
  <artifactId>cglib</artifactId>
  <version>2.2.2</version>
</dependency>

添加依赖后启动,成功启动
IDEA: spring+mybatis+springMVC SSM框架(一) 搭建spring环境

org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization completed in 1106 ms 代表spring已经加载完毕

然后我们配置下log4j进行日志输出
首先在resources目录下添加文件log4j.properties,注:log4j.properties 建议放到resources目录下 别的目录会报错配置比较麻烦
在web.xml中配置log4j的监听器

<!-- 设置由Sprng载入的Log4j配置文件位置 -->
<context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>classpath:log4j.properties</param-value>
</context-param>

<listener>
  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

在pom文件中添加log4j的依赖

<!--解决Spring使用slf4j输出日志与log4j冲突的问题-->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.6.6</version>
</dependency>
<!-- log4j的包 -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.6.6</version>
</dependency>

IDEA: spring+mybatis+springMVC SSM框架(一) 搭建spring环境

log4j配置成功~

此文章源码下载(最低1分,无法免费),后续还会更新整合mybatis和springmvc,如果感觉有帮助希望可以点个关注,希望大家可以一起讨论一起进步~

https://download.****.net/download/qq_33668603/10756535