学习SSM第一天:SSM01

Spring概述、注入方式。

1、Spring概述

  • 开源的免费框架,是一个容器,可以管理所有的组件(类);
  • 轻量级的、非入侵的框架,不依赖于Spring的API
  • 控制反转(IOC)和面向切面编程(AOP)
  • 支持事务处理,支持对框架整合
  • 组件化、一站式
  • spring模块划分
  • [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NRuYEAPG-1602578053260)(F:\网课学习笔记\SSM\spring模块划分.png)]
  • Test:Spring的单元测试模块
  • Core Container:核心容器(IOC),包括4部分:
    • spring-core:提供了框架的基本组成部分,包括 IoC 和依赖注入功能。
    • spring-beans:提供 BeanFactory,
    • spring-context:模块建立在由core和 beans 模块的基础上建立起来的,它以一种类似于JNDI注册的方式访问对象。Context模块继承自Bean模块,并且添加了国际化(比如,使用资源束)、事件传播、资源加载和透明地创建上下文(比如,通过Servelet容器)等功能
    • spring-expression:提供了强大的表达式语言,用于在运行时查询和操作对象图。它是JSP2.1规范中定义的统一表达式语言的扩展,支持set和get属性值、属性赋值、方法调用、访问数组集合及索引的内容、逻辑算术运算、命名变量、通过名字从Spring IoC容器检索对象,还支持列表的投影、选择以及聚合等
  • AOP+Aspects:面向切面编程模块
  • Data Access:数据访问模块
  • Web:Spring开发Web引用模块

2、IOC(控制反转)

控制:资源的获取方式;

主动式:(要什么资源都是自己创建)

被动式:(资源的获取不是自己创建,交给容器创建和设置)

容器:管理所有的组件(有功能的类)

DI:Dependency Injection,依赖注入,是IOC的一种实现形式。容器能知道哪个组件运行时需要另外一个类,容器通过反射的形式,将容器中准备好的BookService对象注入(用反射)到BookServlet中 。

3、写一个简单的helloworld。

  1. ApplicationContext:IOC容器的接口

  2. ClassPathXmlApplicationContext(“ioc.xml”):ioc容器的配置文件在类路径下,

    FileSystemXmlApplicationContext(“d://ioc.xml”)ioc容器的配置文件在磁盘路径下

  3. 同一个组件在IOC容器中默认是单实例的

  4. 容器中的对象的创建在容器创建完成的时候就已经创建好了

  5. 容器中如果没有这个组件,获取组件时会报异常 NoSuchBeanDefinitionException

  6. IOC容器用property标签创建这个组件对象的时候,会利用setter方法为其属性赋值,注意属性名是set方法后的那串的首字母小写

  7. 该代码的路径:F:\新建文件夹\神器资料-很重要!!\test10113

4、属性的注入方式

  • 依赖:bean对象的创建依赖于容器

  • 注入:bean对象中所有的属性由容器来注入

    4.1 setter注入

    需要借助set方法,使用propetry标签

     <property name="lastName" value="zhangsan"/>1
    

    4.2 通过构造器注入

    使用constructor-arg标签,则调用构造器进行属性注入,需要借助有参构造

    • 通过构造函数中的参数名称注入
      <bean id="person" class="com.xiao.bean.Person">       
          <constructor-arg name="lastName" value="wangwu"/>        
        <constructor-arg name="age" value="30"/>  </bean>
    
    • 只写value属性,会默认按顺序寻找构造方法进行匹配
      <bean id="person" class="com.xiao.bean.Person">        
          <constructor-arg  value="wangwu"/>        
        <constructor-arg  value="30"/>  </bean>
    
    • 通过构造函数参数类型,默认按照顺序
    <bean id="person" class="com.xiao.bean.Person">      
          <constructor-arg type="java.lang.String" value="wangwu"/>     
        <constructor-arg type="java.lang.Integer" value="30"/>  
    </bean>
    
  • 通过构造函数参数索引,如果有多个重载的构造函数时也可以配合type一起使用

    <bean id="person" class="com.xiao.bean.Person">      
      <constructor-arg index="0" value="wangwu"/>      
        <constructor-arg index="1" value="30"/>  </bean>

4.3 p名称空间注入

使用p:propertyName直接注入属性的值。本质上还是调用的set方法

导入头文件约束:

xmlns:p="http://www.springframework.org/schema/p"1<?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:p="http://www.springframework.org/schema/p"       xsi:schemaLocation="http://www.springframework.org/schema/beans        https://www.springframework.org/schema/beans/spring-beans.xsd">    
    <bean id="person01" class="com.xiao.bean.Person">        
      <property name="lastName" value="zhangsan"/> <property name="age" value="20"/>        <property name="email" value="zhangsan@163.com"/><property name="gender" value="0"/>    
    </bean>    
      <bean id="person04" class="com.xiao.bean.Person"  p:lastName="zhangsan" p:age="30" p:email="zhangsan@qq.com" p:gender="1">    
    </bean></beans>

4.4 c命名空间注入

c(构造: Constructor)命名空间注入,使用c:propertyName注入属性值,本质上使用的是构造器注入

导入头文件约束:

 xmlns:c="http://www.springframework.org/schema/c"1  
<bean id="person05" class="com.xiao.bean.Person" c:lastName="zhangsan" c:age="30" c:email="zhangsan@qq.com" c:gender="1">    </bean>

该代码的路径:F:\新建文件夹\神器资料-很重要!!\test10113 (很重要!!!!!!!!!!!1)

5、注入不同类型的属性值

5.1注入基本类型值

之前的例子都是注入基本类型的属性。如果不赋值的话,会使用属性的默认值

5.2注入null

如果有属性给了初始值,想注入为null,则在property内部需要使用null标签( 使用value="null"是不对的 ):

    <bean id="student01" class="com.xiao.bean.Student">        
        <property name="name"><null/></property>    
</bean>

5.3注入bean

可以使用ref引用外部的值:

<!--先注册一个Address对象-->
<bean id="address01" class="com.xiao.bean.Address">    
    <property name="name" value="beijing"/>    
    <property name="num" value="001"/>
</bean>
<bean id="student02" class="com.xiao.bean.Student">    
    <!--通过id值引用-->    
    <property name="address" ref="address01"/>
</bean>

5.4集合类型赋值

01 数组

array标签+value标签:

<property name="books">    
    <array><value>西游记</value>
        <value>红楼梦</value>        
        <value>水浒传</value>    
    </array>
</property>

02 List

list标签+value标签:

<property name="hobbys">    
    <list>       
        <value>玩游戏</value>        
        <value>看电影</value>    
    </list>
</property>

03 Map

map标签+entry标签,entry也可以使用ref引用:

<property name="card">    
    <map>        
        <entry key="中行" value="001"/>        
        <entry key="邮政" value="002"/>        
        <entry key-ref="..." value-ref="...."/>    
    </map>
</property>

04 Properties

props标签:

 <property name="info">     
     <props> <prop key="学号">20190604</prop>
         <prop key="性别"></prop>         
         <prop key="姓名">小明</prop>     
     </props> 
</property>

05util名称空间

util名称空间可以创建集合类型的bean,以便别的地方引用。

头文件约束:

xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation= "http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.1.xsd"
<!--util名称空间 提取出通用的集合-->     
<util:list id="myList">        
    <value>玩游戏</value>        
    <value>看电影</value>    
</util:list><!--使用ref直接引用util提取出来的集合id即可-->    
<bean id="student05" class="com.xiao.bean.Student">        
    <property name="hobbys" ref="myList"/>    
    </bean>

5.5级联属性赋值

propetry标签中的name标签,可以使用级联属性,修改属性的属性,但是原来属性的值会被修改。

<bean id="address01" class="com.xiao.bean.Address">     
    <property name="name" value="beijing"/>     
    <property name="num" value="001"/></bean> 
<bean id="student02" class="com.xiao.bean.Student">    
    <property name="address" ref="address01"/>    <!--将address01中的num属性进行了修改-->    <property name="address.num" value="00005"/>
</bean>

5.6继承实现配置信息重用

指定parent属性为要重用的bean的id值,不写的属性就沿用,也可以重写定义属性

<bean id="person01" class="com.xiao.bean.Person">    
    <property name="lastName" value="zhangsan"/>    
    <property name="age" value="20"/>    
    <property name="email" value="zhangsan@163.com"/>    
    <property name="gender" value="0"/>    
    <property name="flag" value="true"/></bean>    
<!--parent:要重用的配置信息 -->
<bean id="person001" class="com.xiao.bean.Person" parent="person01">   
    <!--单独修改name属性的值 -->    
    <property name="lastName" value="zhang"/>
</bean>

还可以指定属性abstract=“true”,这样的bean只能被用来继承信息,不能获取实例。否则会报异常 BeanIsAbstractException

<bean id="person01" class="com.xiao.bean.Person" abstract="true">    
    <!--使用property标签为Person对象的属性赋值        name:指定属性名        value:指定属性值     -->    <property name="lastName" value="zhangsan"/>    
    <property name="age" value="20"/>    
    <property name="email" value="zhangsan@163.com"/>    
    <property name="gender" value="0"/>    
    <property name="flag" value="true"/>
</bean>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值