Spring基本功能-IOC

一、SpringIOC

  Spring的控制反转:把对象的创建,初始化,销毁的过程交给SpringIOC容器来做,由Spring容器控制对象的生命周期。

  1.1 启动Spring容器的方式:

  (1)加载classpath下的spring配置文件。其中xml可以是全路径,也可以是classpath的书写方式,该方式下Spring的配置文件必须放置于classpath路径下。

ApplicationContext ac = new  ClassPathXmlApplicationContext("classpath:jyk.xml");

    (2)加载文件系统下的配置文件。其中xml是存放于某个具体文件目录下的Spring配置文件。

ApplicationContext ac = new FileSystemXmlApplicationContext("d:\jyk.xml");

  1.2 从Spring容器中提取对象的方式。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
    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="person" class="com.jyk.spring.simpletest.Person">
    </bean>
         
</beans>
package com.jyk.spring.simpletest;

public class Person {

    public Person() {
        System.out.println("fuck...............");
    }
    
    public void printInfo(){
        System.out.println("hello..............");
    }
}
package com.jyk.spring.simpletest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
        ApplicationContext ac = new 
                ClassPathXmlApplicationContext("classpath:jyk.xml");
        //根据beanid获取相应的对象并强制转换
        Person p = (Person)ac.getBean("person");
        p.printInfo();
    }
}

二、Spring对象的常用创建方式

  2.1 无参构造函数

<bean id="person" class="com.jyk.spring.simpletest.Person"></bean>

  2.2 有参构造函数

  <!-- 按照方法参数的顺序赋值 -->
    <bean id="person" class="com.jyk.spring.simpletest.Person">
        <constructor-arg index="0" value="tom"></constructor-arg>
        <constructor-arg index="1" value="12"></constructor-arg>
    </bean>
    
    <!-- 按照方法参数的类型赋值 -->
    <bean id="person" class="com.jyk.spring.simpletest.Person">
        <constructor-arg type="java.lang.String" value="tom"></constructor-arg>
        <constructor-arg type="java.lang.Double" value="12"></constructor-arg>
    </bean>
        
    <!-- 按照方法参数的名称赋值 -->
    <bean id="person" class="com.jyk.spring.simpletest.Person">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="12"></constructor-arg>
    </bean> 

  2.3 静态工厂

<bean id="personFactory" class="com.jyk.spring.simpletest.PersonFactory" factory-method="createPerson"> </bean>
package com.jyk.spring.simpletest;

public class PersonFactory {

    public static Person createPerson(){
        return new Person();
    }
}

三、Spring对象的scope 

 3.1 singleton(默认值)

   在Spring容器中一个Bean定义只有一个共享的对象实例。在一个普通的只有beanid和class配置的bean定义中,默认情况下是Spring容器初始化时便初始化该bean。

   但我们可以在bean节点中添加lazy-init="true"来延迟加载bean。这样只有在第一次获取bean时才会有初始化该bean。

<bean id="person" class="com.jyk.spring.simpletest.Person" lazy-init="true"></bean>

  如果想对所有的bean设置延迟初始化,可以在beans节点设置该属性值,如下

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
    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
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        " default-lazy-init="true">

 3.2 prototype

  当一个bean的scope为prototype时,该bean允许被多次创建(每使用一次就实例化一次),Spring无法对prototype的bean的生命周期负责,因此以为着持有prototype的对象并释放的责任,都需由客户端负责。

<bean id="person" class="com.jyk.spring.simpletest.Person" scope="prototype"></bean>

3.3 request

  每次http请求都会有各自的bean实例创建,即一个bean实例对应一次http请求,该作用域仅在基于web的spring applicationcontext场景下有效。

3.4 session

  一个bean实例对应一个http session,该作用域仅在基于web的spring applicationcontext场景下有效。

3.5 global session

四、Spring对象的初始化与销毁

  Spring默认在启动时对所有singleton bean进行实例化,即ApplicationContext提前地创建并配置好所有singleton bean。提前实例化意味着对启动的性能有所影响,但长远来看是好事,因为一旦某个对象的创建出现了问题,在初始化时便能及时发现并修复,而不是等到调用该对象时才报错(lazy-init="false"的情况)。

  Spring初始化和销毁bean时,有时需要人工介入处理一些逻辑,Spring提供了初始化和销毁bean时的自定义方法配置,init-method和destory-method,当容器初始化该bean时调用init方法,当该bean对象中容器中删除时调用destroy方法。

<bean id="person" class="com.jyk.spring.simpletest.Person" init-method="init" destroy-method="destroy"></bean>
原文地址:https://www.cnblogs.com/jiyukai/p/7295349.html