4、深入理解Bean

前端之家收集整理的这篇文章主要介绍了4、深入理解Bean前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
本节知识点:
1. Bean 的自动装配(了解)
2. bean 之间的关系:继承;依赖
3.Bean 的作用域:可以在 <bean> 元素的 scope 属性里设置 Bean 的作用域
4.使用外部属性文件
5. SpEL:Spring 3.x 引入的新特性,用的不多,了解。


Bean配置总结:

1. Bean 的自动装配(了解):

0). Bean:

public class Dao {

private String database;

public void setDatabase(String database) {
this.database = database;
}
public void save(){
System.out.println("save to " + database);
}

}

public class Service {

private Dao dao;

public void setDao(Dao dao) {
this.dao = dao;
}
public Dao getDao() {
return dao;
}
public void add(){
System.out.println("add...");
dao.save();
}

}

public class Action {

private Service service = null;

public void setService(Service service) {
this.service = service;
}
public Service getService() {
return service;
}
public void execute(){
System.out.println("execute...");
service.add();
}

}

1).Spring 可以自动的装配 bean 的属性。例如,下面的 bean 没有进行属性引用的配置,其装配可以交给 Spring 的 IOC 容器

<bean id="dao" class="com.atguigu.spring.autowire.Dao">
<property name="database" value="DB2"/>
</bean>
在实际的项目中很少使用自动装配功能,因为和自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力一些

<bean id="service" class="com.atguigu.spring.autowire.Service" />
<bean id="action" class="com.atguigu.spring.autowire.Action" />

2). 在 <bean> 的autowire 属性里指定自动装配的模式

①.byType:根据类型自动装配
<bean id="dao" class="com.atguigu.spring.autowire.Dao">
<property name="database" value="DB2"/>
</bean>

<bean id="service3" class="com.atguigu.spring.autowire.Service" autowire="byType"/>

<bean id="action" class="com.atguigu.spring.autowire.Action" autowire="byType"/>

注意:若 IOC 容器中有多个与目标 Bean 类型一致的 Bean,Spring 将无法判定哪个 Bean 最合适该属性,所以不能执行自动装配.

<bean id="service2" class="com.atguigu.spring.autowire.Service" autowire="byName"/>
<bean id="service3" class="com.atguigu.spring.autowire.Service" autowire="byName"/>

<bean id="action" class="com.atguigu.spring.autowire.Action" autowire="byType"/>

将会抛出:NoUniqueBeanDefinitionException异常。

②.byName(根据名称自动装配):必须将目标 Bean 的名称属性名设置的完全相同.

<bean id="service" class="com.atguigu.spring.autowire.Service" autowire="byName"/>
<bean id="service3" class="com.atguigu.spring.autowire.Service" autowire="byName"/>

<bean id="action" class="com.atguigu.spring.autowire.Action" autowire="byName"/>

3). 在实际的项目中很少使用自动装配功能,因为和自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力一些

-----------------------------------------------------------------------------------------------------------------------------------

2.Bean 之间的关系:继承;依赖

1). 继承:并不是指 面向对象的继承,而是指 bean 的配置的继承。

①. 可以通过 bean 的 parent 属性继承其他 bean 的配置,在当前 bean 中可以覆盖继承的 bean 的属性

<beanid="car"class="com.atguigu.spring.relation.Car"abstract="true">
<property name="brand" value="DasAuto"></property>
<property name="corp" value="ShangHai"></property>
<property name="maxSpeed" value="200"></property>
<property name="price" value="100000"></property>
</bean>

<!-- 配置一个 car 的 bean,要求 brand,corp 和 class 和上面的 car 的配置都相同 -->
<bean id="car2"parent="car">
<property name="corp" value="YiQi"></property>
<property name="maxSpeed" value="220"></property>
<property name="price" value="110000"></property>
</bean>

2). 若只想把父 Bean 作为模板,可以设置 <bean> 的abstract 属性为 true,这样 Spring 将不会实例化这个 Bean

<bean id="car" class="com.atguigu.spring.relation.Car" abstract="true">

3). bean 的依赖关系:Spring 允许用户通过 depends-on 属性设定 Bean 前置依赖的Bean,前置依赖的 Bean 会在本 Bean 实例化之前创建好

<bean id="action" class="com.atguigu.spring.autowire.Action" autowire="byName" depends-on="service"/>



-----------------------------------------------------------------------------------------------------------------------------------

3. Bean 的作用域:可以在 <bean> 元素的 scope 属性里设置 Bean 的作用域.

1). 默认情况下,Spring 只为每个在 IOC 容器里声明的 Bean 创建唯一一个实例,整个 IOC 容器范围内都能共享该实例:
所有后续的 getBean() 调用和 Bean 引用都将返回这个唯一的 Bean 实例.该作用域被称为 singleton,它是所有 Bean 的默认作用域.

2). 若把 scope 的值设置为prototype 原型,则意为每次 getBean() 方法都会返回一个新的实例。

<bean id="car2" parent="car" scope="prototype">
<property name="corp" value="YiQi"></property>
<property name="maxSpeed" value="220"></property>
<property name="price" value="110000"></property>
</bean>

3).注意:若 scope 为 singleton,则 IOC 容器创建时,IOC 容器即创建 Bean 的实例;
若 scope 为 prototype,则在 IOC 容器创建时,不再创建 Bean 的实例,而是每次调用 getBean 方法时才创建 Bean 的实例

4).内部 Bean 默认情况下也是单例的


-----------------------------------------------------------------------------------------------------------------------------------
4. 使用外部属性文件

1). 必要性:在配置文件里配置 Bean 时,有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径,数据源配置信息等).
而这些部署细节实际上需要和 Bean 配置相分离

2). 具体步骤:

①. 编写属性文件:在 classpath 下新建 db.properties,并键入如下键值对:
user=root
password=1230
jdbcurl=jdbc:MysqL:///test
driverclass=com.MysqL.jdbc.Driver
minsize=10
maxsize=20
initsize=10

②. 在 bean 的配置文件中导入 properties 文件

◆加入 context 命名空间

<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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


◆使用 <context:property-placeholder location=""/> 导入属性文件

<context:property-placeholder location="classpath:db.properties"/>

③. 在 bean 的配置文件中通过 ${} 来使用属性文件中的值:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user"value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverclass}"></property>
<property name="jdbcUrl" value="${jdbcurl}"></property>

<property name="minPoolSize" value="${minsize}"></property>
<property name="maxPoolSize" value="${maxsize}"></property>
<property name="initialPoolSize" value="${initsize}"></property>
</bean>


-----------------------------------------------------------------------------------------------------------------------------------
5. SpEL:Spring 3.x 引入的新特性,用的不多,了解。

1). Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言。
语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL

2). 例子:

<bean id="car2" parent="car" scope="prototype">
<property name="corp" value="YiQi"></property>
<property name="maxSpeed" value="220"></property>
<property name="price" value="110000"></property>
</bean>

<bean id="dao2" class="com.atguigu.spring.autowire.Dao">
<property name="database" value="#{car2.toString().toLowerCase()}"></property> </bean> <bean id="service2" class="com.atguigu.spring.autowire.Service"> <property name="dao" value="#{dao2}"></property> </bean> <bean id="action2" class="com.atguigu.spring.autowire.Action"> <property name="service" value="#{service2}"></property> </bean>
原文链接:https://www.f2er.com/javaschema/285190.html

猜你在找的设计模式相关文章