java mvc依赖 spring mvc依赖包

admin2024-06-11  13


文章目录

  • 1 SpringMVC
  • 1.1 maven依赖
  • ① SpringMVC
  • ② 日志
  • ③ ServletAPI
  • ④ 导入jackson的依赖
  • ⑤ Spring5和Thymeleaf整合包
  • ⑥ 文件上传
  • 1.2 配置文件springMVC.xml
  • 1.3 web.xml
  • 2 mybatis
  • 2.1 maven依赖
  • ⑦ mybatis
  • ⑧ mysql驱动
  • ⑨ 单元测试
  • ⑩ 分页查询
  • ⑪ 加载资源插件
  • 2.2 mybatis主配置文件
  • 2.3 jdbc.properties文件
  • 2.4 mapper文件
  • 2.5 工具类:
  • 3 Spring
  • 3.1 maven依赖
  • ③ ServletAPI
  • ⑫ jsp依赖
  • ⑬ 监听器依赖
  • ⑭ spring依赖
  • ⑮ spring事务依赖
  • ⑦ mybatis
  • ⑯ mybatis和spring集成
  • ⑧ mysql驱动
  • ⑰ 阿里的连接池
  • ⑱spring-aspects
  • 3.2 配置文件
  • applicationContext_dao.xml(1-4)
  • applicationContext_service.xml
  • 3.3 web.xml文件


1 SpringMVC

1.1 maven依赖

① SpringMVC

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.1</version>
</dependency>

② 日志

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>

③ ServletAPI

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

④ 导入jackson的依赖

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>

⑤ Spring5和Thymeleaf整合包

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
    <version>3.0.12.RELEASE</version>
</dependency>

⑥ 文件上传

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

1.2 配置文件springMVC.xml

1.扫描组件
2.视图解析器
3.view-controller
4.default-servlet-handler
5.mvc注解驱动
6.文件上床解析器
7.异常处理
8.拦截器

<?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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--1.开启注解扫描-->
    <context:component-scan base-package="pdsu.edu.mvc"></context:component-scan>
    <!-- 2.配置Thymeleaf视图解析器 -->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">

                        <!-- 视图前缀 -->
                        <property name="prefix" value="/WEB-INF/templates/"/>

                        <!-- 视图后缀 -->
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8"/>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
    <!-- <bean class=" org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/templates/"></property>
       <property name="suffix" value=".jsp"></property>
   </bean>-->

    <!--3.视图控制器view-controller-->
    <mvc:view-controller path="/" view-name="index"/>
    <!--4.开放静态资源访问-->
    <mvc:default-servlet-handler/>
    <!--5.开启注解驱动-->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <!-- 处理响应中文内容乱码 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="defaultCharset" value="UTF-8" />
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html</value>
                        <value>application/json</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!--6.必须通过文件解析器的解析才能将文件转换为MultipartFile对象-->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
    <!--7.配置拦截器-->
    <mvc:interceptors>
        <!--  <bean class="pdsu.edu.mvc.interceptors.FirstInterceptor"></bean>-->
        <!--<ref bean="firstInterceptor"></ref>-->
        <mvc:interceptor>
            <mvc:mapping path="/*"/>
            <mvc:exclude-mapping path="/"/>
            <ref bean="firstInterceptor"></ref>
        </mvc:interceptor>

    </mvc:interceptors>

    <!--8.配置异常处理-->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <!-- key 指定的异常,跳转到error页面-->     
                <prop key="java.lang.ArithmeticException">error</prop>
            </props>
        </property>
        <!--将异常信息共享在请求域中的键-->
        <property name="exceptionAttribute" value="ex"></property>
    </bean>
</beans>

普通组件配置拦截器(< ref bean=“firstInterceptor”>< /ref> )

@Component//普通组件
public class FirstInterceptor implements HandlerInterceptor {
    //在控制器方法执行之前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle");
        return true;
    }

    //在控制执行之后执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
    }

    //在视图渲染之后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion");
    }
}

使用异常类处理(不在xml中配置)

@ControllerAdvice//包含@Controller注解功能
public class ExceptionController {
    @ExceptionHandler(value = {ArithmeticException.class,NullPointerException.class})//可以写多个异常
    public String testException(Exception ex, Model model){
        model.addAttribute("ex",ex);
        return "error";
    }
}

1.3 web.xml

1.配置过滤器

2.配置HiddenHttpMethodFilter

3.配置SpringMVC的前端控制器

<?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>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--配置HiddenHttpMethodFilter帮助我们将 POST 请求转换为 DELETE 或 PUT 请求 -->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 配置SpringMVC的前端控制器,对浏览器发送的请求统一进行处理 -->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 通过初始化参数指定SpringMVC配置文件的位置和名称 -->
        <init-param>
            <!-- contextConfigLocation为固定值 -->
            <param-name>contextConfigLocation</param-name>
            <!-- 使用classpath:表示从类路径查找配置文件,例如maven工程中的src/main/resources -->
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
        <!-- 
   作为框架的核心组件,在启动过程中有大量的初始化操作要做
  而这些操作放在第一次请求时才执行会严重影响访问速度
  因此需要通过此标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时
       -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <!--
        设置springMVC的核心控制器所能处理的请求的请求路径
        /所匹配的请求可以是/login或.html或.js或.css方式的请求路径
        但是/不能匹配.jsp请求路径的请求
        -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

字符编码过滤器

修改Tomcat默认编码解决中文传值乱码问题

找到tomcat / conf / server.xml中如下标签,加入URIEncoding=“UTF-8”。重启tomcat即可生效。

<Connector port="8080" protocol="HTTP/1.1" 
           connectionTimeout="20000" 
           redirectPort="8443" URIEncoding="UTF-8" />

2 mybatis

2.1 maven依赖

⑦ mybatis

<!--mybatis-->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.1</version>
</dependency>

⑧ mysql驱动

<!--mysql驱动-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.22</version>
</dependency>

⑨ 单元测试

<!--单元测试-->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.1</version>
    <scope>test</scope>
</dependency>

⑩ 分页查询

<!--分页查询-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.10</version>
</dependency>

⑪ 加载资源插件

<build>
    <!--资源插件:处理src/main/java目录中的xml-->
    <resources>
        <resource>
            <directory>src/main/java</directory><!--所在的目录-->
            <includes><!--包括目录下的.properties,.xml 文件都会扫描到-->
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    </resources>
</build>

2.2 mybatis主配置文件

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

    <!--使用外部属性配置文件   resource:指定类路径下的某个属性的配置文件-->
    <properties resource="jdbc.properties" />

    <!--设置日志-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <!--声明别名,建议不使用,使用全限定类名-->
    <typeAliases>
        <!--第一种语法格式
        type:java类型的全限定名称(自定义类型)
        alias:自定义别名
        -->
        <typeAlias type="pdsu.edu.mybatis.domain.Student" alias="stu" />
        <!--
             第二种方式:
             name:包名   mybatis会把这个包中所有类名作为别名(不区分大小写)
             优点:使用方便,一次给多个类定义别名
             缺点:别名不可以自定义,必须是类名
       -->
        <package name="pdsu.edu.mybatis.domain"/>
    </typeAliases>

    <!--声明分页插件-->
    <plugins>
        <plugin interceptor ="com.github.pagehelper.PageInterceptor" />
    </plugins>

    <!--配置 mybatis 环境-->
    <environments default="development">
        <!--id:数据源的名称-->
        <environment id="development">
            <!--配置事务类型:使用 JDBC 事务(使用 Connection 的提交和回滚)-->
            <transactionManager type="JDBC"/>
            <!--配置数据源:创建Connection对象-->
            <dataSource type="POOLED">
                <!--driver:驱动的内容-->
                <property name="driver" value="${jdbc.driver}"/>
                <!--连接数据库的内容-->
                <property name="url" value="${jdbc.url}"/>
                <!--用户名-->
                <property name="username" value="${jdbc.username}"/>
                <!--密码-->
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <!--第一种方式, resources="mapper文件的路径"
        优点:文件清晰。 加载的文件是明确的。
              文件的位置比较灵活。
        缺点:文件比较多, 代码量会比较大, 管理难度大
    -->
        <mapper resource="pdsu/edu/mybatis/dao/StudentDao.xml"/>
        <!--
       第二种方式,使用<package>
       name:包名, mapper文件所在的包名。
       特点: 把这个包中的所有mapper文件,一次加载。

       使用要求:
        1. mapper文件和dao接口在同一目录
        2. mapper文件和dao接口名称完全一样。
    -->
        <package name="pdsu.edu.mybatis.dao" />
    </mappers>
</configuration>

2.3 jdbc.properties文件

8.0版本

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
jdbc.username=root
jdbc.password=root

5.0版本

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

2.4 mapper文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="pdsu.edu.mybatis.dao.StudentDao">

</mapper>

2.5 工具类:

/*
* 工具类:创建SqlSession对象
* */
public class MybatisUtil {
    private static SqlSessionFactory factory = null;
    static {
        String config = "mybatis.xml";
        try{
            InputStream inputStream = Resources.getResourceAsStream(config);
            factory = new SqlSessionFactoryBuilder().build(inputStream);

        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //创建方法:SqlSession对象
    public static SqlSession getSqlSession(){
        SqlSession session = null;
        if (factory!=null){
           session = factory.openSession();//
        }
        return session;
    }
}

3 Spring

3.1 maven依赖

③ ServletAPI

<!-- servlet依赖 -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

⑫ jsp依赖

<!-- jsp依赖 -->
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.2.1-b03</version>
    <scope>provided</scope>
</dependency>

⑬ 监听器依赖

<!--监听器依赖-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.2.5.RELEASE</version>
</dependency>

⑭ spring依赖

<!--spring依赖-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.5.RELEASE</version>
</dependency>

⑮ spring事务依赖

<!--spring事务依赖-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.2.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.5.RELEASE</version>
</dependency>

⑦ mybatis

<!--mybatis-->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.1</version>
</dependency>

⑯ mybatis和spring集成

<!--mybatis和spring集成-->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.1</version>
</dependency>

⑧ mysql驱动

<!--mysql驱动-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.22</version>
</dependency>

⑰ 阿里的连接池

<!--阿里的连接池-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.12</version>
</dependency>

⑱spring-aspects

<!--spring-aspects-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.2.5.RELEASE</version>
</dependency>

3.2 配置文件

1 加载propeties文件

<!--加载propeties文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

2 配置数据源信息

<!--创建数据源-->
<!--配置数据源信息,德鲁伊数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>

3 创建SqlSessionFactoryBean

<!--创建SqlSessionFactoryBean-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--配置数据源-->
    <property name="dataSource" ref="dataSource"></property>
    <!--配置MyBatis的核心配置文件-->
    <property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
    <!--配置实体类-->
    <property name="typeAliasesPackage" value="pdsu.edu.mimissm.pojo"></property>
</bean>

4 创建mapper文件的扫描器

<!--创建mapper文件的扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="pdsu.edu.mimissm.mapper"></property>
</bean>

5 注解扫描

<!-- 示例1 use-default-filters="false"表示现在不是用默认filter,自己配置filter context:include-filter,设置扫描哪些内容 -->
<context:component-scan
                        base-package="pdsu.edu.service" use-default-filters="false">
    <context:include-filter type="annotation"
                            expression="org.springframework.stereotype.Service" />

</context:component-scan> 

<!-- 示例2 context:exclude-filter设置哪些内容不去扫描 -->
<context:component-scan
                        base-package="pdsu.edu.service">
    <context:exclude-filter type="annotation"
                            expression="org.springframework.stereotype.Service" />

</context:component-scan>

6 开启Aspect生成代理对象

<!-- 开启Aspect生成代理对象 -->
aop:aspectj-autoproxy/aop:aspectj-autoproxy

7 设置事务管理器

<!--设置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>

8 添加事务的切面

<!--添加事务的切面-->
<tx:advice id="myAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*select*" read-only="true"/>
        <tx:method name="*find*" read-only="true"/>
        <tx:method name="*get*" read-only="true"/>
        <tx:method name="*search*" read-only="true"/>
        <tx:method name="*insert*" propagation="REQUIRED"/>
        <tx:method name="*add*" propagation="REQUIRED"/>
        <tx:method name="*save*" propagation="REQUIRED"/>
        <tx:method name="*delete*" propagation="REQUIRED"/>
        <tx:method name="*remove*" propagation="REQUIRED"/>
        <tx:method name="*clear*" propagation="REQUIRED"/>
        <tx:method name="*update*" propagation="REQUIRED"/>
        <tx:method name="*modify*" propagation="REQUIRED"/>
        <tx:method name="*change*" propagation="REQUIRED"/>
        <tx:method name="*set*" propagation="REQUIRED"/>
        <tx:method name="*" propagation="SUPPORTS"/>
    </tx:attributes>
</tx:advice>

9 完成切面和切入点的植入

<!--完成切面和切入点的植入-->
<aop:config>
    <aop:pointcut id="mypointcut" expression="execution(* pdsu.edu.mimissm.service.*.*(..))"/>
    <aop:advisor advice-ref="myAdvice" pointcut-ref="mypointcut"></aop:advisor>
</aop:config>

10 开启事务注解

<!--开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

applicationContext_dao.xml(1-4)

<?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 https://www.springframework.org/schema/context/spring-context.xsd">

    <!--加载propeties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!--创建数据源-->
    <!--配置数据源信息,德鲁伊数据库连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!--创建SqlSessionFactoryBean-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
          <!--配置数据源-->
        <property name="dataSource" ref="dataSource"></property>
          <!--配置MyBatis的核心配置文件-->
        <property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
        <!--配置实体类-->
        <property name="typeAliasesPackage" value="pdsu.edu.mimissm.pojo"></property>
    </bean>
    <!--创建mapper文件的扫描器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="pdsu.edu.mimissm.mapper"></property>
    </bean>
</beans>

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_dao.xml"></import>-->
    <!--设定业务逻辑层的包扫描器,
    目的是在指定的路径下使用@Service注解的类,
    Spring负责创建对象,并添加依赖-->
    <context:component-scan base-package="pdsu.edu.mimissm.service">

    </context:component-scan>
    <!--设置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--添加事务的切面-->
    <tx:advice id="myAdvice" transaction-manager="transactionManager">
     <tx:attributes>
         <tx:method name="*select*" read-only="true"/>
         <tx:method name="*find*" read-only="true"/>
         <tx:method name="*get*" read-only="true"/>
         <tx:method name="*search*" read-only="true"/>
         <tx:method name="*insert*" propagation="REQUIRED"/>
         <tx:method name="*add*" propagation="REQUIRED"/>
         <tx:method name="*save*" propagation="REQUIRED"/>
         <tx:method name="*delete*" propagation="REQUIRED"/>
         <tx:method name="*remove*" propagation="REQUIRED"/>
         <tx:method name="*clear*" propagation="REQUIRED"/>
         <tx:method name="*update*" propagation="REQUIRED"/>
         <tx:method name="*modify*" propagation="REQUIRED"/>
         <tx:method name="*change*" propagation="REQUIRED"/>
         <tx:method name="*set*" propagation="REQUIRED"/>
         <tx:method name="*" propagation="SUPPORTS"/>
     </tx:attributes>
    </tx:advice>
    <!--完成切面和切入点的植入-->
    <aop:config>
        <aop:pointcut id="mypointcut" expression="execution(* pdsu.edu.mimissm.service.*.*(..))"/>
        <aop:advisor advice-ref="myAdvice" pointcut-ref="mypointcut"></aop:advisor>
    </aop:config>
</beans>

3.3 web.xml文件

<!--注册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>


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!