Loading

Spring笔记(一)

Spring IoC

一、IoC容器(概念)

1. 什么是IoC?

  • 控制反转,把对象创建和对象之间得调用过程,交给了Spring来管理
  • 使用IoC得目的:为了降低耦合度

2. IoC底层使用了哪些技术?

  • XML解析
  • 工厂模式
  • 反射

3. Spring提供的IoC容器实现的两种方式(两个接口):

  • BeanFactory接口:IoC容器基本实现,是Spring内部的使用接口,不提供开发人员进行使用(加载配置文件的时候不会创建对象,只有在获取对象的时候才回去创建对象,懒加载)
  • ApplicationContext接口:BeanFactory接口的子接口,提供了更多更强大的功能,一般由开发人员进行使用(加载配置文件的时候就会把在配置文件对象进行创建)

4. ApplicationContext接口的实现类

  • FileSystemXmlApplicationContext:加载xml配置文件的时候使用的是绝对路径
  • ClassPathXmlApplicationContext:加载xml配置文件的时候使用的是相对路径,src目录下为根目录

二、 IoC容器 - Bean管理(基于XML方式)

1. Bean管理由两个操作:

  • Spring创建对象
  • Spring注入属性

2. 基于XML方式创建对象

  • <bean id="该Bean对象的id名称,唯一标识" class="Bean对象的全类名"></bean>
    

    创建对象的时候,默认执行无参数构造的方法完成对象的创建

3. 基于XML方式注入属性

  • DI:依赖注入(DI是IoC中的一种具体实现)
  1. 使用set方法进行注入

    public class Book {
        //创建属性
        private String bname;
    
        //创建属性对应的set方法
        public void setBname(String bname) {
            this.bname = bname;
        }
    }
    
    <bean id="book" class="com.atguigu.spring5.Book">
        <!--
    		使用property完成属性注入
    		name:类里面属性名称
    		value:向属性注入的值
    	-->
        <propert1y name="bname" value="Hello"></property>
        <property name="bauthor" value="World"></property>
    </bean>
    
  2. 有参构造函数注入

    public class Orders {
        //属性
        private String oname;
        private String address;
        //有参数构造
        public Orders(String oname,String address) {
            this.oname = oname;
            this.address = address;
        }
    }
    
    <bean id="orders" class="com.atguigu.spring5.Orders">
        <constructor-arg name="oname" value="Hello"></constructor-arg>
        <constructor-arg name="address" value="China!"></constructor-arg>
    </bean>
    
  3. p名称空间注入(了解即可)

    <!--1、添加p名称空间在配置文件头部-->
    <?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"		<!--在这里添加一行p-->
    
    	<!--2、在bean标签进行属性注入(算是set方式注入的简化操作)-->
        <bean id="book" class="com.atguigu.spring5.Book" p:bname="hello" p:bauthor="world"></bean>
    </beans>
    

4. XML注入其他类型的属性

  • 注入null值

    <property name="address">
        <null/>
    </property>
    
  • 注入特殊符号

    <!-- 有两种方法:
    			将符号转义:&lt;替换<,
    			使用CDATA
    -->
    <property name = "address">
        <value><![CDATA[<<南京>>]]></value>
    </property>
    

5. 注入属性:外部Bean

  • 创建两个类Service类和dao类

  • 在service调用dao里面的方法

  • 在Spring配置文件中进行配置

    //service类
    public class UserService {
    
        //创建UserDao类型属性,生成set方法
        private UserDao userDao;
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
    
        public void add() {
            System.out.println("service add...............");
            //调用dao方法
            userDao.update();
        }
    }
    
    //dao类
    public class UserDaoImpl implements UserDao {
    
        @Override
        public void update() {
            System.out.println("dao update...........");
        }
    }
    
  • 在Spring配置文件中进行配置

    <bean id="userService" class="com.atguigu.spring5.UserService">
        <!-- 
    		注入dao对象、
    		name属性:类里面的id名称
    		ref属性:创建userDao对象bean标签id值
    	-->
        <property name="userDao" ref="userDaoImpl"></property>
    </bean>
    <bean id="userDaoImpl" class="com.atguigu.spring5.UserDaoImpl"></bean>
    

6. 注入属性:内部Bean

  • 一对多关系:部门和员工,一个部门有多个员工,一个员工属于一个部门

  • 在实体类之间表示一对多关系,员工表示所属部门,在使用对象类型属性进行表示

    //部门类
    public class Dept {
        private String dname;
        public void setDname(String dname) {
            this.dname = dname;
        }
    }
    
    //员工类
    public class Emp {
        private String ename;
        private String gender;
        //员工属于某一个部门,使用对象形式表示
        private Dept dept;
        
        public void setDept(Dept dept) {
            this.dept = dept;
        }
        public void setEname(String ename) {
            this.ename = ename;
        }
        public void setGender(String gender) {
            this.gender = gender;
        }
    }
    
  • 在Spring配置文件中配置

    <bean id="emp" class="com.atguigu.spring5.bean.Emp">
        <!-- 设置普通属性的值 -->
        <property name="ename" value="Andy"></property>
        <property name="gender" value="女"></property>
        
        <!-- 设置对象类型的属性 -->
        <property name="dept">
            <bean id="dept" name="com.atguigu.spring5.bean.Dept">
                <property name="dname" value="安保部"></property>
            </bean>
        </property>
    </bean>
    

7. 注入属性:级联赋值

  • 方法一:级联赋值

    <!--方式一:级联赋值-->
    <bean id="emp" class="com.atguigu.spring5.bean.Emp">
        <!--设置两个普通属性-->
        <property name="ename" value="Andy"></property>
        <property name="gender" value="女"></property>
        <!--级联赋值-->
        <property name="dept" ref="dept"></property>
    </bean>
    <bean id="dept" class="com.atguigu.spring5.bean.Dept">
        <property name="dname" value="公关部门"></property>
    </bean>
    
  • 方法二:生成dept的get方法(必须要有get方法)

     <!-- 级联赋值 -->
    <bean id="emp" class="com.atguigu.spring5.bean.Emp">
        <!--设置两个普通属性-->
        <property name="ename" value="jams"></property>
        <property name="gender" value="男"></property>
        <!--级联赋值-->
        <property name="dept" ref="dept"></property>
        <property name="dept.dname" value="技术部门"></property>
    </bean>
    
    <bean id="dept" class="com.atguigu.spring5.bean.Dept"></bean>
    

8. XML注入集合属性

  • 注入数组类型属性
  • 注入List集合类型属性
  • 注入Map集合类型属性
  • 注入Set集合类型属性
//(1)创建类,定义数组、list、map、set 类型属性,生成对应 set 方法
public class Stu {
    //1 数组类型属性
    private String[] courses;
    //2 list集合类型属性
    private List<String> list;
    //3 map集合类型属性
    private Map<String,String> maps;
    //4 set集合类型属性
    private Set<String> sets;
    
    public void setSets(Set<String> sets) {
        this.sets = sets;
    }
    public void setCourses(String[] courses) {
        this.courses = courses;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }
}
<!--(2)在 spring 配置文件进行配置 -->
<bean id="stu" class="com.atguigu.spring5.collectiontype.Stu">
    <!--数组类型属性注入-->
    <property name="courses">
        <array>
            <value>java课程</value>
            <value>数据库课程</value>
        </array>
    </property>
    <!--list类型属性注入-->
    <property name="list">
        <list>
            <value>张三</value>
            <value>小三</value>
        </list>
    </property>
    <!--map类型属性注入-->
    <property name="maps">
        <map>
            <entry key="JAVA" value="java"></entry>
            <entry key="PHP" value="php"></entry>
        </map>
    </property>
    <!--set类型属性注入-->
    <property name="sets">
        <set>
            <value>MySQL</value>
            <value>Redis</value>
        </set>
    </property>
</bean>

9. 在集合里面设置对象类型的值

  • 如果集合里的元素类型是对象,则不能用普通的方法来注入

    // 创建课程类,学生所学多门课程
    public class Course {
        private String cname;
    
        public void setCname(String cname) {
            this.cname = cname;
        }
    
        @Override
        public String toString() {
            return "Course{" +
                    "cname='" + cname + '\'' +
                    '}';
        }
    }
    
  • 然后编辑XML配置文件

    <!-- 先创建多个course对象 -->
    <bean id="course1" class="com.atguigu.spring5.collectiontype.Course">
        <property name="cname" value="Spring5框架"></property>
    </bean>
    <bean id="course2" class="com.atguigu.spring5.collectiontype.Course">
        <property name="cname" value="MyBatis框架"></property>
    </bean>
    
    <!-- 然后在property中注入 -->
    <bean>
        <property name="courseList">
            <list>
                <ref bean="course1"></ref>
                <ref bean="course2"></ref>
            </list>
        </property>
    </bean>
    

10. 将集合注入的部分提取出来

  • 先在spring配置文件中映入名称空间util

    <?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:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                               http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    </beans>
    
  • 使用util标签完成List集合注入提取

    <util:list id="bookList">
        <value>易筋经</value>
        <value>九阴真经</value>
        <value>葵花宝典</value>
    </util:list>
    
  • 将提取出来的集合类型属性注入使用

    <bean id="book" class="com.atguigu.spring5.collectiontype.Book">
        <property name="list" ref="bookList"></property>
    </bean>
    

11. FactoryBean

  • Spring的bean有两种类型:一种是普通的bean,一种是工厂bean(FactoryBean)

    • 普通bean:在配置中定义的bean类型就是返回类型

    • 工厂bean:在配置文件定义bean类型可以和返回类型不一样

  • 第一步,先创建类,让这个类作为工厂bean,实现接口FactoryBean

  • 第二步,实现接口里面的方法,在实现的方法中定义返回的bean类型

    public class MyBean implements FactoryBean<Course> {
    
        //定义返回bean
        @Override
        public Course getObject() throws Exception {
            Course course = new Course();
            course.setCname("hhh");
            return course;
        }
    }
    
    <bean id="myBean" class="com.atguigu.spring5.factorybean.MyBean"></bean>
    
    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        //返回值类型可以不是定义的bean类型
        Course course = context.getBean("myBean", Course.class);
        System.out.println(course);
    }
    
    

12. bean作用域

  • 在Spring里面,bean默认是单实例对象(也可以是多实例对象,但是需要我们手动配置)

  • 设置单实例还是多实例:

    • 在Spring的配置文件bean标签里面有属性scope,用于设置单实例/多实例
    • scope属性值有两个:singleton(单实例,默认的)、prototype(多实例)
    <!--设置为多实例对象-->
    <bean id="book" class="com.atguigu.spring5.collectiontype.Book" scope="prototype">
            <property name="list" ref="bookList"></property>
    </bean>
    
  • singleton和prototype的区别

    • singleton单实例,prototype多实例
    • 设置scope值为singleton的时候,加载spring配置文件的时候就会创建单实例对象(饿汉);值为prototype时候,只在调用getBean的时候创建多实例对象(懒汉)

13. bean生命周期

  • 什么是生命周期:从对象创建到销毁的过程

  • bean生命周期:

    1. 通过构造器创建bean实例(无参构造)

    2. 为bean的属性设置值和对其他bean的引用(set方法)

    3. 调用bean的初始化方法(需要进行配置初始化方法)

    4. 获取到了对象,可以开始使用

    5. 当容器关闭的时候,调用bean的销毁方法,此时对象被销毁(需要进行配置销毁方法)

    public class Orders {
        private String oname;
        
        //无参数构造
        public Orders() {
            System.out.println("第一步 执行无参数构造创建 bean 实例");
        }
        
        public void setOname(String oname) {
            this.oname = oname;
            System.out.println("第二步 调用 set 方法设置属性值");
        }
        
        //创建执行的初始化的方法
        public void initMethod() {
            System.out.println("第三步 执行初始化的方法");
        }
        
        //创建执行的销毁的方法
        public void destroyMethod() {
            System.out.println("第五步 执行销毁的方法");
        }
    }
    
    <bean id="orders" class="com.atguigu.spring5.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
        <property name="oname" value="手机"></property>
    </bean>
    
     @Test
     public void testBean3() {
    	ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
    	Orders orders = context.getBean("orders", Orders.class);
    	System.out.println("第四步 获取创建 bean 实例对象");
    	System.out.println(orders);
     	//手动让 bean 实例销毁,需要向下转型
    	((ClassPathXmlApplicationContext)applicationContext).close();
     }
    
  • 正常的生命周期有5步,如果配置了后置处理器,则生命周期有7步

    1. 通过构造器创建bean实例(无参构造)
    2. 为bean的属性设置值和对其他bean的引用(set方法)
    3. 把 bean 实例传递 bean 后置处理器的方法(postProcessBeforeInitialization)
    4. 调用bean的初始化方法(需要进行配置初始化方法)
    5. 把 bean 实例传递 bean 后置处理器的方法(postProcessAfterInitialization)
    6. 获取到了对象,可以开始使用
    7. 当容器关闭的时候,调用bean的销毁方法,此时对象被销毁(需要进行配置销毁方法)
  • 配置后置处理器(只要配置了后置处理器,则对该配置文件里面的所有bean都生效):

    // 创建后置处理器实现类,需要实现BeanPostProcessor
    public class MyBeanPost implements BeanPostProcessor {
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("在初始化之前执行的方法");
            return bean;
        }
        
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("在初始化之后执行的方法");
            return bean;
        }
    }
    
    <bean id="myBeanPost" class="com.atguigu.spring5.bean.MyBeanPost"></bean>
    

14. 自动装配

  • 什么是自动装配?

    • 根据指定装配规则(属性名称或者属性类型),Spring自动将匹配的属性进行注入
  • 实现自动装配过程

    • bean标签属性autowire,配置自动装配

    • autowire常用的两个值:

      • byName:根据属性名称自动注入,注入的bean的id要和类属性名称一样

      • byType:根据属性类型注入(不能有两个相同类型的bean)

    1. 根据属性名称自动注入

      <bean id="emp" class="com.atguigu.spring5.autowire.Emp" autowire="byName">
      	<!--<property name="dept" ref="dept"></property>-->
      </bean>
      <bean id="dept" class="com.atguigu.spring5.autowire.Dept"></bean>
      
    2. 根据属性类型自动注入

      <bean id="emp" class="com.atguigu.spring5.autowire.Emp" autowire="byType">
      	<!--<property name="dept" ref="dept"></property>-->
      </bean>
      <bean id="dept" class="com.atguigu.spring5.autowire.Dept"></bean>
      

15. 外部属性文件

  • 直接配置数据库信息

    1. 配置druid连接池
    2. 引入druid依赖的jar包
    <!--直接配置连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/study"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    
  • 引入外部属性文件配置数据库连接池

    1. 创建外部属性文件,properties格式的文件,写入连接使用数据库的信息

      driverClass=com.mysql.jdbc.Driver
      url=jdbc:mysql://localhost:3306/study
      username=root
      password=root
      
    2. 把外部的properties属性文件引入到spring的配置文件中(引入context名称空间)

      <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 http://www.springframework.org/schema/context/spring-context.xsd">
          
      	<!--使用标签引入外部属性文件-->
          <context:property-placeholder location="classpath:jdbc.properties"/>
      
          <!--配置连接池-->
          <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
              <property name="driverClassName" value="${driverClass}"></property>
              <property name="url" value="${url}"></property>
              <property name="username" value="${userName}"></property>
              <property name="password" value="${password}"></property>
          </bean>
      </beans>
      
posted @ 2021-03-18 00:14  linzeliang  阅读(97)  评论(0)    收藏  举报