系列四:Spring框架的使用

源码地址:https://download.csdn.net/download/u013636987/11387400

上一篇:系列三:Spring框架介绍

Spring的相关配置:

id属性和name属性标签的配置

id :Bean起个名字. 在约束中采用ID的约束:唯一. 必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号 id:不能出现特殊字符.

<bean id=”bookAction”>

name:Bean起个名字. 没有采用ID的约束.  name:出现特殊字符.如果<bean>没有id的话 , name可以当做id使用.

scope属性:Bean的作用范围.

* singleton :默认值,单例的.

* prototype :多例的.

* request :WEB项目中,Spring创建一个Bean的对象,将对象存入到request域中.

* session :WEB项目中,Spring创建一个Bean的对象,将对象存入到session域中.

* globalSession :WEB项目中,应用在Porlet环境.如果没有Porlet环境那么globalSession相当于session.

{

为什么用单例、多例:
    之所以用单例,是因为没必要每个请求都新建一个对象,这样子既浪费CPU又浪费内存;
   之所以用多例,是为了防止并发问题;即一个请求改变了对象的状态,此时对象又处理另一个请求,而之前请求对对象状态的改变导致了对象对另一个请求做了错误的处理;
    用单例和多例的标准只有一个:
    当对象含有可改变的状态时(更精确的说就是在实际应用中该状态会改变),则多例,否则单例;
何时用单例?何时用多例?
    对于struts2来说,action必须用多例,因为action本身含有请求参数的值,即可改变的状态;
  而对于STRUTS1来说,action则可用单例,因为请求参数的值是放在actionForm中,而非action中的;
   另外要说一下,并不是说service或dao一定是单例,标准同第3点所讲的,就曾见过有的service中也包含了可改变的状态,同时执行方法也依赖该状态,但一样用的单例,这样就会出现隐藏的BUG,而并发的BUG通常很难重现和查找;

}

Bean的生命周期的配置:

通过配置<bean>标签上的init-method作为Bean的初始化的时候执行的方法,配置destroy-method作为Bean的销毁的时候执行的方法。

销毁方法想要执行,需要是单例创建的Bean而且在工厂关闭的时候,Bean才会被销毁.

Spring的Bean的管理XML的方式:

Spring的Bean的属性注入:

【构造方法的方式注入属性】

<!-- 第一种:构造方法的方式 -->

<bean id="login" class="com.spring.login">

<constructor-arg name="login_id" value="1"/>

</bean>

这样可以代替在构造类里面创建对象 

【set方法的方式注入属性】

<!-- 第二种:set方法的方式 -->

	<!-- 配置客户模块 -->
	<!-- 强调:以后配置Action,必须是多例的 -->
	<!-- 客户功能配置 -->
	<bean id="customerAction" class="com.customer.action.CustomerAction" scope="prototype">
		<property name="customerService" ref="customerService"/>
	</bean>
	<bean id="customerService" class="com.customer.service.CustomerServiceImpl">
		<property name="customerDao" ref="customerDao"/>
	</bean>

 这样action要使用时就可以直接用,而不需要重新new该对象(注意要set)

Spring的属性注入:对象类型的注入:

<!-- 注入对象类型的属性 -->

<!-- ref属性:引用另一个bean的id或name -->
	<bean id="customerDao" class="com.customer.dao.CustomerDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	<bean id="customerService" class="com.customer.service.CustomerServiceImpl">
		<property name="customerDao" ref="customerDao"/>
	</bean>
	<bean id="customerAction" class="com.customer.action.CustomerAction" scope="prototype">
		<property name="customerService" ref="customerService"/>
	</bean>

名称空间p的属性注入的方式:Spring2.x版本后提供的方式.

第一步:引入p名称空间

<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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd">

第二步:使用p名称空间.

    * 普通属性:     p:属性名称=””

    * 对象类型属性: p:属性名称-ref=””

    <!-- 开启事务的注解 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

<!-- p名称空间的属性注入的方式 -->

<bean id="car2" class="com.spring.demo4.Car2" p:name="宝马7" p:price="1200000"/>

<bean id="person" class="com.spring.demo4.Person" p:name="思聪" p:car2-ref="car2"/>

SpEL的方式的属性注入:Spring3.x版本后提供的方式.

SpEL:Spring Expression Language.

语法:#{ SpEL }

<!-- SpEL的注入的方式 -->

<bean id="car2" class="com.spring.demo4.Car2">

<property name="name" value="#{'奔驰'}"/>

<property name="price" value="#{800000}"/>

</bean>

    <bean id="person" class="com.spring.demo4.Person">

     <property name="name" value="#{'冠希'}"/>

     <property name="car2" value="#{car2}"/>

    </bean>

<bean id="carInfo" class="com.spring.demo4.CarInfo"></bean>

  引用了另一个类的属性

<bean id="car2" class="com.spring.demo4.Car2">

<!-- <property name="name" value="#{'奔驰'}"/> -->

<property name="name" value="#{carInfo.carName}"/>

<property name="price" value="#{carInfo.calculatePrice()}"/>

</bean>

​​​​​​​注入复杂类型:

<!-- Spring的复杂类型的注入===================== -->

<bean id="collectionBean" class="com.spring.demo5.CollectionBean">

<!-- 数组类型的属性 -->

<property name="arrs">

<list>

<value>会希</value>

<value>冠希</value>

<value>天一</value>

</list>

</property>

<!-- 注入List集合的数据 -->

<property name="list">

<list>

<value>芙蓉</value>

<value>如花</value>

<value>凤姐</value>

</list>

</property>

<!-- 注入Map集合 -->

<property name="map">

<map>

<entry key="aaa" value="111"/>

<entry key="bbb" value="222"/>

<entry key="ccc" value="333"/>

</map>

</property>

<!-- Properties的注入 -->

<property name="properties">

<props>

<prop key="username">root</prop>

<prop key="password">123</prop>

</props>

</property>

</bean>

​​​​​​​Spring的分配置文件的开发

一种:创建工厂的时候加载多个配置文件:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");

二种:在一个配置文件中包含另一个配置文件:

<import resource="applicationContext2.xml"></import>

发布了39 篇原创文章 · 获赞 2 · 访问量 5032

猜你喜欢

转载自blog.csdn.net/u013636987/article/details/96740035