2. spring IOC基本使用

1 spring_helloworld

1.1 使用手动加载jar包的方式实现
  1. 现在基本不使用这种方式
  2. 导包:其中第一个jar包在maven官网找到,剩下几个在spring压缩包下载地址中,下载的spring-5.2.6.RELEASE-dist.zip文件解压即可得到
    1. commons-logging-1.2.jar
    2. spring-beans-5.2.3.RELEASE.jar
    3. spring-context-5.2.3.RELEASE.jar
    4. spring-core-5.2.3.RELEASE.jar
    5. spring-expression-5.2.3.RELEASE.jar
  3. Person
package com.mashibing.bean;

public class Person {
    private int id;
    private String name;
    private int age;
    private String gender;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
}

  1. 编写spring的配置文件
    1. src–New–XML Configuration File-- Spring Config–ioc.xml
    2. 导jar包后才有这个选项
<?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">

    <!--注册一个对象,spring回自动创建这个对象-->
    <!--
    一个bean标签就表示一个对象
    id:这个对象的唯一标识,不能有两个id相同的bean
    class:注册对象的完全限定名
    -->
    <bean id="person" class="com.mashibing.bean.Person">
        <!--使用property标签给对象的属性赋值
        name:表示属性的名称
        value:表示属性的值
        -->
        <property name="id" value="1"></property>
        <property name="name" value="zhangsan"></property>
        <property name="age" value="18"></property>
        <property name="gender" value=""></property>
    </bean>
</beans>
  1. SpringDemoTest:测试类
package com.mashibing.test;

import com.mashibing.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//容器中的对象在使用该对象前,就已经把对象创建好了
public class SpringDemoTest {
    public static void main(String[] args) {
        //ApplicationContext:表示ioc容器
        //ClassPathXmlApplicationContext:表示从当前classpath路径中获取xml文件的配置
        //根据spring的配置文件来获取ioc容器对象
        ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
        Person person = (Person) context.getBean("person");
        //获取对象时不需要强制类型转换的方法
        //Person person = context.getBean("person",Person.class);
        System.out.println(person);
    }
}
1.2 使用maven的方式来构建项目
  1. 先下载老师提供的3.5.4的maven,如果一个jar需要依赖其他jar,maven会帮我们自动下载
  2. 有些jar包在国外,下载非常慢,所以需要修改maven仓库默认地址,在配置文件conf\settings.xml中
<!--可以配置下载到本地的哪个路径下-->
<localRepository>E:\javaMvn</localRepository>
...
<!--用阿里云的维护的一个jar包仓库,下载速度有保证-->
<mirror>
  <id>alimaven</id>
  <name>aliyun maven</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  <mirrorOf>central</mirrorOf>
</mirror>
  1. 配置环境变量
    1. MAVEN_HOME:E:\Program Files (x86)\apache-maven-3.5.4
    2. Path:%MAVEN_HOME%\bin
    3. cmd输入mvn -v,能输出版本号,说明配置无误
  2. 在project中配置maven
    在这里插入图片描述
  3. idea中创建maven项目
    1. GroupId:com.mashibing,类似原来包名命名规则
    2. ArtifactId:spring_study,类似原来project命名规则
    3. 在右下角出现maven project need to imported时,选enable auto import
      在这里插入图片描述
      在这里插入图片描述
  4. 修改pom依赖:
    1. 在maven官网搜索Spring Context,复制其xml配置粘贴到pom.xml中的dependencies标签中,由于Context包在core模块内,此时Core模块下所有需要的jar包都会被自动引入
      在这里插入图片描述
    2. pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>spring_01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
    </dependencies>

</project>
  1. 编写spring的配置文件
    1. resources–New–XML Configuration File-- Spring Config–ioc.xml
    2. 使用maven构建项目时,resources中内容会自动加载到项目的classes文件夹下,也就是在classpath中
  2. 剩下内容与1.1中一致
1.3 总结
  1. 以上两种方式创建spring的项目都是可以的,但是在现在的企业开发环境中使用更多的还是maven这样的方式,无须自己处理jar之间的依赖关系,也无须提前下载jar包,只需要配置相关的pom即可,因此推荐大家使用maven的方式,具体的maven操作大家可以看maven的详细操作文档
  2. 搭建spring项目需要注意的点
    1. 一定要将配置文件添加到类路径中,使用idea的maven创建项目的时候要放在resource目录下
    2. 使用导包的方式时,别忘了commons-logging-1.2.jar包
  3. 细节点
    1. ApplicationContext就是IOC容器的接口,可以通过此对象获取容器中创建的对象
    2. 其实现类有FileSystemXmlApplicationContext和ClassPathXmlApplicationContext。前者为从文件系统中查找配置文件,后者为从classpath中查找文件。一般不用前者,因为如果使用前者windows开发的内容布在linux上时,还需要改路径
    3. 对象在Spring容器创建完成的时候就已经创建完成,不是需要用的时候才创建
    4. 对象在IOC容器中存储的时候都是单例的,如果需要多例需要修改属性
    5. 创建对象给属性赋值的时候是通过setter方法实现的。也就是对象必须有getter和setter方法,否则配置文件都会报错,也无法注入
    6. 对象的属性是由setter/getter方法决定的,而不是定义的成员属性。也就是setter方法叫什么,配置文件中,property应该写什么,而不是属性本身叫什么决定

2 获取对象

2.1 通过bean的id

上面已经使用过

2.2 通过bean的类型
  1. MyTest
import com.mashibing.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
        Person bean = context.getBean(Person.class);
        System.out.println(bean);
    }
}
  1. 注意:通过bean的类型在查找对象的时候,在配置文件中不能存在两个类型一致的bean对象,如果有的话,可以通过如下方法
Person person = context.getBean("person", Person.class);

3 为对象属性赋值

3.1 通过setter方法
  1. 上面已经使用过
  2. 使用这种方法,可以想象spring一定是先创建出一个对象,再调用该对象的setter方法为其属性赋值
  3. 因此使用这种方法前提是对象必须有无参构造方法,因为spring底层实现是反射,没有无参构造器会报错
3.2 通过构造器
  1. name:参数列表中形参名
<!--给person类添加构造方法,参数name由构造方法中形参列表名决定-->
<bean id="person2" class="com.mashibing.bean.Person">
    <constructor-arg name="id" value="1"></constructor-arg>
    <constructor-arg name="name" value="lisi"></constructor-arg>
    <constructor-arg name="age" value="20"></constructor-arg>
    <constructor-arg name="gender" value=""></constructor-arg>
</bean>
  1. value:为形参传的实参值
    1. 如果形参为Date类型,那么value要固定写为"2020/02/08"这种格式
<!--在使用构造器赋值的时候可以省略name属性,但是此时就要求必须严格按照构造器参数的顺序来填写了,而且如果有多个参数个数相同,不同类型的构造器的时候,默认调用最后面的构造方法-->
<bean id="person3" class="com.mashibing.bean.Person">
    <constructor-arg value="1"></constructor-arg>
    <constructor-arg value="lisi"></constructor-arg>
    <constructor-arg value="20"></constructor-arg>
    <constructor-arg value=""></constructor-arg>
</bean>
  1. index:值的下标,从0开始
<!--如果想不按照顺序来添加参数值,那么可以搭配index属性来使用-->
<bean id="person4" class="com.mashibing.bean.Person">
    <constructor-arg value="lisi" index="1"></constructor-arg>
    <constructor-arg value="1" index="0"></constructor-arg>
    <constructor-arg value="" index="3"></constructor-arg>
    <constructor-arg value="20" index="2"></constructor-arg>
</bean>
  1. type:值的类型
<!--如果形参中为int型,那么type也写int型,如果形参中为Integer,形参中应该写java.lang.Integer-->
<!--需要type跟index组合使用,才能使用第一个构造器,必须指定第几个参数是哪个构造器,如果只指定type="int",还是使用第二个构造器,而value对应的20,会被当做int类型的id处理-->
<bean id="person5" class="com.mashibing.bean.Person">
    <constructor-arg value="1"></constructor-arg>
    <constructor-arg value="lisi"></constructor-arg>
    <constructor-arg value="20" type="int" index="2"></constructor-arg>
</bean>
public Person(int id, String name, Integer age) {
    this.id = id;
    this.name = name;
    this.age = age;
    System.out.println("Age");
}

public Person(int id, String name, String gender) {
    this.id = id;
    this.name = name;
    this.gender = gender;
    System.out.println("gender");
}
  1. 日常工作中,都使用name+value方式,很少有人用index或type的方式,但要注意各种情况出现的问题
3.3 通过命名空间:简化配置文件中属性声明的写法
  1. 导入命名空间xmlns:p:类似JSTL标签的用法,其实就是导入了另一种写法
<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns:xml name spcae,xsi:xml schema instance,schemaLocation表示记录了定义规范的位置,ioc.xml中能写bean、constructor-arg等这些标签,就是因为schemaLocation中定义了xsd的位置-->
<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 http://www.springframework.org/schema/beans/spring-beans.xsd">

  1. 具体配置
<!--p:表示使用命名空间p-->
<bean id="person6" class="com.mashibing.bean.Person" p:id="3" p:name="wangwu" p:age="22" p:gender=""></bean>
3.4 为复杂类型进行赋值操作
  1. 在之前的测试代码中,我们都是给最基本的属性进行赋值操作,在正常的企业级开发中还会遇到给各种复杂类型赋值,如集合、数组、其他对象等
  2. Person.java
package com.mashibing.bean;

import java.util.*;

public class Person {
    private int id;
    private String name="dahuang";
    private int age;
    private String gender;
    private Address address;
    private String[] hobbies;
    private List<Book> books;
    private Set<Integer> sets;
    private Map<String,Object> maps;
    private Properties properties;

    public Person(int id, String name, int age, String gender) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        System.out.println("有参构造器");
    }

    public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
        System.out.println("Age");
    }

    public Person(int id, String name, String gender) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        System.out.println("gender");
    }

    public Person() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Address getAddress() {
        return address;
    }

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

    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public String[] getHobbies() {
        return hobbies;
    }

    public void setHobbies(String[] hobbies) {
        this.hobbies = hobbies;
    }

    public Set<Integer> getSets() {
        return sets;
    }

    public void setSets(Set<Integer> sets) {
        this.sets = sets;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", address=" + address +
                ", hobbies=" + Arrays.toString(hobbies) +
                ", books=" + books +
                ", sets=" + sets +
                ", maps=" + maps +
                ", properties=" + properties +
                '}';
    }
}
  1. Book.java
package com.mashibing.bean;

public class Book {
    private String name;
    private String author;
    private double price;

    public Book() {
    }

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    public String getName() {
        return name;
    }

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

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
}

  1. Address.java
package com.mashibing.bean;

public class Address {
    private String province;
    private String city;
    private String town;

    public Address() {
    }

    public Address(String province, String city, String town) {
        this.province = province;
        this.city = city;
        this.town = town;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getTown() {
        return town;
    }

    public void setTown(String town) {
        this.town = town;
    }

    @Override
    public String toString() {
        return "Address{" +
                "province='" + province + '\'' +
                ", city='" + city + '\'' +
                ", town='" + town + '\'' +
                '}';
    }
}
  1. ioc.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:p="http://www.springframework.org/schema/p"
       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 https://www.springframework.org/schema/util/spring-util.xsd"
>

    <!--给复杂类型的赋值都在property标签内进行-->
    <bean id="person" class="com.mashibing.bean.Person">
        <property name="name">
            <!--赋空值-->
            <null></null>
        </property>
        <!--通过ref引用其他对象,引用外部bean-->
        <property name="address" ref="address"></property>
        <!--引用内部bean-->
        <!-- <property name="address">
             <bean class="com.mashibing.bean.Address">
                 <property name="province" value="北京"></property>
                 <property name="city" value="北京"></property>
                 <property name="town" value="西城区"></property>
             </bean>
         </property>-->
        <!--为list赋值-->
        <property name="books">
            <list>
                <!--内部bean,内部bean无法从ioc容器中直接获取对象的值,外部bean可以-->
                <bean id="book1" class="com.mashibing.bean.Book">
                    <property name="name" value="多线程与高并发"></property>
                    <property name="author" value="马士兵"></property>
                    <property name="price" value="1000"></property>
                </bean>
                <!--外部bean-->
                <ref bean="book2"></ref>
            </list>
        </property>
        <!--给map赋值-->
        <!--<property name="maps" ref="myMap"></property>-->
        <property name="maps">
            <map>
                <entry key="a" value="aaa"></entry>
                <entry key="address" value-ref="address"></entry>
                <entry key="address2">
                    <bean class="com.mashibing.bean.Address">
                        <property name="province" value="广东省"></property>
                    </bean>
                </entry>
            </map>
        </property>
        <!--给property赋值-->
        <property name="properties">
            <props>
                <prop key="aaa">aaa</prop>
                <prop key="bbb">222</prop>
            </props>
        </property>
        <!--给数组赋值-->
        <property name="hobbies">
            <array>
                <value>book</value>
                <value>movie</value>
                <value>game</value>
            </array>
        </property>
        <!--给set赋值-->
        <property name="sets">
            <set>
                <value>111</value>
                <value>222</value>
                <value>222</value>
            </set>
        </property>
    </bean>
    <bean id="address" class="com.mashibing.bean.Address">
        <property name="province" value="河北"></property>
        <property name="city" value="邯郸"></property>
        <property name="town" value="武安"></property>
    </bean>
    <bean id="book2" class="com.mashibing.bean.Book">
        <property name="name" value="JVM"></property>
        <property name="author" value="马士兵"></property>
        <property name="price" value="1200"></property>
    </bean>
    <!--级联属性-->
    <bean id="person2" class="com.mashibing.bean.Person">
        <property name="address" ref="address"></property>
        <property name="address.province" value="北京"></property>
    </bean>
    <!--util名称空间创建集合类型的bean,外部bean如果是map、list这种类型,需要使用util命名空间-->
    <util:map id="myMap">
        <entry key="key1" value="value1"></entry>
        <entry key="key2" value-ref="book2"></entry>
        <entry key="key03">
            <bean class="com.mashibing.bean.Book">
                <property name="name" value="西游记"></property>
                <property name="author" value="吴承恩"></property>
                <property name="price" value="100"></property>
            </bean>
        </entry>
    </util:map>
</beans>
3.5 "继承"其他bean的属性值
  1. ioc.xml
<bean id="person" class="com.mashibing.bean.Person">
    <property name="id" value="1"></property>
    <property name="name" value="zhangsan"></property>
    <property name="age" value="21"></property>
    <property name="gender" value=""></property>
</bean>
<!--parent:指定当前bean的属性值来"继承"于哪个bean-->
<bean id="person2" class="com.mashibing.bean.Person" parent="person">
    <property name="name" value="lisi"></property>
</bean>
  1. 如果不想将某个bean实例化,只用于提供给其他bean继承其属性值,可以使用abstract属性
<!--该bean不会被真正创建对象--> 	
<bean id="person" class="com.mashibing.bean.Person" abstract="true">
    <property name="id" value="1"></property>
    <property name="name" value="zhangsan"></property>
    <property name="age" value="21"></property>
    <property name="gender" value=""></property>
</bean>
<bean id="person2" class="com.mashibing.bean.Person" parent="person">
    <property name="name" value="lisi"></property>
</bean>
3.6 利用外部配置文件
  1. 在resource中添加dbconfig.properties
username=root
password=123456
url=jdbc:mysql://localhost:3306/demo
driverClassName=com.mysql.jdbc.Driver
  1. ioc.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
                           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命名空间
 -->
    <context:property-placeholder location="classpath:dbconfig.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>
        <property name="url" value="${url}"></property>
        <property name="driverClassName" value="${driverClassName}"></property>
    </bean>
</beans>
  1. 利用外部配置文件为属性赋值的坑
    1. spring容器在进行启动时,会读取当前系统的某些环境变量的配置,而当前系统用户名使用username表示的
    2. 如果配置文件中,key起名为username,会导致${username}将环境变量中,主机名读取出来,所以为避免这种情况,配置文件中尽量添加前缀来区分,例如jdbc.username
    3. 如果通过context:property-placeholder,引入了多个配置文件,只有第一个配置文件中的内容会生效,第二个配置文件中内容无法被读取到

4 bean对象创建的依赖关系

  1. bean对象在创建的时候是按照bean在配置文件的顺序决定的

  2. 可以使用depend-on标签来决定顺序

  3. 一般在实际工作中,不必在意bean创建的先后顺序,需要依赖的对象,在创建完成后,都会进行赋值操作,也就是Person中有address属性,即使先创建person对象,再创建address对象,后创建的address也会为person中该属性赋值

  4. ioc.xml

<bean id="book" class="com.mashibing.bean.Book" depends-on="person,address"></bean>
<bean id="address" class="com.mashibing.bean.Address"></bean>
<bean id="person" class="com.mashibing.bean.Person"></bean>

5 bean的作用域

  1. bean的作用域可以通过scope进行设置:request、session为spring 4.x版本内容,表示每次新的请求、会话时,创建一个新的对象,但从来没用过,所以spring 5.x版本中删除
    1. singleton:默认为单例,容器启动完成之前就已经创建好对象,每次获取的对象是同一个
    2. prototype:每次获取bean时,容器启动时不创建,获取对象的时候创建新对象,每次获取的对象都不同
    3. request
    4. session
  2. ioc.xml
<bean id="person4" class="com.mashibing.bean.Person" scope="prototype"></bean>

6 利用工厂模式创建bean对象

  1. 在之前的案例中,所有bean对象的创建都是通过反射得到对应的bean实例,其实在spring中还包含另外一种创建bean实例的方式,就是通过工厂模式进行对象的创建

  2. 在利用工厂模式创建bean实例的时候有两种方式

    1. 静态工厂:工厂本身不需要创建对象,但是可以通过静态方法调用,对象=工厂类.静态工厂方法名()
    2. 实例工厂:工厂本身需要创建对象,工厂类 工厂对象=new 工厂类;工厂对象.get对象名()
  3. PersonStaticFactory.java

package com.mashibing.factory;

import com.mashibing.bean.Person;

public class PersonStaticFactory {

    public static Person getPerson(String name){
        Person person = new Person();
        person.setId(1);
        person.setName(name);
        return person;
    }
}
  1. ioc.xml
<!--
class:指定静态工厂类
factory-method:指定哪个方法是工厂方法
-->
<bean id="person5" class="com.mashibing.factory.PersonStaticFactory" factory-method="getPerson">
    <!--constructor-arg:可以为方法指定参数-->
    <constructor-arg value="lisi"></constructor-arg>
</bean>
  1. SpringDemoTest
Person person = (Person) context.getBean("person5",Person.class);
  1. PersonInstanceFactory.java
package com.mashibing.factory;

import com.mashibing.bean.Person;

public class PersonInstanceFactory {
    public Person getPerson(String name){
        Person person = new Person();
        person.setId(1);
        person.setName(name);
        return person;
    }
}

  1. ioc.xml
<!--实例工厂使用-->
<!--需要先创建实例工厂类-->
<bean id="personInstanceFactory" class="com.mashibing.factory.PersonInstanceFactory"></bean>
<!--
    factory-bean:指定使用哪个工厂实例
    factory-method:指定使用工厂实例的哪个方法
    -->
<bean id="person6" class="com.mashibing.bean.Person" factory-bean="personInstanceFactory" factory-method="getPerson">
    <constructor-arg value="wangwu"></constructor-arg>
</bean>
  1. SpringDemoTest
Person person = (Person) context.getBean("person6",Person.class);

7 继承FactoryBean来创建对象

  1. FactoryBean是Spring规定的一个接口,当前接口的实现类,Spring都会将其作为一个工厂
  2. ioc容器启动的时候不会创建实例,只有在使用的时候才会创建对象
  3. MyFactoryBean.java
package com.mashibing.factory;

import com.mashibing.bean.Person;
import org.springframework.beans.factory.FactoryBean;

/**
 * 实现了FactoryBean接口的类是Spring中可以识别的工厂类,spring会自动调用工厂方法创建实例
 */
public class MyFactoryBean implements FactoryBean<Person> {

    /**
     * 工厂方法,返回需要创建的对象
     * @return
     * @throws Exception
     */
    @Override
    public Person getObject() throws Exception {
        Person person = new Person();
        person.setName("maliu");
        return person;
    }

    /**
     * 返回创建对象的类型,spring会自动调用该方法返回对象的类型
     * @return
     */
    @Override
    public Class<?> getObjectType() {
        return Person.class;
    }

    /**
     * 创建的对象是否是单例对象
     * @return
     */
    @Override
    public boolean isSingleton() {
        return false;
    }
}
  1. ioc.xml
<bean id="myfactorybean" class="com.mashibing.factory.MyFactoryBean"></bean>
  1. SpringDemoTest
Person person = (Person) context.getBean("myfactorybean",Person.class);
System.out.println(person);

8 bean对象的初始化和销毁方法

  1. 可以指定创建对象和销毁对象时,所调用的方法
  2. Address.java
package com.mashibing.bean;

public class Address {
    private String province;
    private String city;
    private String town;

    public Address() {
        System.out.println("address被创建了");
    }

    public Address(String province, String city, String town) {
        this.province = province;
        this.city = city;
        this.town = town;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getTown() {
        return town;
    }

    public void setTown(String town) {
        this.town = town;
    }

    public void init(){
        System.out.println("对象被初始化");
    }
    
    public void destory(){
        System.out.println("对象被销毁");
    }
    
    @Override
    public String toString() {
        return "Address{" +
                "province='" + province + '\'' +
                ", city='" + city + '\'' +
                ", town='" + town + '\'' +
                '}';
    }
}
  1. ioc.xml
<!--bean生命周期:即bean的创建到销毁
	1. init-method:指定对象创建后调用的方法
	2. destroy-method:指定对象销毁前调用的方法
	3. 如果bean是单例,容器在启动的时候会创建好,关闭的时候会销毁创建的bean,且由于对象的创建和销毁都是由容器控制的,所以init-method和destroy-method的功能会生效
	4. 如果bean是多例,对象的创建是由容器控制的,获取的时候创建对象,但销毁并不由容器控制,而是由垃圾回收器决定,因此只有init-method生效,destroy-method不生效
  -->
<bean id="address" class="com.mashibing.bean.Address" init-method="init" destroy-method="destory"></bean>
  1. MyTest.java
import com.mashibing.bean.Address;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("ioc2.xml");
        Address address = context.getBean("address", Address.class);
        System.out.println(address);
        //applicationContext没有close方法,需要使用具体的子类
        //关闭容器的方法
        ((ClassPathXmlApplicationContext)context).close();

    }
}

9 配置bean对象初始化方法的前后处理方法

  1. spring中存在一个名为BeanPostProcessor的接口
  2. 如果将实现了该接口的类,交给spring管理(配置为bean),那么调用任何其他bean的初始化方法之前,都会调用该实现类的postProcessBeforeInitialization方法,初始化之后,又会调用该实现类的postProcessAfterInitialization方法
  3. 即使bean中未配置初始化方法,那么会在对象调用后,依次调用postProcessBeforeInitialization、postProcessAfterInitialization
  4. 如果只想对某些bean生效,那么需要手动在postProcessBeforeInitialization、postProcessAfterInitialization方法中,判断传进来的bean对象类型,然后根据不同类型决定如何处理
  5. MyBeanPostProcessor.java
package com.mashibing.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {
    /**
     * 在初始化方法调用之前执行
     * @param bean  初始化的bean对象
     * @param beanName  xml配置文件中的bean的id属性
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization:"+beanName+"调用初始化前置方法");
        return bean;
    }

    /**
     * 在初始化方法调用之后执行
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization:"+beanName+"调用初始化后缀方法");
        return bean;
    }
}
  1. ioc.xml
<bean id="myBeanPostProcessor" class="com.mashibing.bean.MyBeanPostProcessor"></bean>

10 spring创建第三方bean对象

  1. 在Spring中,很多对象都是单实例的,在日常的开发中,我们经常需要使用某些外部的单实例对象,例如数据库连接池,下面我们来讲解下如何在spring中创建第三方bean实例
  2. 导入数据库连接池的pom文件
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.21</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>
  1. ioc.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="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/demo"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    </bean>
</beans>
  1. MyTest.java
import com.alibaba.druid.pool.DruidDataSource;
import com.mashibing.bean.Address;
import com.mashibing.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.SQLException;

public class MyTest {
    public static void main(String[] args) throws SQLException {
        ApplicationContext context = new ClassPathXmlApplicationContext("ioc3.xml");
        DruidDataSource dataSource = context.getBean("dataSource", DruidDataSource.class);
        System.out.println(dataSource);
        System.out.println(dataSource.getConnection());
    }
}

11 spring基于xml文件的自动装配

  1. 当一个对象中的属性需要指向另外一个对象的时候,在之前的配置中都是通过property标签来进行手动配置的,其实在spring中还提供了一个非常强大的功能就是自动装配,可以按照我们指定的规则自动为对象的属性赋值

  2. 使用autowire进行自动装配

    1. default/no:不自动装配
    2. byName:按setter方法名的首字母小写,去ioc容器中查找是否有同名的bean,如果找不到装配null,如果压根没有setter方法,自然而然也是装配null
    3. byType:按照类型进行装配,以属性的类型作为查找依据,去ioc容器中查找是否有相同类型的bean
      1. 如果找不到则装配null
      2. 如果有多个类型相同的bean对象,那么会抛出异常
      3. 如果Person中有List和Address类型的属性,而List中又存放了Address类型的对象,那么List中的Address和Person中的Address都会被自动装配成功
      4. 发现使用byType自动装配Person时,最终打印的Person对象中,包含大量的环境变量信息,这是因为spring启动时,会将环境变量信息,创建成一个Map<String,Object>类型以及Properties的bean对象,并对其进行初始化,而Person中,恰好包含Map<String,Object>类型的属性maps和Properties类型的属性properties,当使用byType自动装配时,这些环境变量所在的bean对象,被自动装配进了Person的这两个属性中
    4. constructor:按照构造器进行装配
      1. 如果想装配成功,那么该构造器中的所有形参对应的类型,必须都在ioc容器中存在
      2. 如果按照类型找到了多个,那么就会查找哪个bean的名和构造器中形参名完全匹配,找到就装配,找不到就装配失败
  3. ioc.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="address" class="com.mashibing.bean.Address">
        <property name="province" value="河北"></property>
        <property name="city" value="邯郸"></property>
        <property name="town" value="武安"></property>
    </bean>
    <bean id="person" class="com.mashibing.bean.Person" autowire="byName"></bean>
    <bean id="person2" class="com.mashibing.bean.Person" autowire="byType"></bean>
    <bean id="person3" class="com.mashibing.bean.Person" autowire="constructor"></bean>
</beans>

12 SpEL的使用

  1. SpEL:全称Spring Expression Language,是spring的表达式语言,是一个支持运行时查询和操作对象图的强大的表达式语言,类似jsp中的EL、JSTL表达式
  2. 使用#{…}作为语法规则,所有的大括号中的字符都认为是SpEL,之前context表达式中,取配置文件中的属性值使用${},注意与SpEL区分
  3. ioc.xml
<bean id="person4" class="com.mashibing.bean.Person">
    <!--支持任何运算符-->
    <property name="age" value="#{12*2}"></property>
    <!--可以引用其他bean的某个属性值-->
    <property name="name" value="#{address.province}"></property>
    <!--引用其他bean-->
    <property name="address" value="#{address}"></property>
    <!--调用静态方法-->
    <property name="hobbies" value="#{T(java.util.UUID).randomUUID().toString().substring(0,4)}"></property>
    <!--调用非静态方法-->
    <property name="gender" value="#{address.getCity()}"></property>
</bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值