JAVA之Spring学习(第一天)

23 篇文章 0 订阅
15 篇文章 0 订阅

一,大纲

Spring 框架简介及官方压缩包目录介绍
Spring 环境搭建
IoC 详解
Spring 创建 Bean 的三种方式 ( 包含两种工厂方式 )
scope 属性讲解 ( 包含单例设计模式 )
DI 详解
Spring 中几种注入方式
利用 Spring DI 实现生成 SqlSessionFactory 对象

二,Spring 框架简介及官方压缩包目录

介绍
        1.主要发明者 :Rod Johnson
        2.轮子理论推崇者 :
                2.1 轮子理论: 不用重复发明轮子 .
                2.2 IT 行业: 直接使用写好的代码 .
        3.Spring 框架宗旨 : 不重新发明技术 , 让原有技术使用起来更加方便 .
        4.Spring 几大核心功能
                4.1 IoC/DI 控制反转/ 依赖注入
                4.2 AOP 面向切面编程
                4.3 声明式事务.
        5.Spring 框架 runtime
                5.1 test: spring 提供测试功能
                5.2 Core Container:核心容器.Spring 启动最基本的条件 .
                        5.2.1 Beans : Spring 负责创建类对象并管理对象
                        5.2.2 Core: 核心类
                        5.2.3 Context: 上下文参数.获取外部资源或这管理注解等
                        5.2.4 SpEl: expression.jar
                5.3 AOP: 实现 aop 功能需要依赖
                5.4 Aspects: 切面 AOP 依赖的包
                5.5 Data Access/Integration : spring 封装数据访问层相关内容
                        5.5.1 JDBC : Spring 对 JDBC 封装后的代码 .
                        5.5.2 ORM: 封装了持久层框架的代码.例如 Hibernate
                        5.5.3 transactions:对应 spring-tx.jar,声明式事务使用 .
                5.6 WEB:需要 spring 完成 web 相关功能时需要 .
                        5.6.1 例如:由 tomcat 加载 spring 配置文件时需要有 spring-web

 

        6.Spring 框架中重要概念
                6.1 容器(Container): Spring 当作一个大容器 .
                6.2 BeanFactory 接口. 老版本 .
                        6.2.1 新版本中 ApplicationContext 接口 , BeanFactory 子接
.BeanFactory 的功能在 ApplicationContext 中都有 .
         7.从 Spring3 开始把 Spring 框架的功能拆分成多个 jar.
               7.1 Spring2 及以前就一个 jar

二.IoC

        1 中文名称 : 控制反转
        2.英文名称 :( Inversion of Control )
        3.IoC 是什么 ?
                3.1 IoC 完成的事情原先由程序员主动通过 new 实例化对象事情 , 转交给 Spring 负责 .
                3.2控制反转中控制指的是:控制类的对象 .
                3.3控制反转中反转指的是转交给 Spring 负责 .
                3.4 IoC 最大的作用: 解耦 .
                        3.4.1 程序员不需要管理对象.解除了对象管理和程序员之间 的耦合 .

三.Spring 环境搭建

        1.导入 jar
                1.1 四个核心包一个日志包(commons-logging)
        2.在 src 下新建 applicationContext.xml
                2.1文件名称和路径自定义.
                2.2记住 Spring 容器 ApplicationContext,applicationContext.xml 配 置的信息最终存储到  了 AppliationContext 容器中
                2.3 spring 配置文件是基于 schema
                        2.3.1 schema 文件扩展名.xsd
                        2.3.2 把 schema 理解成 DTD 的升级版 .
                                2.3.2.1 比 DTD 具备更好的扩展性.
                        2.3.3 每次引入一个 xsd 文件是一个 namespace(xmlns)
                2.4 配置文件中只需要引入基本 schema
                        2.4.1通过<bean/> 创建对象.
                        2.4.2默认配置文件被加载时创建对象.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">
                
           <bean id="peo2" class="com.ljx.pojo.People">
        3. 编写测试方法
                3.1 getBean(“<bean>标签 id ”, 返回值类型 ); 如果没有第二个参数 ,
默认是 Object
                3.2 getBeanDefinitionNames(), Spring 容器中目前所有管理的所有
对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xm l"); People people = ac.getBean("peo",People.class); 
   System.out.println(people);
      String[] names = ac.getBeanDefinitionNames(); 
              for (String string : names) { 
                    System.out.println(string);

四.Spring 创建对象的三种方式

        1. 通过构造方法创建
                1.1无参构造创建:默认情况 .
                1.2有参构造创建:需要明确配置
                        1.2.1需要在类中提供有参构造方法
                        1.2.2在 applicationContext.xml 中设置调用哪个构造方法创建 对象
                                1.2.2.1 如果设定的条件匹配多个构造方法执行最后的构造方法
                                1.2.2.2 index :参数的索引,从 0 开始
                                1.2.2.3 name:参数名
                                1.2.2.4 type:类型(区分开关键字和封装类 int Integer)
           <bean id="peo2" class="com.ljx.pojo.People">
               <constructor-arg index="0" name="id" type="int" value="1"></constructor-arg>
               <constructor-arg index="1" name="name"  type="String" value="zhangsan"></constructor-arg>
           </bean> 
        2. 实例工厂
                2.1 工厂设计模式: 帮助创建类对象 . 一个工厂可以生产多个对象 .
                2.2实例工厂:需要先创建工厂 , 才能生产对象
                2.3实现步骤:
                        2.3.1 必须要有一个实例工厂

五.如何给 Bean 的属性赋值(注入)

        1.通过构造方法设置值 .
        2.设置注入 ( 通过 set 方法 )
                2.1 如果属性是基本数据类型或 String 等简单
           <bean id="peo3" class="com.ljx.pojo.People">
              <property name="id" value="1"></property>
              <property name="name" value="zhangsan"></property>
              <property name="age" value="1"></property>
           </bean >
                2.2 如果属性是 Set<?>
 <!--set注入  -->
           <bean id="peo5" class="com.ljx.pojo.People">
              <property name="set" value="1,2,3">
             <!--       <set>
                      <value>1</value>
                      <value>2</value>
                      <value>3</value>
                   </set> -->
              </property>
           </bean>

六. DI

        1.DI:中文名称 : 依赖注入
        2.英文名称( (Dependency Injection )
        3.DI 是什么 ?
                3.1 DI 和 IoC 是一样的
                3.2 当一个类(A) 中需要依赖另一个类 () 对象时 , B 赋值给 A 的过 程就叫做依赖注入 .
        4.代码体现:
           <!--DI,引用一个对象  -->
           <bean id="peo6" class="com.ljx.pojo.People">
             <property name="student" ref="student"></property>
           </bean>
           <bean id="student" class="com.ljx.pojo.Sutdent">
             <property name="id" value="1"/>
             <property name="name" value="张氏"></property>
           </bean>

七.使用 Spring 简化 MyBatis

        1. 导入 mybatis jar spring ,spring-jdbc,spring-tx,spring-aop,spring-web,spring 整合 mybatis 的包

         2. 先配置 web.xml

     <!--上下文参数  -->
     <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:applicationContext.xml</param-value>
     </context-param>                                   
     <!--安装了一个监听,帮助加载Spring配置文件  -->                             
    <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
        3. 编写 spring 配置文件 applicationContext.xml
    <!--配置JDBC  
           //数据源封装,
       -->
        <bean id="dataSouce" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		    	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		    	<property name="url" value="jdbc:mysql://localhost:3306/ssm"></property>
		    	<property name="username" value="root"></property>
		    	<property name="password" value="root"></property>
       </bean>
       <!--配置工厂的  -->
       <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
          <property name="dataSource" ref="dataSouce"></property>
       </bean>
       <!--配置扫描器  -->
       <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
           <property name="basePackage" value="com.ljx.mapper"></property>
           <property name="sqlSessionFactory" ref="factory"></property>
       </bean>
       <!--控制service  -->
       <bean id="airPortService" class="com.ljx.serivce.impl.AirPortServiceImpl">
            <property name="airPortMapper" ref="airPortMapper"></property>
       </bean>
        4. 编写代码
                4.1正常编写 pojo
                4.2 编写 mapper 包下时必须使用接口绑定方案或注解方案 ( 必须 有接口 )
                4.3 正常编写 Service 接口和 Service 实现类
                        4.3.1 需要在 Service 实现类中声明 Mapper 接口对象 , 并生成 get/set 方法
                4.4 spring 无法管理 Servlet, service 中取出 Servie 对象

 

public class AirportServlet extends HttpServlet{ 
     private AirportService airportService; 
@Override 
     public void init() throws ServletException {
         //对 service 实例化 //
         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xm l"); //spring 和 web 整合后所有信息都存放在 webApplicationContext
        ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationC ontext(getServletContext()); 
        airportService=ac.getBean("airportService",AirportS erviceImpl.class); }
@Override
 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 

req.setAttribute("list", airportService.show()); req.getRequestDispatcher("index.jsp").forward(req, resp);
 } 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值