初学Java ssh之Spring 第四篇

时间:2022-05-20 16:45:31

  今天我来学习学习Spring中Bean。

  在Spring中原来还有<beans.../>标签啊,它相当于<bean.../>标签的老爸,老爸可以有很多个儿子,但是老爸只有一个哦。

  也就是说一个<beans.../>标签内可以有多个<bean...>标签,每个<bean.../>标签代表一个java实例。

  定义<bean...>时,需要注意两个属性,一个是id,一个是class。

  id是<bean.../>标签的唯一标示符,容器对其的访问、管理、注入都通过这个属性来完成,所以它也是唯一的。

  class呢,是该bean的具体实现类,因为Spring要通过class来完成对bean的实例化,所以其必须是完整的类名,不能是接口哦!

  还可以用name属性为其增加一个别名。

  当Spring创建一个bean实例时,还为其指定了特定的作用域:

    singleton:单例模式,使用singleton定义的bean只有一个实例。

    prototype:原型模式,每次通过容器的getbean方法去调用prototype定义的bean时,都会创建一个实例。

    request:每次http请求都会产生一个bean实例。

    seesion:与request相似,每次http session请求时,都会产生一个bean实例。

    global session:(这个不太懂啊)每个全局的http session对应一个bean实例。

  不指定作用域时,默认为singleton模式。

  举个例子:<bean id="test" class="..." scope="singleton"/>

  session和request作用域的范围很相似,不同的是session指每次Http Session请求都会有效,而request则是每次Http请求都有效。

  为此,我们需要有两种配置方式:采用Listener或者Filter配置。

  当使用Servlet2.4以上规范的Web容器时,可在Web容器下的web.xml文件中增加如下配置,该Listerner负责为request作用域负责:

 <web-app>
...
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>

  当使用2.4以下规范的就不能用这个配置了,只能改为Filter配置:

 <web-app>
...
<filter>
<filter-name>requestContextFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
<filter-mapping>
<filter-name>requestContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</filter>
...
</web-app>

  一旦配置上面二者之一,就可以在Spring配置文件中使用request和session作用域了。

  

  如果需要为Bean属性值是容器中另一个bean实例,需要使用<ref...>元素,使用该元素会有两个属性:

  bean:引用不在同一个XML文件中的另一个bean实例的id属性值。

  local:引用在同一个XML文件中其他bean实例的id属性值。举一个例子:

 <bean id="A" class="com.basic.test.A"/>
<bean id="B" class="com.basic.test.B">
<property name="c">
<!-- 引用容器中另一个bean-->
<ref local="A"/>
</property>
</bean>

  它还有一种简洁的写法:

 <bean id="A" class="com.basic.test.A"/>
<bean id="B" class="com.basic.test.B">
<property name="c" ref="A"/>
</bean>

  此处简洁了ref元素,将它并入了property的属性中,当然这么写也就不区分ref元素中的local和bean属性了。

  此外,如果bean的属性值是个集合的话,则需要使用集合元素,<list.../>、<set.../>、<map.../>和<props.../>分别用来设置类型为List、Set、Map和Properties的集合属性值。

  下面我们来改造一个之前写的com/sep/basic/service/impl/Lenovo类,将其中包含多个集合属性。

 package com.sep.basic.service.impl;

 import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; import com.sep.basic.service.Computer;
import com.sep.basic.service.Key; public class Lenovo implements Computer{
//下面是集合属性
private List<String> size;
private Set price;
private Map<String , Key> key;
private Properties health;
//private Key key; //各属性的setter方法
public void setSize(List<String> size) {
this.size = size;
} public void setPrice(Set price) {
this.price = price;
} public void setKey(Map<String, Key> key) {
this.key = key;
} public void setHealth(Properties health) {
this.health = health;
} @Override
public void useKey() {
System.out.println(size);
System.out.println(price);
System.out.println(key);
System.out.println(health);
} }

  下面来看一下我们的applicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 将配置Lenovo实例 -->
<bean id="lenovo" class="com.sep.basic.service.impl.Lenovo">
<property name="size">
<!-- 为list属性配置属性值 -->
<list>
<!-- 为每个value、ref、bean配置一个List元素 -->
<value>17'</value>
<value>21'</value>
<value>30'</value>
</list>
</property>
<property name="key">
<!-- 为Set设置属性值 -->
<set>
<!-- 为每个value、ref、bean配置一个Set元素 -->
<value>普通的字符串</value>
<bean class="com.sep.basic.service.impl.LogicTech"/>
<ref local="logicTech"/>
</set>
</property>
<property name="cpu">
<!-- 为Map类型设置属性值 -->
<map>
<!-- 每个entry配置一个key-value对 -->
<entry key="cpu1" value="intel"/>
<entry key="cpu2" value="amd"/>
<entry key="cpu3" value="other"/>
</map>
</property>
<property name="keyType">
<!-- 为Map类型设置属性值 -->
<map>
<!-- 每个entry配置一个key-value对 -->
<entry key="罗技键盘" value-ref="logicTech"/>
</map>
</property>
<property name="health">
<!-- 为properties类型设置属性值 -->
<!-- 为每一个prop元素配置一个属性项,其中key指定属性名 -->
<props>
<prop key="温度">正常</prop>
<prop key="存取速度">正常</prop>
</props>
</property>
</bean>
<!-- 将配置LogicTech实例-->
<bean id="logicTech" class="com.sep.basic.service.impl.LogicTech"/> </beans>

  这样我们就成功地将所有集合属性赋值了。