Spring 相关配置&属性注入

Spring是一个JavaSE/EEfull-stack)框架。

Spring框架有EE每层的解决方案。

WEB层:Spring MVC

Service层:SpringBean管理,Spring的事务管理

DAO层:SpringJDBC模板,ORM模块用于整合其他的持久层框架。

spring特点:

Ioc:控制反转——从主动的去new对象变成了被动接收由spring实例化的对象,解耦

Aop:面向切面编程

 

好的程序需要遵循ocp原则:open-close原则,对于程序的扩展是open的、

对于修改源码是close的。Ioc就实现了ocp原则

 

DI 依赖注入:Ioc环境中实现,功能是可以通过配置文件的设置自动封装类中的参数,参数必须提供set方法。类需要交给spring管理:也就是说这个类是由spring来生成实例的

Spring的工厂类

常用的有两个 ApplicationContext、BeanFactory。两者都为接口。ApplicationContext是新版本中的工厂类,ApplicationContext是BeanFactory的子接口。BeanFactory是最顶层的接口。根据多态:父类更通用,子类更具体方法更多。

现在一般都使用ApplicationContext工厂类接口。

Spring 相关配置&属性注入


两者的区别:

ApplicationContext:在加载完配置文件后,配置的所有的对象都已经实例化创建完毕并存到了容器中。

创建这个对象:ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

   或ApplicationContext ac = new FileSystemXmlApplicationContext("D:/applicationContext.xml");

ClassPathXmlApplicationContext:加载类路径下的配置文件。

FileSystemXmlApplicationContext :加载文件系统中的配置文件。

BeanFactory:在加载完配置文件后并不会马上实例化创建对象,而是到了调用BeanFactorygetBean(id)方法后才会实例化创建对象。

创建这个对象:BeanFactory bf = new XmlBeanFactory(new FileSystemResource("d:/bean.xml"));

只加载文件系统中的配置文件

 

Springxml配置:

格式:<bean id|name=” ”  class=” ”  scope=” ”>标签的配置

bean标签作用:定义Java类的。这个类就可以被Spring创建。也就是交给spring管理。

Name属性和id的区别是name属性可重复可以出现特殊字符id属性相反struts1会用到name属性,现在基本都用id属性。

Class:属性指类的全限定类名。

Scope:属性描述bean的作用范围

共有5个取值,两个常用的为:singleton指定创建这个类时这个类为单例模式,为 Scope默认值,想取此值不操作       即可。

prototyoe指定创建这个类时这个类为多例模式。

Request,应用web开发,创建一个对象,将这个对象存入到request域中。

session ,应用web开发,创建一个对象,将这个对象存入到session域中。

globalSession,应用web开发,应用在porlet环境中,没有这个环境的话与session等价      的。父项目与子项目共用的session

init-methoddestroy-method属性用于做Bean的初始化(类被创建后执行)和销毁( 类被销毁前执行)的操作。

注:使用destroy-method 必须使用单例模式  scope="singleton"时不写scope属性也可以。

SpringBean管理的XML的配置

Spring的实例化Bean的方式

共三种:

无参数构造方法的实例化常用

 <!-- 无参数构造方法的实例化 -->

    <bean id="bean1" class="com.xxxx.xxx.xx">

    </bean>

静态工厂实例化的方式

提供Bean2的静态工厂

public class Bean2Factory {

 

public static Bean2 createBean2(){

return new Bean2();

}

}

 

配置

    <!-- 静态工厂实例化的方式 -->

    <bean id="bean2" class="com.xxxx.demo.Bean2Factory" factory-method="createBean2"/>

实例工厂实例化的方式

提供Bean3的实例工厂

public class Bean3Factory {

 

public Bean3 createBean3(){

return new Bean3();

}

}

 

配置

    <!-- 实例工厂实例化的方式 -->

    <bean id="bean3Factory" class="com.xxxx.demo.Bean3Factory"></bean>

    <bean id="bean3" factory-bean="bean3Factory" factory-method="createBean3"></bean>


Spring的属性的注入(DL)

构造方法的注入

<!-- 构造方法的注入 -->

<bean id="" class="com.xxxx.xxx.xx">

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

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

</bean>

Set方法的注入常用

<!-- set方法的属性注入 -->

<bean id="" class="com.xxxx.xxx.xx.">

<property name="" value=""/>

<property name="" value=""/>

</bean>

对象类型属性注入常用

<!-- 注入对象类型 -->

<bean id="" class="com.xxxx.xxx.xx">

<property name="" value=""/>

<property name="" ref=""/>

</bean>

p名称空间的属性注入(spring2.5以后才有)

在配置文件中引入

<beans 

xmlns="http://www.springframework.org/schema/beans"

xmlns:p="http://www.springframework.org/schema/p"

    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">

语法:

普通类型的属性p:属性名=””

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

 

<!-- 使用P名称空间属性注入 -->

 <bean id="" class="com.xxxx.xxx.xx" p:属性名="" p:属性名-ref=""/>

SpEL的属性注入(Spring3.0后才有)

格式

#{SpEL}

正常赋值很少用,但是可以用来写java代码,例如#{user.getUsername()}

集合属性的注入

<!-- 给数组赋值 -->

<property name="arr">

<array>

<value>aa</value>

<value>bb</value>

<value>sb</value>

<value>cc</value>

<value>mm</value>

</array>

</property>


<!-- list集合赋值 -->

<property name="list">

<list>

<value>aaa</value>

<value>bbb</value>

<value>ccc</value>

<ref bean="car1"/>

</list>

</property>


<!-- set集合赋值 -->

<property name="set">

<set>

<value>aaaa</value>

<value>bbbb</value>

<value>cccc</value>

<ref bean="car2"/>

</set>

</property>


<!-- map集合赋值 -->

<property name="map">

<map>

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

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

<entry key="c" value-ref="car1"></entry>

</map>

</property>


<!-- properties赋值 -->

<property name="prop">

<props>

<prop key="a">aa</prop>

<prop key="b">bb</prop>

<prop key="c">cc</prop>

</props>

</property>