Spring 进阶

Spring 进阶

Spring中的bean

bean是根据scope来生成,表示bean的作用域,scope有四种类型:

  • ⭐singleton:单例,表示通过Spring容器获取的对象是唯一的,默认值。
  • ⭐prototype:原型,表示通过Spring容器获取的对象是不同的。
  • ⭐request:请求,表示在一次HTTP请求内有效。
  • ⭐session:会话,表示在一个用户会话内有效。
    request、session适用于web项目。
    ⭐singleton模式下,只要加载IoC容器,无论是否从IoC中取出bean,配置文件中的bean都会被创建。
    ⭐prototype模式下,如果不从IoC中取bean,则不创建对象,取一次bean,就会创建一次对象,所以每个对象不一样。
Spring的继承

Spring继承不同于Java中的继承,区别:Java中的继承是针对于类的,Spring的继承是针对于对象(bean)。Spring的继承中,子bean可以继承父bean中所有成员变量的值。而Java继承中,子类只是继承了父类的结构。

<?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="user1" class="com.oyrf.entity.User">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
    </bean>
    <bean id="user2" class="com.oyrf.entity.User" parent="user1">
        <property name="name" value="李四"></property>
    </bean>
</beans>

通过设置bean标签的parent属性建立继承关系,同时子bean可以覆盖父bean的属性值。
Sring的继承是针对对象的,所以子bean和父bean并不需要属于同一个数据类型,只要其成员变量列表一致即可。

Spring的依赖

用来设置两个bean的创建顺序。
IoC容器默认情况下是通过spring.xml中bean的配置顺序来决定创建顺序的,配置在前面的bean会先创建。在不更改spring.xml配置顺序的前提下,通过设置bean之间的依赖关系来调整bean的创建顺序。(depends-on)

<bean id="account" class="com.oyrf.entity.Account" depends-on="user"></bean>
<bean id="user" class="com.oyrf.entity.User"></bean>

上述代码的结果是先创建User,再创建Account。

Spring读取外部资源

实际开发中,数据库的配置一般会单独保存到后缀为properties的文件中,方便维护和修改,如果使用Spring来加载数据源,就需要在spring.xml中读取properties中的数据,这就是读取外部资源。
jdbc.properties

user = root
password = 1234
url = jdbc:mysql://localhost:3306/t_account
driveName = com.mysql.jdbc.Driver

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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!--导入外部资源-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!-- SpEL -->
    <bean id="datasource" class="com.oyrf.entity.DataSource">
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <property name="url" value="${user}"></property>
        <property name="driverName" value="${driveName}"></property>
    </bean>
</beans>
Spring p命名空间

p命名空间可以用来简化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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd"
        xmlns:p="http://www.springframework.org/schema/p">
    <bean id="student" class="com.oyrf.entity.Student" p:id="1" p:name="张三" p:age="22" p:classes-ref="classes">
    </bean>
    <bean id="classes" class="com.oyrf.entity.Classes" p:id="1" p:name="一班"></bean>
</beans>

个人博客:https://oybox.github.io/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

OYBox

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

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

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

打赏作者

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

抵扣说明:

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

余额充值