Spring framework(三)SpringBean

二、SpringBean

由Spring IoC容器管理的对象称为Bean,Bean根据Spring配置文件中的信息创建。
Spring配置文件支持两种格式,即XML文件格式和Properties文件格式。

  • Properties:key-value,赋值和取值,适用简单的属性配置。
  • XML:树形结构,但是内容较为繁琐,适用于大型复杂项目。
    Beans.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-3.0.xsd">
    <bean id="helloWorld" class="com.jsonliu.bean.HelloWorld">
        <property name="message" value="Hello World!" />
    </bean>
</beans>

2.1、元素的常用属性

属性名称描述
idBean 的唯一标识符,Spring 容器对 Bean 的配置和管理都通过该属性完成。id 的值必须以字母开始,可以使用字母、数字、下划线等符号。
namename 属性中可以为 Bean 指定多个名称,每个名称之间用逗号或分号隔开。Spring 容器可以通过 name 属性配置和管理容器中的 Bean。
class该属性指定了 Bean 的具体实现类,它必须是一个完整的类名,即类的全限定名。
scope用于设定 Bean 实例的作用域,属性值可以为 singleton(单例)、prototype(原型)、request、session 和 global Session。其默认值是 singleton
constructor-arg元素的子元素,可以使用此元素传入构造参数进行实例化。该元素的 index 属性指定构造参数的序号(从 0 开始),type 属性指定构造参数的类型
property元素的子元素,用于调用 Bean 实例中的 setter 方法来属性赋值,从而完成依赖注入。该元素的 name 属性用于指定 Bean 实例中相应的属性名
ref 和 等元素的子元索,该元素中的 bean 属性用于指定对某个 Bean 实例的引用
value 和 等元素的子元素,用于直接指定一个常量值
list用于封装 List 或数组类型的依赖注入
set用于封装 Set 类型的依赖注入
map用于封装 Map 类型的依赖注入
entry 元素的子元素,用于设置一个键值对。其 key 属性指定字符串类型的键值,ref 或 value 子元素指定其值
init-method容器加载 Bean 时调用该方法,类似于 Servlet 中的 init() 方法
destroy-method容器删除 Bean 时调用该方法,类似于 Servlet 中的 destroy() 方法。该方法只在 scope=singleton 时有效
lazy-init懒加载,值为 true,容器在首次请求时才会创建 Bean 实例;值为 false,容器在启动时创建 Bean 实例。该方法只在 scope=singleton 时有效

2.2、Spring Bean的作用域

作用域:如果每次获取Bean时,都需要一个Bean实例,那么应该将Bean的Scope属性定义为prototype,如果需要每次都返回一个相同的Bean实例,则应将Bean的scope属性定义为singleton。
作用域种类:

  1. singleton:默认值,单例模式,表示在Spring容器中只有一个Bean实例,Bean以单例方式存在。
  2. prototype:原型模式,表示每次通过Spring容器获取Bean时,容器都会创建一个Bean实例。
  3. request:每次HTTP请求时,容器都会创建一个Bean实例。该作用域只在当前HTTP Request内有效。
  4. session:同一个HTTP Session共享一个Bean实例,不同的Session使用不同的Bean实例。该作用域仅在当前HTTP Session内有效。
  5. application:同一个web应用共享一个Bean实例,该作用域在当前ServletContext内有效。类似于singleton,不同的是,singleton表示每个IoC容器中仅有一个Bean实例,而同一个Web应用找那个可能会有多个IoC容器,但一个Web应用中只会有一个SevletContext,application才是web应用中货真价实的单例模式。
  6. websocket:在整个websocket中有效。

2.3、singleton

Spring容器默认的作用域。当Bean的作用域为singleton时,Spring容器中只会存在一个共享的Bean实例。该Bean实例将存储在高速缓存中,并且对Bean的请求,只要id与该Bean定义匹配,都会返回该缓存对象。

通常情况,这种单例模式对于无会话状态的Bean(dao、service),是理想选择。
在spring配置文件中,配置方式:

<bean id="..." class="..." scope="singleton"/>

例1:

public class HelloWorld {

    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public void getMessage() {
        System.out.println("message : " + message);
    }
}
public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");

        HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
        objA.setMessage("对象A");
        objA.getMessage();

        HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
        objB.getMessage();

    }
}

<?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-3.0.xsd">

    <bean id="helloWorld" class="com.jsonliu.bean.HelloWorld" scope="singleton">
    </bean>

</beans>

运行结果
在这里插入图片描述

2.4、prototype

Spring容器会在每次请求该Bean时都创建一个新的Bean实例。prototype作用域适用于需要保持会话状态的Bean.
配置方式

<bean id="..." class="..." scope="prototype"/>

例2:

修改例1中Beans.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-3.0.xsd">
    <bean id="helloWorld" class="net.biancheng.HelloWorld" scope="prototype"/>
      
</beans>

运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑谈子云亭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值