Spring之Core模块

n672 9年前

Core模块主要的功能是实现了控制反转与依赖注入、Bean配置以及加载。Core模块中有Beans、BeanFactory、BeanDefinitions、ApplicationContext等概念

BeanFactory

BeanFactory是实例化、配置、管理众多bean的容器

在Web程序中用户不需要实例化Beanfactory,Web程序加载的时候会自动实例化BeanFactory,并加载所欲的Beans,将各个Bean设置到Servlet、Struts的Action中或者Hibernate资源中

在Java桌面程序中,需要从BeanFactory中获取Bean,因此需要实例化BeanFactory,例如,加载ClassPath下的配置文件:

ClassPathResource  res = new ClassPathResource(“applicationContext.xml”);  XmlBeanFactory  factory = new XmlBeanFactory  (res);  Iservice service= factory.getBean(“service”);  ……  factory.destroySingletons();

或者使用文件流加载任意位置的配置文件

InputStream  in = new FileInputStream(“C:\\ApplicationContext.xml”);  XmlBeanFactory  factory = new XmlBeanFactory  (in);

或者用ClassPathXmlApplicationContext加载多个配置文件(以字符串形式传入)

ClassPathXmlApplicationContext  appContext = newClassPathXmlApplicationContext(  new String [] {“applicationContext.xml”,”applicationContext-part2.xml”}  );     BeanFactoryfactory = (BeanFactory) appContext;  //ApplicationContext继承自BeanFactory接口

配置Bean

工厂模式

如果一个bean不能通过new直接实例化,而是通过工厂类的某个方法创建的,需要把<bean>的class属性配置为工厂类(或者吧factory-bean属性配置为工厂类对象)

<bean  id=”examBean” class = “examples.MyBeanFactory”method=”createInstance” />  <--等价于下面的配置-->  <bean  id=”examBean2” factory-bean = “examples.MyBeanFactory”method=”createInstance” />

 

构造函数

如果Bean的构造函数带有参数,需要指定构造函数的参数

<bean id = “examBean”class=” examples.ExampleBean”>        <constructor-args><ref bean=”anotherBeanId”/></constructor-args>        <constructor-args><ref bean=”anotherBeanId2”/></constructor-args>        <constructor-args><value>1</value></constructor-args>  </bean>

参数又先后顺序,要与构造函数参数的顺序相同

 

单态模式

Bean可以定义是否为单态模式,在非单态模式下,每次请求该Bean都会生成一个新的对象

像数据源等一般配置为单态模式

<bean id=”exampleBean”class=”examples.ExamleBean” singleton=”false”/>

 

property属性

destroy-method属性配置关闭方法,如果有配置,在丢弃Java对象时会调用该方法,某些数据源、SessionFactory对象都需要用destroy-method配置关闭方法

<propertyname=”examProperty” value=”pValue” />

等价于

<propertyname=”examProperty”>        <value>pValue</value>  </property>

注意:

<propertyname=”password”>

      <value></value>

</property>

会将password设置为””,而不是null,如果想设置为null应该为

<propertyname=”password”>

      <null/>

</property>

 

<ref>属性

Spring配置文件的Bean之间可以相互引用,引用时用<ref>标签配合Bean的id属性使用

也可以使用内部配置

<bean id=”dao” class = ”com.clf.DaoImpl”></bean>  <bean id=”serviceImpl”class=”com.clf. serviceImpl”>        <property name=”dao”>               <ref bean=”dao”/>        </property>  </bean>

等价于内部配置

<property name=”dao”>        <bean class=” com.clf.DaoImpl”/>  </property>
 

除了使用<ref>的bean属性,还可以使用local、parent,它们与bean属性的作用是一样的,但是,local只能使用本配置文件中的bean,parent只能使用父配置文件中的bean

 

<list>属性

<property name=”propName”>        <list>               <value>String、Integer、Double等类型数据</value>               <ref bean=”dataSource”/>        </list>  </property>
 

<set>属性

<property name=”propName”>        <set>               <value>String、Integer、Double等类型数据</value>               <ref bean=”dataSource”/>        </set>  </property>
 

<map>属性

<property name=”propName”>        <map>               <entry key=”key1”>                      <value>String、Integer、Double等类型数据</value>               </entry>               <entry key-ref=”key2”>                      <ref bean=”dataSource”/>               </entry>        </map>  </property>

 

<props>属性

<property name=”propName”>        <props>               <prop key=”url”>http://localhost:8080/clf</prop>               <prop key=”name”>clf</prop>        </ props >  </property>
 

<destroy-method>和<init-method>属性

<bean id=”dataSource”class=”org.apache.commons.dbcp.BasicDataSource” destroy-method=”close”>

……

</bean>

Spring在注销这些资源时会调用close方法

 

有些对象在实例化之后需要执行某些初始化代码,但是这些代码不能写进构造函数,这时候可以把初始化代码写进某个方法中,并用<init-method>指定该方法

<bean id=”c”class=”com.clf.SpringExample”  init-method=”init”>

 

depends-on属性

Spring会默认按照配置文件里的Bean顺序地实例化Bean,但是有时候实例化A对象之前需要先实例化后面的B对象,这时候可以使用depends-on属性强制先实例化B对象

<bean id=”a”clas=”com.clf.A” depends-on=”b”></bean>  <bean id=”b”clas=”com.clf.B” ></bean>
 

<idref>与<ref>的区别

<idref>与<ref>的作用是一样的,都是配置Java对象的,不同的是,<idref>只有bean与local属性,没有parent属性

Spring加载XML配置文件时,会检查<idref>配置的Bean在不在,而<ref>只会在第一次调用时才会检查,换句话说,如果Bean不存在,<idref>在启动程序时就会抛出错误,而<ref>只会在运行中抛出错误

 

<autowire>

可以通过Bean的autowire属性设置自动装配规则。使用autowire后不需要再用<propertyname=”” value=”” />显式地设置该Bean的属性、依赖关系,Spring会根据反射,自动寻找符合条件的属性,设置到该Bean上

autowire属性定义的不是需要自动装配的属性名,而是自动装配的规则,一旦配置,所有的属性都会遵循autowire定义的规则

 

No:即不启用自动装配。Autowire默认的值。

byName:通过属性的名字的方式查找JavaBean依赖的对象并为其注入。比如说类Computer有个属性printer,指定其autowire属性为byName后,Spring IoC容器会在配置文件中查找id/name属性为printer的bean,然后使用Setter方法为其注入。

byType:通过属性的类型查找JavaBean依赖的对象并为其注入。比如类Computer有个属性printer,类型为Printer,那么,指定其autowire属性为byType后,Spring IoC容器会查找Class属性为Printer的bean,使用Setter方法为其注入。

constructor:通byType一样,也是通过类型查找依赖对象。与byType的区别在于它不是使用Setter方法注入,而是使用构造子注入。

autodetect:在byType和constructor之间自动的选择注入方式。

default:由上级标签<beans>的default-autowire属性确定。

 

dependency-check

有时候某些Bean的属性配置有错误,这种错误在程序启动的时候不会有任何异常,会一直潜伏到Spring调用该Bean时才会被发现

依赖检查能够检查属性是否被设置,如果配置了依赖检查,程序启动是会进行配置校验,以便及时地发现错误。

但是需要注意的是,依赖检查是很生硬的,例如设置为object,将会检查所有的Java对象属性,只要有一个属性没有设置,就会抛出异常

 

no或default:不做任何检查,默认

simple:仅检查基本类型、集合属性

object:仅检查Java对象属性

all:检查所有属性

Bean的高级特性

BeanNameAware接口帮助Bean知道自己在配置文件中的id

import  org.springframework.beans.factory.BeanNameAware  public class  BeanNameTest implementsBeanNameAware{        private String beanName;        //Spring会调用该方法        public void setBeanName(String beanName){               this.beanName = beanName;        }         }
 

BeanFactoryAware接口帮助Bean知道哪个BeanFactory实例化了自己

public interface  BeanFactoryAware{        void setBeanFactoryAware(BeanFactorybeanFactory) throws BeanException;  }

用法同BeanNameAware

此外还有一下常用的方法

boolean containsBean(String)判定指定名称的Bean是否存在

Object getBean(String)返回指定名称,如果没有该Bean会抛出异常

Object getBean(String,Class)返回指定名称的Bean,并转化为指定的类对象

boolean isSingleton(String)判断指定名称的Bean是否被配置为单态模式

String [] getAliases(String)返回指定名称的Bean的别名

 

InitializingBean接口会在Bean实例化后、所有属性被设置后调用初始化方法。但是使用该接口会与Spring代码发生耦合,因此不推荐使用,Spring推荐使用init-method配置

public interface InitializingBean{        public void afterPropertiesSet();  //初始化时调用此方法  }
 

DisposableBean接口会在Bean对象丢弃时调用销毁方法

public interface DisposableBean{        public void destroy();  //销毁时调用此方法  }

属性覆盖器

对于一些参数,更实用更简单的方法是使用properties配置,而不是配置在Spring的配置文件中

PropertyPlaceholderConfigurer允许把XML配置的某些参数配置到properties文件中

<bean id=”dataSource”class=”org.apache.commons.dbcp.BasicDataSource” destroy-method=”close”>        <property name=”driverClassName” value=”${jdbc.driverClassName}”/>        <property name=”url” value=”${jdbc.url}”/>        <property name=”username” value=”${jdbc.username }”/>        <property name=”password” value=”${jdbc.password }”/>  </bean>     <bean id=”propertyConfigurer”class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>        <property name=”loaction” value=”classpath:jdbc.properties”>  </bean>

 

jdbc.properties

jdbc.driverClassName= com.mysql.jdbc.Driver  jdbc.url =jdbc:mysql://localhost:3306/clf?characterEncoding=UTF-8  jdbc.username =clf  jdbc.password =admin