ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2、Spring的IOC标签介绍以及实例

2021-11-17 10:31:26  阅读:105  来源: 互联网

标签:配置文件 Spring bean 标签 context UserService userService IOC public


一、Spring_ioc配置文件bean标签介绍

1、 bean标签

名称:bean
类型:标签
归属:beans标签
作用:定义spring中的资源,受此标签定义的资源将受到spring控制
格式:
    <beans>
        <bean/>
    </beans>
    基本属性:
   <bean id="bean_id" name="beanName1,beanName2" class="ClassName"></bean>

    基本属性解释:
       id:bean的名称,通过id值获取bean
       class:bean的类型
       name:bean的名称,可以通过name值获取bean,用于多人配合时给bean起别名

2、 bean标签下scope属性

名称:scope
类型:属性
归属:bean标签
作用:定义bean的作用范围
格式:
    <bean scope="singleton"></bean>
取值:

    singleton:设定创建出的对象保存在spring容器中,是一个单例的对象
    prototype:设定创建出的对象保存在spring容器中,是一个非单例的对象
    request、session、application、websocket 设定创建出的对象放置在web容器对应的位置

演示scope标签
配置文件spring.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">

        <!--演示scope的值为singleton是,单例创建对象-->
        <bean id="userService" class="com.why.service.impl.UserServiceImpl" scope="singleton"></bean>
        
        <!--演示scope的值为singleton是,单例创建对象-->
        <bean id="userService" class="com.why.service.impl.UserServiceImpl" scope="prototype"></bean>
</beans>

测试scope=singleton属性

package com.why.controller;

import com.why.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author Laity
 * @date 2021年11月16日 9:41
 */
public class UserController {
    public static void main(String[] args) {

        //获取Spring上下文环境 (加载配置文件)
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

        /*
              通过getBean方法得到Spring容器中实例化好的Bean对象 (实例化Bean对象)
              userService代表的是配置文件中bean标签的id属性值(id标识唯一的bean)
         */
        UserService userService = (UserService) context.getBean("userService");
        UserService userService1 = (UserService) context.getBean("userService");
        System.out.println(userService);
        System.out.println(userService1);
    }
}

运行scope="singleton"时结果

运行scope="prototype"时结果

3、 bean生命周期

名称:init-method,destroy-method

类型:属性

归属:bean标签

作用:定义bean对象在初始化或销毁时完成的工作

格式:
    <bean init-method="init" destroy-method="destroy></bean>

取值:bean对应的类中对应的具体方法名

注意事项:
    当scope=“singleton”时,spring容器中有且仅有一个对象,init方法在创建容器时仅执行一次

    当scope=“prototype”时,spring容器要创建同一类型的多个对象,init方法在每个对象创建时均执行一次

    当scope=“singleton”时,关闭容器会导致bean实例的销毁,调用destroy方法一次

    当scope=“prototype”时,对象的销毁由垃圾回收机制gc()控制,destroy方法将不会被执行

测试生命周期

  • 在业务层实现类创建两个类
package com.why.service.impl;
import com.why.service.UserService;

public class UserServiceImpl implements UserService {

    @Override
    public void saveUser() {
        System.out.println("Hello,Spring............");
    }


    public void init(){
        System.out.println("bean初始化");
    }

    public void destroy(){
        System.out.println("bean销毁");
    }
}
  • 配置文件
<?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">

        <!--当前scope为singleton单例时-->
        <bean id="userService" class="com.why.service.impl.UserServiceImpl"
              scope="singleton"
              init-method="init"
              destroy-method="destroy"
        />
</beans>

当配置文件中scope属性为singleton时:

  • 测试代码
public class UserController {
    public static void main(String[] args) {

        //获取Spring上下文环境 (加载配置文件)
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

        /*
              通过getBean方法得到Spring容器中实例化好的Bean对象 (实例化Bean对象)
              userService代表的是配置文件中bean标签的id属性值(id标识唯一的bean)
         */
        UserService userService = (UserService) context.getBean("userService");
        UserService userService1 = (UserService) context.getBean("userService");
        System.out.println(userService);
        System.out.println(userService1);
    }
}

总结:当scope=“singleton”时,spring容器中有且仅有一个对象,init方法在创建容器时仅执行一次

当配置文件中scope属性为prototype时:

  • 配置文件
<?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">

        <!--演示scope的值为singleton是,单例创建对象-->
       <!-- <bean id="userService" class="com.why.service.impl.UserServiceImpl" scope="singleton"></bean>-->
       <!-- <bean id="userService" class="com.why.service.impl.UserServiceImpl" scope="singleton"></bean>-->

        <bean id="userService" class="com.why.service.impl.UserServiceImpl"
              scope="prototype"
              init-method="init"
              destroy-method="destroy"
        />
</beans>
  • 测试代码
public class UserController {
    public static void main(String[] args) {

        //获取Spring上下文环境 (加载配置文件)
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

        /*
              通过getBean方法得到Spring容器中实例化好的Bean对象 (实例化Bean对象)
              userService代表的是配置文件中bean标签的id属性值(id标识唯一的bean)
         */
        UserService userService = (UserService) context.getBean("userService");
        UserService userService1 = (UserService) context.getBean("userService");
        System.out.println(userService);
        System.out.println(userService1);
    }
}
  • 测试结果

总结:当scope=“prototype”时,spring容器要创建同一类型的多个对象,init方法在每个对象创建时均执行一次

思考: 为什么只有init方法运行,destory方法怎么没有运行?

其实destory是有运行的,但是由于程序结束的太快虚拟机以及关闭了,来不及打印,所以就没有显示了。

我们可以通过ClassPathXmlApplicationContext类的close()方法在程序结束之前强制关闭容器,这样就可以看到destroy方法的打印了。

  1. 修改配置文件(scope = singleton)
<?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="userService" class="com.why.service.impl.UserServiceImpl"
              scope="singleton"
              init-method="init"
              destroy-method="destroy"
        />
</beans>
  1. 测试
public class UserController {
    public static void main(String[] args) {

        //获取Spring上下文环境 (加载配置文件)
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

        /*
              通过getBean方法得到Spring容器中实例化好的Bean对象 (实例化Bean对象)
              userService代表的是配置文件中bean标签的id属性值(id标识唯一的bean)
         */
        UserService userService = (UserService) context.getBean("userService");
        UserService userService1 = (UserService) context.getBean("userService");
        System.out.println(userService);
        System.out.println(userService1);

        context.close();
    }
}
  1. 结果

当scope=“singleton”时,关闭容器会导致bean实例的销毁,调用destroy方法一次

  • 修改配置文件(scope = prototype)
<?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="userService" class="com.why.service.impl.UserServiceImpl"
              scope="prototype"
              init-method="init"
              destroy-method="destroy"
        />
</beans>
  • 测试代码
public class UserController {
    public static void main(String[] args) {

        //获取Spring上下文环境 (加载配置文件)
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

        /*
              通过getBean方法得到Spring容器中实例化好的Bean对象 (实例化Bean对象)
              userService代表的是配置文件中bean标签的id属性值(id标识唯一的bean)
         */
        UserService userService = (UserService) context.getBean("userService");
        UserService userService1 = (UserService) context.getBean("userService");
        System.out.println(userService);
        System.out.println(userService1);

        context.close();
    }
}
  • 运行结果

当scope=“prototype”时,对象的销毁由垃圾回收机制gc()控制,destroy方法将不会被执行

4、 bean对象创建方式

(1)  factory-bean

    名称:factory-bean

    类型:属性

    归属:bean标签

    作用:定义bean对象创建方式,使用静态工厂的形式创建bean,兼容早期遗留系统的升级工作

    格式:
        <bean class="FactoryClassName" factory-method="factoryMethodName"></bean>
        取值:工厂bean中用于获取对象的静态方法名

    注意事项:

    class属性必须配置成静态工厂的类名

(2)factory-bean,factory-method

    名称:factory-bean,factory-method

    类型:属性

    归属:bean标签

    作用:定义bean对象创建方式,使用实例工厂的形式创建bean,兼容早期遗留系统的升级工作

    格式:
        <bean factory-bean="factoryBeanId" factory-method="factoryMethodName"></bean>
        取值:工厂bean中用于获取对象的实例方法名

    注意事项:
        使用实例工厂创建bean首先需要将实例工厂配置bean,交由spring进行管理
        factory-bean是实例工厂的beanId

4.1 配置静态工厂创建bean

  1. 创建工厂类(工厂方法为静态方法)
package com.why.factory;

/**
 * @author Laity
 * @date 2021年11月17日 9:45
 */
public class UserServiceFactory {

    public static UserService getService(){
        System.out.println("对象是静态工厂创建的");

        //返回需要创建的对象
        return new UserServiceImpl();
    }
}

  1. 修改配置文件(spring.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="userService" class="com.why.factory.UserServiceFactory" factory-method="getService"/>

</beans>
  1. 测试
public class UserController {
    public static void main(String[] args) {

        //获取Spring上下文环境 (加载配置文件)
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

        /*
              通过getBean方法得到Spring容器中实例化好的Bean对象 (实例化Bean对象)
              userService代表的是配置文件中bean标签的id属性值(id标识唯一的bean)
         */
        UserService userService = (UserService) context.getBean("userService");
    }
}
  1. 结果

4.2 实例工厂创建bean

  1. 创建工厂类(工厂方法非静态方法)
public class UserServiceFactory {

    public UserService getService(){
        
        System.out.println("对象是实例化工厂创建的");

        //返回需要实例化的对象
        return new UserServiceImpl();
    }
}
  1. 修改配置文件(spring.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="factoryBean" class="com.why.factory.UserServiceFactory" />
        <bean id="userService" factory-bean="factoryBean" factory-method="getService"/>

</beans>
  1. 测试
public class UserController {
    public static void main(String[] args) {

        //获取Spring上下文环境 (加载配置文件)
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

        /*
              通过getBean方法得到Spring容器中实例化好的Bean对象 (实例化Bean对象)
              userService代表的是配置文件中bean标签的id属性值(id标识唯一的bean)
         */
        UserService userService = (UserService) context.getBean("userService");
        userService.saveUser();
    }
}
  1. 结果

4.3 Spring三种创建bean的方式比较

方式一: bean标签创建,当各个bean的业务逻辑相互比较独立的时候或者和外 界关联较少的时候可以使用。

方式二: 利用静态factory方法创建,可以统一管理各个bean的创建,如各个bean在创建之前需要 相同的初始化处理,则可用这个factory方法险进行统一的处理等等。

方式三: 利用实例化factory方法创建,即将factory方法也作为了业务bean来控制,1可用于集成 其他框架的bean创建管理方法,2能够使bean和factory的角色互换。

开发中,项目一般使用第一种方式实例化bean,交给spring托管,使用时直接拿来使用即可,另外两种了解即可。

标签:配置文件,Spring,bean,标签,context,UserService,userService,IOC,public
来源: https://www.cnblogs.com/cmsdnbog/p/15566039.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有