Acquaintance SpringIOC

Acquaintance SpringIOC

Brief introduction

The IOC (Inversion of Control) control inversion. Access refers to actively obtain the object original manner, the transition to the passive received in the Spring, the IOC is the factory mode decoupling, a container Srping framework for creating and management objects of .

Maven configuration

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>XXXX</groupId>
    <artifactId>XXXX</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <!--依赖版本-->
    <properties>
        <!-- spring版本 -->
        <spring.version>5.0.2.RELEASE</spring.version>
    </properties>

    <!--依赖包-->
    <dependencies>
        <!--spring ioc依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
    </dependencies>
    
</project>

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

    <!--配置service,说明:
        标签:
            bean:配置javaBean对象
        属性:
            id:bean的唯一标识名称
            class:类的全路径信息
        细节:
            默认使用无参数构造方法,创建对象
    -->
    <bean id="service" class="XXXX.serivce"></bean>

    <!--配置dao-->
    <bean id="dao" class="XXXX.dao"></bean>

</beans>

Bean label's role
configuration JavaBean Object
Properties

id: Bean的唯一标识名称
class: 类的全限定名称
scope: 设置Bean的作用范围
    singleton: 单例(默认值)
    prototype: 多例
    request: web项目中,将对象存入request域中
    session: web项目中,将对象存入session域中
    globalsession: web项目中,应用在集群环境,如果没有集群环境,相当于session
init-method: 指定类中初始化方法的名称:,在构造方法执行完毕后立即执行
destroy-method: 指定类中销毁方法名称,在销毁Spring容器前执行

Scope and life cycle

单例对象: 一个应用中只有一个对象实例
    出生: 加载配置文件,容器创建,对象出生
    或者: 只要容器存在,对象一直活着
    死亡: 容器销毁,对象死亡
    
多例对象: 在一次使用过程中
    出生: 第一次获取对象,对象出生
    或者: 在一次使用过程中,对象活着
    死亡: 当对象不再使用或者没被引用,交由垃圾回收器回收

Controller Gets bean objects

package XXXX.controller;

import com.itheima.service.CustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Controller{
    /**
     * 使用Spring框架提供的IOC容器获取对象
     *      1.Spring框架提供了一个大工厂接口:ApplicationContext
     *      2.该工厂接口中提供了一个getBean()方法,用于根据bean的名称获取bean
     *      3.该工厂接口提供了常见的实现类:
     *          3.1 ClassPathXmlApplicationContext: 从类路径下加载bean.xml配置文件,创建Spring的IOC容器
     *          3.2 FileSystemXmlApplicationContext: 从文件系统路径下加载bean.xml配置文件,创建Spring的IOC容器
     */
    public static void main(String[] args){
        // 1.加载Spring配置文件,创建Spring IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        
        // 2.从容器中获取客户service对象
        Service service = (Service)context.getBean("service");
        
        // 3.调用Service中的方法
        service.method();
    }
}

Spring factory class structure

BeanFactory and ApplicationContext difference

1. beanFactory是顶层接口
2. ApplicationContext是子接口
3. 它们最大的区别是创建对象的时间不一样
   - BeanFactory采用的是延迟加载思想(什么时候使用对象,什么时候创建)
   - ApplicationContext采用立即创建思想(一加载配置文件,立即创建)

Spring Bean instantiation

method one

<!-- 无参构造方法实例化 -->
<bean id="dao" class="XXXX.dao.impl.daoImpl" scope="singleton" init-method="init" destroy-method="destroy"></bean>

Second way

/**
 * 使用静态工厂方法实例化
 */
public class StaticFactoryBean{
    /**
     * 注意该方法为静态方法
     */
    public static Dao createDao(){
        return new daoImpl();
    }
}
<!--
    静态工厂方法实例化对象
        id: 唯一标识名称
        class: 类全路径
        factory-method: 指定工厂方法
-->
<bean id="staticDao" class="XXXX.factory.StaticFactoryBean" factory-method="createDao"></bean>

Three ways

/**
 * 使用实例工厂方法实例化
 */
public class InstanceFactoryBean{
    /**
     * 注意该方法为普通方法
     */
    public Dao createDao(){
        return new daoImpl();
    }
}
<!--
    实例工厂方法实例化对象
        配置静态工厂对象
        factory-bean: 指定工厂对象
        facotry-method: 指定工厂方法
-->
<bean id="instanceFactory" class="XXXX.factory.InstanceFactoryBean"></bean>
<bean id="instanceDao" factory-bean="instaceFactory" factory-method="createDao"></bean>

Spring dependency injection

Dependency injection (Dependency Injection), he is the realization of the Spring Framework core IOC, maintenance, application objects and relationships between objects, between the layers, for (dependency injection is the member variables to the class assignment)

Two ways dependent input

Constructor injection

<!--
    构造方法注入(通过构造方法,给类的成员变量赋值)
    标签
        constructor-arg: 指定通过构造方法给成员变量赋值
    属性
        index: 成员变量在构造方法参数列表中的索引
        name: 成员变量的名称(与index使用一个即可)
        type: 成员变量的类型(默认即可)
        value: 给Java简单类型成员变量赋值(八种基本类型+String)
        ref: 给其他Bean类型成员变量赋值
-->
<bean id="constructDao" class="XXXX.dao.impl.ConstructorDaoImpl">
    <constructor-arg index="0" name="id" type="int" value="1"/>
    <constructor-arg name="name" value="姓名"/>
    <constructor-arg name="newDate" ref="now"/>
</bean>
<!-- ref: now -->
<bean id="now" class="java.util.Date"></bean>

c namespace injection

<!--
    c名称空间注入
        导入c名称空间: xmlns:c="http://www.springframework.org/schema/c"
        c:属性名称 ==> 给Java简单类型成员变量赋值
        c:属性名称-ref ==> 给其他Bean类型成员变量赋值
-->
<bean id="cDao" class="XXXX.dao.impl.ConstructorDaoImpl" c:id="4" c:name="姓名" c:newDate-ref="now"></bean>

Set injection method

<!--
    set方法注入数据
        标签
            property: 指定通过set方法,给类的成员变量赋值
        属性
            name: 指定成员变量名称
            value: 给Java简单类型成员变量赋值(八种基本类型+String)
            ref: 给其他Bean类型成员变量赋值
-->
<bean id="setDao" class="XXXX.dao.impl.SetDaoImpl">
    <property name="id" value="2"/>
    <property name="name" value="姓名"/>
    <property name="newDate" ref="now"/>
</bean>

p namespace injection

<!--
    p名称空间注入
        导入p名称空间: xmlns:p="http://www.springframework.org/schema/p"
        p:属性名称 ==> 给Java简单类型成员变量赋值
        p:属性名称-ref ==> 给其他Bean类型成员变量赋值
-->
<bean id="pDao" class="XXXX.dao.impl.SetDaoImpl" p:id="3" p:name="姓名" p:newDate-ref="now"></bean>

A collection of attributes injection

<!--
    集合属性注入
    List结构: array/list/set
    Map结构: map/prop
    数据结构一致,标签可以互换
-->
<bean id="collectionDao" class="XXXX.dao.impl.CollectionDaoImpl">
    <!-- array -->
    <property name="array">
        <array>
            <value>array</value>
        </array>
    </property>
    <!-- list -->
    <property name="list">
        <list>
            <value>list</value>
        </list>
    </property>
    <!-- set -->
    <property name="set">
        <set>
            <value>set</value>
        </set>
    </property>
    <!-- map -->
    <property name="map">
        <map>
            <entry key="map-k" value="map-v"/>
        </map>
    </property>
    <!-- prop -->
    <property name="prop">
        <props>
            <entry key="prop-k">prop-v</entry>
        </props>
    </property>
</bean>

Spring annotation-based configuration IOC

Common Annotations

@Component

作用: 配置JavaBean对象,相当于xml配置方式中的Bean标签
属性: value(给Bean一个唯一标识名称)
细节: value属性可以省略,默认使用类的名称首字母小写作为Bean的名称

由@Component验货的三个注解
    @Controller: 一般用于表现层
    @Service: 一般用于业务层
    @Repository: 一般用于持久层

@Scope

作用: 设置Bean的作用范围,相当于xml配置中Bean标签的scope属性
属性: value(指定作用范围取值)
     属性取值
        singleton: 单例(默认值)
        prototype: 多例
        request:web项目中,把bean对象存入request域中
         session:web项目中,把bean对象存入session域中
         globalsession:web项目中,把bean对象存入全局session域中

@Autowired

作用: 默认按照Bean的类型注入数据
属性: required(指定目标Bean是否必须存在于Spring的IOC容器,true为必须存在(默认值),false为不存在)
细节: 在Spring容器中,如果同一个类型存在多个Bean实例对象,则先按照Bean的类型进行注入,再按照Bean的名称进行匹配,匹配上注入成功,匹配不上注入失败

@Qualifier

作用: 与@Autowired注解一起使用,指定在按照Bean类型注入的基础上,再按照Bean的名称注入
属性: value(指定Bean的名称)
细节
    1. 在类的成员变量上,不能单独属于,需要与@Autowired注解一起使用
    2. 在方法的成员变量上,可以单独使用

@Resource

作用: 默认按照Bean的名称注入数据
属性
    name: 指定Bean的名称注入数据
    type: 指定Bean的类型注入数据
细节: 默认按照Bean的名称注入数据,如果注入失败,再按照Bean的类型注入

@Value

作用: 给Java简单类型成员变量注入数据

Bean associated with the life cycle of notes

@PostConstruct

初始化操作,相当于xml配置方式中Bean标签的init-method属性

@PreDestroy

销毁操作,相当于xml配置方式中Bean标签的destroy-method属性

Guess you like

Origin www.cnblogs.com/unrecognized/p/11621206.html