Spring学习笔记——第四天

Spring学习打卡

  • Spring学习笔记---第四天
  • Spring笔记 第四天
      • SS整合(Spring+Struts2)
      • SSM整合(配置文件版)
      • SSM整合(注解版)

Spring学习笔记—第四天

Spring笔记 第四天

SS整合(Spring+Struts2)

环境搭建:

​1.引入相关依赖

<dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>
    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>servlet-apiartifactId>
      <version>2.5version>
      <scope>providedscope>
    dependency>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aspectsartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-beansartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-expressionartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aopartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>

    
    <dependency>
      <groupId>org.apache.strutsgroupId>
      <artifactId>struts2-coreartifactId>
      <version>2.3.16version>
    dependency>

    
    <dependency>
      <groupId>org.apache.strutsgroupId>
      <artifactId>struts2-spring-pluginartifactId>
      <version>2.3.16version>
    dependency>

    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.38version>
    dependency>


    
    <dependency>
      <groupId>log4jgroupId>
      <artifactId>log4jartifactId>
      <version>1.2.15version>
    dependency>

    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-log4j12artifactId>
      <version>1.7.5version>
    dependency>

2.初始化配置 web.xml

Struts2核心过滤器

 
     <filter>
       <filter-name>struts2filter-name>
       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
     filter>

    <filter-mapping>
      <filter-name>struts2filter-name>
      <url-pattern>/*url-pattern>
    filter-mapping>

Spring的工厂监听器

​ 作用:在web开发中,创建Spring的工厂
Spring学习笔记——第四天_第1张图片

开发步骤:

​ 1.定义Service接口

​ 2.实现Service接口

​ 3.配置Service的相关信息

​ 4.开发Action

​ 5.配置Action的相关信息
Spring学习笔记——第四天_第2张图片

​6.配置Struts.xml

Spring学习笔记——第四天_第3张图片

SSM整合(配置文件版)

​ 整合思路:
Spring学习笔记——第四天_第4张图片

​环境搭建:

​ 1.引入相关依赖

 
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>servlet-apiartifactId>
      <version>2.5version>
      <scope>providedscope>
    dependency>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aspectsartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-beansartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-expressionartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aopartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>

    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.2.8version>
    dependency>

    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>1.3.2version>
    dependency>

    
    <dependency>
      <groupId>org.apache.strutsgroupId>
      <artifactId>struts2-coreartifactId>
      <version>2.3.16version>
    dependency>

    
    <dependency>
      <groupId>org.apache.strutsgroupId>
      <artifactId>struts2-spring-pluginartifactId>
      <version>2.3.16version>
    dependency>

    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.38version>
    dependency>

    
    <dependency>
      <groupId>commons-dbcpgroupId>
      <artifactId>commons-dbcpartifactId>
      <version>1.4version>
    dependency>

    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>fastjsonartifactId>
      <version>1.2.47version>
    dependency>


    
    <dependency>
      <groupId>log4jgroupId>
      <artifactId>log4jartifactId>
      <version>1.2.15version>
    dependency>

    
    <dependency>
      <groupId>jstlgroupId>
      <artifactId>jstlartifactId>
      <version>1.2version>
    dependency>

    <dependency>
      <groupId>javax.servlet.jspgroupId>
      <artifactId>jsp-apiartifactId>
      <version>2.2version>
    dependency>

2.初始化配置 web.xml

​ Struts2的核心过滤器

​ Spring的工厂监听器

<web-app>
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext.xmlparam-value>
  context-param>

   
  <filter>
    <filter-name>struts2filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
  filter>
  <filter-mapping>
    <filter-name>struts2filter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
  
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
web-app>

​ 开发步骤:

​ 1.建表

​ 2.写实体

​ 3.定义DAO接口

​ 4.Mapper文件实现DAO接口

​ 5.在Spring配置文件中配置mybatis的相关内容

​ 6.定义Service接口

​ 7.实现Service接口

​ 8.配置Service的相关信息

​ 9.配置额外功能事务

   
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource">property>
    bean>

    
    <tx:advice id="dstm" transaction-manager="transactionManager">
         <tx:attributes>
             <tx:method name="modify*"/>
             <tx:method name="*" propagation="SUPPORTS" read-only="true">tx:method>
         tx:attributes>
    tx:advice>


    <aop:config>
        
        <aop:pointcut id="pc" expression="execution(*  com.baizhi.service..*.*(..))">aop:pointcut>
        
        <aop:advisor advice-ref="dstm" pointcut-ref="pc">aop:advisor>
    aop:config>

​ 10.开发Action

​ 11.配置Action的相关信息


<bean id="userAction" class="com.baizhi.action.UserAction" scope="prototype">
     
     <property name="userService" ref="userService">property>
bean>

​ 12.配置Struts的相关信息

 <package name="user" extends="struts-default" namespace="/user">
        <action name="register" class="userAction" method="register">
             <result name="index">
                 /index.jsp
             result>
        action>
    package>                       

SSM整合(注解版)

​ 作用: 替换配置文件中繁琐的配置 提高开发效率

​ 使用:

​ 1.把对应注解放置在对应的位置上
Spring学习笔记——第四天_第5张图片

​ 2.告知Spring框架在哪一个位置使用了注解
在这里插入图片描述

​ 常用注解:

​ 1.@Component 创建简单对象

​ 位置:类的上边

​ 注意:默认提供的ID 是该类类名首字母小写

​ 2.@Autowired 给属性进行自动注入

​ 前提:Spring工厂中已经拥有该实现类对象

​ 3.@Service 创建Service的实现类对象

​ 4.@Transactional 给对应的方法添加事务

​ 前提:激活@Transactional 注解
Spring学习笔记——第四天_第6张图片

​ 5.@Controller 自动创建Action的对象

​ 默认ID类名首字母小写

​ 6.@Scope 指定创建对象的次数
Spring学习笔记——第四天_第7张图片

​ SSM(注解版)开发步骤

​ 环境搭建:



<web-app>
    <context-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:applicationContext.xmlparam-value>
    context-param>
  
  
     
      <filter>
        <filter-name>struts2filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
      filter>
       <filter-mapping>
         <filter-name>struts2filter-name>
         <url-pattern>/*url-pattern>
       filter-mapping>
  
    
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
web-app>

​ 步骤:

​ 1.建表

​ 2.写实体

​ 3.定义DAO接口

​ 4.Mapper文件实现DAO接口

​ 5.在Spring配置文件中配置mybatis的相关内容

​ 6.定义Service接口

​ 7.实现Service接口

​ @Service

​ @Autowire

​ 8.为Service添加声明式事务
Spring学习笔记——第四天_第8张图片

​ 9.开发Action

​ @Controller

​ @Scope

​ @Autowire

​ 10.配置Action的相关信息
Spring学习笔记——第四天_第9张图片

​ 11.告知Spring框架 扫描对应的注解
在这里插入图片描述

补充:

​ Spring注解与Junit集成

1.引入对应依赖

 <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>

2.使用

package com.baizhi.test;


import com.baizhi.dao.UserDAO;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
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 java.util.List;

//集成注解
@RunWith(SpringJUnit4ClassRunner.class)
//自动构建Spring工厂 相当于    //创建Spring的工厂
//        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
@ContextConfiguration("classpath:applicationContext.xml")
public class TestAnonationDriven {

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private UserService userService;

    @Test
    public  void   test1(){
        List<User> users = userDAO.selectAll();
        for (User user : users) {
            System.out.println(user);
        }
    }
    @Test
    public  void   test2(){
        userService.register(new User("111","瞎说说","123",18));

    }
}

​ Druid连接池的使用

1.引入druid对应的依赖

 <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>druidartifactId>
      <version>1.0.19version>
    dependency>

2.配置

Spring学习笔记——第四天_第10张图片

你可能感兴趣的:(spring)