Spring

93 阅读2分钟

一、报错记录

  • 不支持选项5

就是 Maven的版本不正确导致的。参考链接 不支持选项5

二、基础

1、Bean

bean 的核心概念就是减少耦合、尽量避免在class 里面 new 出来对象。 通过一个 ISO 容器来管理new 出来的对象。默认是单利对象。可以通过属性修改 scope 进行修改

bean ISO 基础框架

1、添加Spring依赖

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.22.RELEASE</version>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

2、创建对象、配置bean文件

package com.jince;

public class Person {
    public void eat() {
        System.out.println("person eat ...");
    }
}
<?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="person" class="com.jince.Person"></bean>
</beans>

3、获取对象

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    Person person = (Person) context.getBean("person");
    person.eat();
}

bean DI 注入

<!--    集合注入 -->
    <bean id="person" class="com.jince.Person">
        <property name="contentList">
            <array>
                <value>100</value>
                <value>120</value>
                <value>130</value>
            </array>
        </property>
    </bean>


<!--    set 注入 -->
    <bean id="personServer" name="personServerName,personServerNam2" class="com.jince.PersonServer" scope="prototype">
        <property name="serverPerson" ref="person"/>
        <property name="number" value="111"/>
    </bean>

<!--    构造器注入 -->
    <bean id="personServer" name="personServerName,personServerNam2" class="com.jince.PersonServer" scope="prototype">
        <constructor-arg name="serverPerson" ref="person"/>
        <constructor-arg name="number" value="1131"/>
    </bean>

<!--    自动装配、根据类型
根据类型:必须保证相同类型的bean唯一、推荐使用
根据名称:必须保证bean中有指定名称的bean。有耦合、不推荐使用
-->
<bean id="personServer" name="personServerName,personServerNam2" class="com.jince.PersonServer" scope="prototype" autowire="byType"></bean>

bean 工厂模式

工厂模式可以分为三种。 静态方法、成员变量、和接口工厂。最后一种是比较实用的。 因为他可以通过接口自动处理返回值、bean配置也比较简单、而且可以处理单利 如下 1、静态工厂模式

public class PersonStaticFactory {

    public static Person getPerson() {
        System.out.println("static make a person");
        return new Person();
    }
}
<bean id="staticPersonFactory" class="com.jince.PersonStaticFactory" factory-method="getPerson"></bean>

2、成员工厂

public class PersonPropertyFactory {

    public Person getPerson() {
        System.out.println("PersonPropertyFactory make a person");
        return new Person();
    }
}
<bean id="propertyFactory" class="com.jince.PersonPropertyFactory"></bean>
<bean id="propertyFactoryPerson" factory-bean="propertyFactory" factory-method="getPerson"></bean>

3、接口工厂

import org.springframework.beans.factory.FactoryBean;

public class PersonFactoryBean implements FactoryBean<Person> {

    @Override
    public Person getObject() throws Exception {
        System.out.println("bean make a person");
        return new Person();
    }

    @Override
    public Class<?> getObjectType() {
        return Person.class;
    }

    // 控制是否是单利
    @Override
    public boolean isSingleton() {
        return false;
    }
}
<bean id="personFactoryBean" class="com.jince.PersonFactoryBean"></bean>

这种是最实用的。重点掌握

bean 生命周期

1、通过配置 bean 方法来标注、哪个是初始化方法、哪个是销毁的方法

public class Person {

    public void eat() {
        System.out.println("person eat ...");
    }

    public void init() {
        System.out.println("person bean have init ....");
    }

    public void destory() {
        System.out.println("person bean have destory");
    }
}
<bean id="person" class="com.jince.Person" init-method="init" destroy-method="destory"></bean>

使用 ClassPathXmlApplicationContext 就会调用 destory。

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

// 基础iso框架
Person person = (Person) context.getBean("person");
person.eat();

// 强制关闭
context.close();

或者通过注册钩子方法来销毁也是可以的。如下

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.registerShutdownHook(); // 钩子

// 基础iso框架
Person person = (Person) context.getBean("person");
person.eat();

2、通过接口来控制生命周期。就不用在通过 xml 进行指定了。 如下

public class Person implements InitializingBean, DisposableBean {
    public void eat() {
        System.out.println("person eat ...");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("destroy .... ");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet .... ");
    }
}
<bean id="person" class="com.jince.Person"></bean>

加载properties

1、增加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">

2、加载properties文件

<context:property-placeholder location="jdbc.properties"/>

3、加载属性

<bean id="personServer" name="personServerName,personServerNam2" class="com.jince.PersonServer" scope="prototype">
    <property name="serverPerson" ref="person"/>
    <property name="userName" value="${jdbc.username}"/>
</bean>

4、给你看一下我的 properties

jdbc.username = root

Bean 容器总结

<bean
id="id"
name="name"
class="com.jince.Person"
scope="prototype" // 是否是单利、默认是的
init-method="eat" // 初始化方法
destroy-method="destroy" // 绑定销毁方法
autowire="byType" // 自动装配
factory-method="aaa"
factory-bean="com.jince.book"
lazy-init="true" // 是否懒加载
></bean>

三种加载bean的方式

Person person = (Person) context.getBean("person");
Person person = context.getBean(Person.class); // 推荐使用
Person person = context.getBean("person", Person.class);

两种加载context的方式

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext context1 = new FileSystemXmlApplicationContext("绝对路径.xml")