SpringMVC IOC

IOC介绍

(1)什么是Spirng IOC?
控制反转(Inversion of Control,缩写为IoC)
》把原来new对象的这种方式转换成了,spring通过反射创建对象的方式
》spring创建完的对象放到一个容器中,谁需要就给谁注入进去- (获取对象并赋值给引用)

(2)SpringIOC环境搭建
1.创建Project
2.创建module maven
3.环境依赖

!--spring依赖 -->
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>5.2.9.RELEASEversion>
        dependency>

(3)SpringIOC代码编写
1.定义Person类
2.手动完成创建与赋值
3.由spring创建与赋值
创建容器对象
读取配置文件
new ClassPathXmlApplicationContext(“applicationContext.xml”);
从容器中查找getBean()

编写Test测试类
src\main\resources\applicationContext.xml

public class Test01 {
     
    private ClassPathXmlApplicationContext context;
    @Before
    public void init(){
     
         context=new ClassPathXmlApplicationContext("applicationContext.xml");
    }
 @Test
    public void test(){
     
        //1:创建ioc 容器对象  暂时看成map
        //2:给定配置文件的名称 applicationContext.xml
        //3:调用容器的getBean方法获取id对应的对
        Person person= context.getBean("person1",Person.class);//无需转型
        Person person1 = (Person) context.getBean("person1");//需转型
        //设置方法
        person.setAge(1);
       log.debug(person1+"");


    }

编写配置文件
src\main\resources\applicationContext.xml


<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">


    <bean id="person1" class="com.xjj.Bean.Person" scope="prototype" >
        
        <property name="age" value="1">property>
        <property name="name" value="xjj">property>
    bean>
    <bean id="person2" class="com.xjj.Bean.Person">
        <constructor-arg name="name" value="xjj"/>
        <constructor-arg name="age" value="2"/>
    bean>
    <bean id="person3" class="com.xjj.Demo.PersonFactory" factory-method="getBean">

    bean>
    <bean class="com.xjj.Demo.PersonFactory2" id="factory2"/>
    <bean factory-bean="factory2" factory-method="getBean" id="person4"/>

    <bean id="person6" class="com.xjj.Bean.Person"
          init-method="init"
    destroy-method="destory">
    bean>
    <bean id="person7" class="com.xjj.Bean.Person" >
        <property name="name" value="jack">property>
        <property name="arr" >
            <array>
                <value>rosevalue>
                <value>nikevalue>
            array>
        property>
    bean>
    <bean id="person8" class="com.xjj.Bean.Person" >
        <property name="name" value="jack">property>
        <property name="list" >
            <list>
                <value>rosevalue>
                <value>nikevalue>
            list>
        property>
    bean>
    <bean id="person9" class="com.xjj.Bean.Person" >
        <property name="name" value="jack">property>
        <property name="set" >
            <set>
                <value>rosevalue>
                <value>nikevalue>
            set>
        property>
        <property name="map" >
            <map>
                <entry key="1001" value="rose111">entry>
                <entry key="1002" value="rose1311">entry>
            map>
        property>
        <property name="properties" >
            <props>
                <prop key="10011">222prop>
                <prop key="2222">333prop>
            props>
        property>
    bean>
    <bean id="personService" class="com.xjj.Service.PersonService">
    <property name="personDao" ref="personDao">property>
    bean>
    <bean id="personDao" class="com.xjj.Dao.PersonDao">

        
    bean>
    <context:component-scan base-package="com.xjj"/>
beans>

SpringIOC的方法区别
(1)区别

context.getBean("id值", 类型.class);//无需转型
context.getBean("id值");//需转型

(2)bean标签的属性
id:bean标签的识别ID,理论上可以随便写
class:你要上Spring给你创建哪个类的对象,需要写上该类的全路径名

两种赋值方法

第一种

 Person person= context.getBean("person1",Person.class);//无需转型

配置文件
src\main\resources\applicationContext.xml
name:成员变量的名字
value:成员变量的值
一个property标签最后会调一个对应的set方法

 <bean id="person2" class="com.wzx.domain.Person" >
        <property name="id" value="10"/>
        <property name="name" value="rose"/>
        <property name="age" value="20"/>
    bean>

第二种,构造方法创建对象

 Person person2=new Person("xjj",2);
  System.out.println(person2);

配置文件
src\main\resources\applicationContext.xml
配置构造方法的参数的
constructor-arg 如果有四个,就表示调的一个四个参数的构造方法。
value可以赋上基本类型数据与String,但是其他对象,要使用ref
表示在当前容器中查找一个已存在的对象

 <bean id="person2" class="com.xjj.Bean.Person">
        <constructor-arg name="name" value="xjj"/>
        <constructor-arg name="age" value="2"/>
 bean>

SpringIOC静态工厂
(1)乜嘢是静态工厂?
XxxFactory.get();
(2)通过静态方法调用对象

package com.xjj.Demo;

import com.xjj.Bean.Person;

public class PersonFactory {
     
    public  static Person getBean(){
     
        return new Person();//静态方法返回的对象
    }
}

(3)factory-method
指定获取对象的静态工厂的方法


 <bean id="person3" class="com.xjj.Demo.PersonFactory" factory-method="getBean">

实例工厂创建对象

(1)什么是实例工厂
XxxFactory
(2)通过工厂对象调用成员方法获得bean对象
XxxFactory factory = new XxxFactory(); //实例化工厂对象
factory .yyy() //获得对象
(3)factory-bean 创建工厂对象
(4)factory-method 调用方法获得bean对象

配置文件

<bean factory-bean="factory2" factory-method="getBean" id="person4"/>

创建实例工厂类

package com.xjj.Demo;

import com.xjj.Bean.Person;

public class PersonFactory2 {
     
    public Person getBean() {
     
        //方法上没有static,必须先new PersonFactory2()才能调用
        return  new Person();
    }
}

测试类

// PersonFactory2 factory2 =new PersonFactory2();
//Person person=factory2.getBean();
Person person3 = (Person) context.getBean("person4");//使用这个就不需要new对象了
System.out.println(person3);

Spring单例和多例

单例:内存中只有一个对象,每次获取对象的内存地址都一样
多里:内存中每一个对象都是新的对象,每次获取对象的内存地址都不一样

每次获取对象的时候,spring是新创建一个对象还是始终给我们返回同一个对象.?

答案是:Spring默认情况下创建对象对象都是单例

scope="singleton" 单例(默认值)
       scope="prototype" 多例
       scope="request" 创建的对象放到request域中
       scope="session" 创建对象放到session对象

多实例:

 <bean id="person1" class="com.xjj.Bean.Person" scope="prototype" >

单实例:

<bean id="person1" class="com.xjj.Bean.Person" scope="singleton" >

SpringMVC生命周期

(1)生命周期
创建方法init
销毁方法destory
普通方法service
(2)属性
init-method 当该对象初始化的时候该方法会自动执行
destroy-method 当该对象即将销毁的时候会自动调用该方法

context().close关闭容器
测试类

    @Test
    public void test06(){
     
       /*Person person1=new Person();
       person1.init();
       person1.work();
       person1.work();
       person1.destory();*/
        Person person= (Person) context.getBean("person6");
        person.work();
      //自己销毁容器才执行destory()方法
        context.close();
    }
public void init(){
     
        System.out.println("HELLO");
    }
    public void work(){
     
        System.out.println("hello word");
    }
    public void destory(){
     
        System.out.println("wuhu");
    }

配置文件


    <bean id="person6" class="com.xjj.Bean.Person"
          init-method="init"
    destroy-method="destory">

Spring注入依赖DI-set方法

(1)何为注入依赖?
DI (dependency injection) 依赖注入 含义:就是给对象的属性设置值
Set对对象属性设置值
构造方法给对象初始化的时候设置值.

(2)property标签
set方式设置属性(掌握)
让spring调set方法,前提条件类中必须有set方法

name : 代表的是set方法去掉set,首字母小写setName Name name
value: 基本类型或字符串类型的值,具体给属性设置用的
ref (引用) : 引用对象的id,作为一个对象类型注入
SpringMVC IOC_第1张图片

Spring依赖注入-给复杂类型注入

(1)什么是复杂类型?
简单的是基本类型与字符串
Aarry 数组 List 集合 Map集合 Set集合 Properties集合
(2)如何给这些属性设置值
使用对应的子标签
array,list,map,set,props

 private  String[] arr;
    private List<String> list;
    private Set<String> set;
    private Map<String,String> map;
    private Properties properties;

配置文件

 <bean id="person7" class="com.xjj.Bean.Person" >
        <property name="name" value="jack">property>
        <property name="arr" >
            <array>
                <value>rosevalue>
                <value>nikevalue>
            array>
        property>
    bean>
    <bean id="person8" class="com.xjj.Bean.Person" >
        <property name="name" value="jack">property>
        <property name="list" >
            <list>
                <value>rosevalue>
                <value>nikevalue>
            list>
        property>
    bean>
    <bean id="person9" class="com.xjj.Bean.Person" >
        <property name="name" value="jack">property>
        <property name="set" >
            <set>
                <value>rosevalue>
                <value>nikevalue>
            set>
        property>
        <property name="map" >
            <map>
                <entry key="1001" value="rose111">entry>
                <entry key="1002" value="rose1311">entry>
            map>
        property>
        <property name="properties" >
            <props>
                <prop key="10011">222prop>
                <prop key="2222">333prop>
            props>
        property>
    bean>

你可能感兴趣的:(学习,spring)