Spring学习(一)--- Spring的IOC

目录

 

一、资料 

二、IOC的含义:

三、Spring环境Ioc前期准备

四、基础知识

1、 ApplicationContext的三个常用实现类  

2、BeanFactory和ApplicationContext的区别

3、把对象的创建交给spring来管理---spring对bean的管理细节   

3.1 创建Bean的三种方式:

3.2  bean的作用范围调整       

3.3 bean对象的生命周期       


一、资料 

程序的耦合
        耦合:程序间的依赖关系
            包括:
                类之间的依赖
                方法间的依赖
        解耦:
            降低程序间的依赖关系
        实际开发中:
            应该做到:编译期不依赖,运行时才依赖。
        解耦的思路:
            第一步:使用反射来创建对象,而避免使用new关键字。
            第二步:通过读取配置文件来获取要创建的对象全限定类名

    Bean:在计算机英语中,有可重用组件的含义。
   JavaBean:用java语言编写的可重用组件。
        javabean >  实体类

二、IOC的含义:

三、Spring环境Ioc前期准备

1、第一步:建立maven工程,导入Spring需要的依赖(配置pom.xml中的Spring)

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>

2、第二步:创建配置文件(例如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">

</beans>

3、把对象的创建交给Spring来管理

<?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="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>

    <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"></bean>
</beans>

 4、获取Spring的Ioc核心容器,并根据id获取对象

    public static void main(String[] args) {
        //1.获取核心容器对象
        // 因为bean.xml在resources目录下,相当于在系统根目录下,所以不需要加路径
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.根据id获取Bean对象(两种方式)
        IAccountService as  = (IAccountService)ac.getBean("accountService");
        IAccountDao adao = ac.getBean("accountDao",IAccountDao.class);

        System.out.println(as);
        System.out.println(adao);
       
    }

运行结果: 

四、基础知识

1、 ApplicationContext的三个常用实现类
         

        ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。(更常用)

        FileSystemXmlApplicationContext:它可以加载磁盘任意路径下的配置文件(必须有访问权限)

        AnnotationConfigApplicationContext:它是用于读取注解创建容器的。

2、BeanFactory和ApplicationContext的区别

        ApplicationContext:   单例对象适用------采用此接口
        它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。也就是说,只要一读取完配置文件马上就创建配置文件中配置的对象。
 
        BeanFactory:  多例对象使用
        它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才真正的创建对象。

3、把对象的创建交给spring来管理---spring对bean的管理细节
   

    3.1 创建bean的三种方式
    3.2 bean对象的作用范围
    3.3 bean对象的生命周期

3.1 创建Bean的三种方式:

    第一种方式:使用默认构造函数创建
    在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。
    采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。 

<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>

例如:

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">
 
    <!-- 第一种方式:使用默认构造函数创建。
            在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。
            采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
    -->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>

</beans>

Client.java

/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        //1.获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.根据id获取Bean对象
        IAccountService as  = (IAccountService)ac.getBean("accountService");
        as.saveAccount();
    }
}

AccountServiceImpl.java

/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl implements IAccountService {
    //默认构造函数
    public AccountServiceImpl(){
        System.out.println("对象创建了");
    }

    public void  saveAccount(){
        System.out.println("service中的saveAccount方法执行了。。。");
    }

}

第二种方式: 使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
(某个方法的返回值存入Spring容器中 

例如:假如Instancefactory是一个jar包,想要使用Instancefactory类中的getAccountService方法获取对象,如果使用第一种方法,获取的是Instancefactory对象,而不是AccountServiceImpl对象。

Instancefactory.java:

/**
 * 模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造函数)
 */
public class InstanceFactory {
    public IAccountService getAccountService(){
        return new AccountServiceImpl();
    }
}

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

    <!-- 第二种方式: 使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器) -->
    <bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
    <bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
</beans>

 Client.java :

/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        //1.获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.根据id获取Bean对象
        IAccountService as  = (IAccountService)ac.getBean("accountService");
        as.saveAccount();
    }
}

AccountServiceImpl.java :

/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl implements IAccountService {
    //默认构造函数
    public AccountServiceImpl(){
        System.out.println("对象创建了");
    }

    public void  saveAccount(){
        System.out.println("service中的saveAccount方法执行了。。。");
    }

}

第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器) 

(某个方法的返回值存入Spring容器中 

StaticFactory.java:

/**
 * 模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造函数)
 */
public class StaticFactory {

    public static IAccountService getAccountService(){

        return new AccountServiceImpl();
    }
}

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

     <!-- 第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器)-->
    <bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>
</beans>

 Client.java :

/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        //1.获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.根据id获取Bean对象
        IAccountService as  = (IAccountService)ac.getBean("accountService");
        as.saveAccount();
    }
}

AccountServiceImpl.java :

/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl implements IAccountService {
    //默认构造函数
    public AccountServiceImpl(){
        System.out.println("对象创建了");
    }

    public void  saveAccount(){
        System.out.println("service中的saveAccount方法执行了。。。");
    }

}

 

3.2  bean的作用范围调整
       

bean标签的scope属性:
            作用:用于指定bean的作用范围
            取值: 常用的就是单例的和多例的
                singleton:单例的(默认值)
                prototype:多例的
                request:作用于web应用的请求范围
                session:作用于web应用的会话范围
                global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session
 

<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="prototype"></bean>

 init-method:指定类中的初始化方法名称

 destroy-method:指定类中销毁方法名称

<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
          scope="prototype" init-method="init" destroy-method="destroy"></bean>

3.3 bean对象的生命周期
       

        单例对象
            出生:当容器创建时对象出生
            活着:只要容器还在,对象一直活着
            死亡:容器销毁,对象消亡
            总结:单例对象的生命周期和容器相同
        多例对象
            出生:当我们使用对象时spring框架为我们创建
            活着:对象只要是在使用过程中就一直活着。
            死亡:当对象长时间不用,且没有别的对象引用时,由Java的垃圾回收器回收 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值