Spring5框架笔记

Spring框架笔记

1.优点

  • Spring是一个开源的免费的框架(容器)!

  • Spring是一个轻量级的、非入侵式的框架!

  • 控制反转(IOC)、面向切面编程(AOP)

  • 支持事务的处理,对框架整合的支持!

 

总结一句话:Spring就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架

2.IOC本质

控制反转,是一种设计思想,DI(依赖注入)是实现IoC的一种方法,也有人认为DI只是IoC的另一种说法,没有IoC的程序中,我们使用面向对象编程,对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,个人认为所谓控制反转就是:获得依赖对象的方式反转了。

 

采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注释的方式可以把两者何为一体,Bean的定义信息直接以注释的形式定义在实现类中,从而达到了零配置的目的。

 

控制反转是一种通过描述(XML或注解)并通过第三方法生产或获取特定对象的方式,在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入。

 

3.IOC创建对象的方式

  1. 使用无参构造创建对象,默认

  2. 假设我们要使用有参构造创建对象。

    1. 下标赋值

      <!--第一种,下标赋值-->
      <bean id = "User" class = "org.wdzl.entity.User">
         <constructor-arg index= "0" value="你好啊">
         </bean>
    2. 类型

      <!--第二种方式:通过类型创建,不建议使用-->
      <bean id="User" class = "org.wdzl.entity.User">
       <constructor-arg type= "java.lang.String" value="luhaonan">
      </bean>

3.参数名

           ```xml

<!--第三种,直接通过参数名来设置--> <bean> <bean id="User" class = "org.wdzl.entity.User"> <constructor-arg name= "name" value="卢浩南"> </bean> ```

 

总结:在配置文件加载的时候,容器中管理的对象就已经初始化了!

4.Spring配置说明

4.1、别名

<!--别名,如果添加了别名,我们也可以使用别名获取到这个对象-->
<alias name ="user" alias="userNew">

4.2、Bean的配置

<!--
    id : bean 的唯一标识符,也就是相当于我们学的对象名
    class:bean 对象所对应的权限定名:包名 + 类型
    name : 也是别名,而且name可以同时取多个别名
-->
<bean id = ""  class=""   name=""> </bean>

4.3、import

这个import,一般用于团队开发使用,它可以将多个配置文件,导入合并为一个

假设,想在项目中有多个人在开发使用,这三个人负责不同的类开发,不同的类需要注册在不容的bean中,我们可以利用import将所有人的beans.xml合并为一个总的!

  • 张三

  • 李四

  • 王五

  • applicationContext.xml

<import resource = "beans.xml">
   <import resource = "beans1.xml">
       <import resource = "beans2.xml">

使用的时候,直接使用总的配置就可以了

5、依赖注入

5.1、构造器注入

就是前面的方法

5.2、Set方式注入(***重点)

  • 依赖注入:Set注入!

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

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

      【环境搭建】

      1. 复杂类型

        public class Address {
           private String address;

           public String getAddress() {
               return address;
          }

           public void setAddress(String address) {
               this.address = address;
          }
        }

         

      2. 真实测试对象

        public class Student {
           private String name;
           private Address address;
           private String[] books;
           private List<String> hobbys;
           private Map<String,String> card;
           private Set<String> games;
           private String wife;
           private Properties info;

           public String getName() {
               return name;
          }

           public void setName(String name) {
               this.name = name;
          }

           public Address getAddress() {
               return address;
          }

           public void setAddress(Address address) {
               this.address = address;
          }

           public String[] getBooks() {
               return books;
          }

           public void setBooks(String[] books) {
               this.books = books;
          }

           public List<String> getHobbys() {
               return hobbys;
          }

           public void setHobbys(List<String> hobbys) {
               this.hobbys = hobbys;
          }

           public Map<String, String> getCard() {
               return card;
          }

           public void setCard(Map<String, String> card) {
               this.card = card;
          }

           public Set<String> getGames() {
               return games;
          }

           public void setGames(Set<String> games) {
               this.games = games;
          }

           public String getWife() {
               return wife;
          }

           public void setWife(String wife) {
               this.wife = wife;
          }

           public Properties getInfo() {
               return info;
          }

           public void setInfo(Properties info) {
               this.info = info;
          }
        }

        3.beans.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"
               xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
          <bean id = "student" class="org.wdzl.pojo.Student">
              <!--第一种,普通值注入,value-->
              <property name="name" value="卢荣琳"/>
          </bean>
        </beans>

        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"
               xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        
            <bean id = "address" class="org.wdzl.pojo.Address">
            <property name="address" value="西安"/>
            </bean>>
        
        
            <bean id = "student" class="org.wdzl.pojo.Student">
              <!--第一种,普通值注入,value-->
              <property name="name" value="卢荣琳"/>
        
              <!--第二种,bean注入,ref-->
              <property name="address" ref="address"/>
        
              <!--数组注入,ref-->
              <property name="books">
                  <array>
                      <value>红楼梦</value>
                      <value>西游记</value>
                      <value>水浒传</value>
                      <value>三国演义</value>
                  </array>
              </property>
        
              <!--list-->
              <property name="hobbys">
                  <list>
                      <value>听歌</value>
                      <value>敲代码</value>
                      <value>看电视</value>
                  </list>
              </property>
        
              <!--Map-->
              <property name="card" >
                  <map>
                      <entry key="身份证" value="3165165656465156"></entry>
                      <entry key="银行卡" value="36456565498498"></entry>
                  </map>
              </property>
        
              <!--set-->
              <property name="games">
                  <set>
                      <value>LOL</value>
                      <value>COC</value>
                      <value>Dota</value>
                  </set>
              </property>
        
               <!--null-->
                <property name="wife">
                    <null/>
                </property>
        
                <!--Properties-->
                <property name="info">
                    <props>
                        <!--<prop key="学号">03175050</prop>
                        <prop key="性别">男</prop>
                        <prop key="姓名">小明</prop>-->
                        <prop key="driver">03175050</prop>
                        <prop key="url">男</prop>
                        <prop key="username">小明</prop>
                        <prop key="password">123456</prop>
        
                    </props>
                </property>
          </bean>
        </beans>

      标签的属性:

      name:用于指定注入时所调用的bean中的set方法名称

      value:用于提供基本类型和String类型的数据

      ref:用于指定其他的bean的类型,它指的是在Spring的IOC核心容器中出现的bean对象

      优势:创建对象是没有明确的限制,可以直接使用默认的构造方法

      弊端:如果某个对象中的成员必须有值,则获取对象时必须有set方法注入才可以。

      方法二:通过有参方法进行属性值注入

      使用的标签:

      constructor-arg

      出现的位置:bean标签的子标签

      标签中的属性:

      type:用于指定要注入的数据类型,代表构造方法中某个参数的类型

      index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值,索引位置是从0开始

      name:用于指定给构造函数中指定名称的参数赋值(常用方式)

      value:⽤于提供基本类型和String类型的数据

      ref:⽤于指定其他bean类型数据,他指的是在springIOC核⼼容器中出现的bean对象

      优势:获取bean对象时,注⼊数据是必须的操作,否则对象⽆法创建

      劣势:改变 了bean对象的实例⽅式,使我们创建对象时,如果⽤不到这些数据,也必须提供。

      <?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
              https://www.springframework.org/schema/beans/spring-beans.xsd">
      <!-- 第一种方式  下标赋值-->
      <!--       <bean id="user" class="hao.pojo.User">-->
          <!--        <constructor-arg index="0" value="张浩楠学习"/>-->
          <!--    </bean>-->
      
      
      <!--  第二种方式   通过类型创建  不建议使用-->
      <!--    <bean id="user" class="hao.pojo.User">-->
      <!--        <constructor-arg type="java.lang.String" value="zhang浩楠"/>-->
      <!--    </bean>-->
      
      <!--    第三种方式 直接通过参数名设置-->
          <bean id="user" class="hao.pojo.User">
              <constructor-arg name="name" value="学习"/>
          </bean>
          <bean id="userT" class="hao.pojo.UserT">
          </bean>
          <!--
           alias 别名   但是区分大小写
      
      
           bean
           id : bean 的唯一标识符 也就是相当于我们学的对象名
           class: bean 对象所对应的全限定名  :  包名+类型
           name: 也就是别名 而且name 可以同时取多个别名
      
      
           import一般用于团队开发使用,它可以将多个配置文件 导入合并为一个
      
           -->
      
      </beans>

       

       

5.3、拓展方式注入

我们可以使用p命令和C命令进行注入

5.4、bean的作用域

  1. 单例模式(Spring默认机制)

   <bean id="user2" class="org.wdzl.pojo.User" c:name="汪涵然" c:age="18" scope="singleton"/>
  1. 原型模式:每次从容器中get的时候,都会产生一个新对象

   <bean id="user2" class="org.wdzl.pojo.User" c:name="汪涵然" c:age="18" scope="prototype"/>
  1. 其余的request,session,application,这些只能在web开发中使用到

     

6、bean的自动装配

  • 自动装配是spring满足bean依赖的一种方式!

  • Spring会在上下文中自动寻找,并自动给bean装配属性

 

在Spring中有三种装配的方式

  1. 在xml中显示的配置

  2. 在Java中显示配置

  3. 隐式的自动装配bean【重要的东西】

6.1 测试

  1. 环境搭建:一个人有两个宠物

 

6.2 ByName自动装配

 <!--
   byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id~
   -->
    <bean id="people" class="org.wdzl.pojo.People" autowire="byName">
        <property name="name" value="神啊"/>
<!--        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>-->

    </bean>

6.3 ByType自动装配

<bean class="org.wdzl.pojo.Cat"/>
      <bean class="org.wdzl.pojo.Dog"/>

   <!--
   byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id~
   byType:会自动在容器上下文中查找,和自己对象属性类型相同的对应的bean
   -->
    <bean id="people" class="org.wdzl.pojo.People" autowire="byType">
        <property name="name" value="神啊"/>
<!--        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>-->

    </bean>

小结:

  • byname的时候,需要保证所有的bean唯一,并且这个bean需要和自动注入的属性的set的值一致

  • 不要type的时候,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性的类型一致

7.IOC操作bean管理(基于注解方式)

7.1 什么是注解

  1. 注解是代码的特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值,。。。。)

  2. 使用注解,注解可以作用于类、方法、属性上面

  3. 使用注解的目的:简化xml配置

7.2 Spring中常用的注解

  1. @componet: 普通创建Bean的注解,属性:value:用于指定Bean的id名称,当我们不写时,它的默认名称为当前类名,且首字母改为小写。

  2. @service: 业务逻辑层创建Bean的注解

  3. @Controller:控制层创建bean的注解

  4. @Respository:数据的持久层

  5. @Autowired:依赖关系的注入,默认根据类型匹配注入

注意:上面四个注解的功能是一模一样的,都是用来创建Bean的对象的

7.3基于注解方式实现对象的创建

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

    <context:component-scan base-package="org.wdzl"/>


</beans>

##

 

 

 

 

 

 

 

 

 

posted @ 2020-07-23 22:57  浮生丶L  阅读(171)  评论(0)    收藏  举报