【Spring学习】ring的core模块应用详解

Core模块主要的功能是实现了控制反转与依赖注入、Bean配置以及加载。Core模块中有Beans、BeanFactory、BeanDefinitions、ApplicationContext等概念。Beans为spring里的各种对象,一般要配置在spring的配置文件中;BeanFactory为创建Beans的Factory,spring通过BeanFactory加载各种Beans;BeanDefinition为Bean在配置文件中的定义,一般要定义id和class;ApplicationContext代表配置文件。

BeanFactory

BeanFactory是实例化、配置、管理众多bean的容器。BeanFactory可以用接口org,springframework.beans.factory.BeanFactory表示。BeanFactory有多种实现,其中最为常用的是org,springframework.beans.factory.xml.XmlBeanFactory,能加载XML格式的配置文件。在Web程序中用户不需要实例化Beanfactory,Web程序加载的时候会自动实例化BeanFactory,并加载所有的Beans,将各个Bean设置到Servlet、Struts的Action中或者hibernate资源中。在Java程序中,需要从BeanFactory中获取Bean,因此需要实例化BeanFactory,例如,加载ClassPath下的配置文件:

ClassPathResource  cpr= new ClassPathResource("applicationContext.xml");  
XmlBeanFactory  factory = new XmlBeanFactory  (cpr);  
Iservice service= factory.getBean("service");  
……  
factory.destroySingletons();  

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

InputStream  in = new FileInputStream("D:\\ApplicationContext.xml");  
XmlBeanFactory  factory = new XmlBeanFactory  (in);  

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

ClassPathXmlApplicationContext  appContext = new ClassPathXmlApplicationContext(  
    new String [] {"applicationContext1.xml","applicationContext2.xml"} );  
BeanFactory factory = (BeanFactory) appContext;  
//ApplicationContext继承自BeanFactory接口  

配置Bean

工厂模式

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

<bean  id="example1" class = "examples.MyBeanFactory" method="createInstance" />  
<!--等价于下面的配置-->  
<bean  id="example12" factory-bean = "examples.MyBeanFactory" method="createInstance" /> 
构造函数

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

<bean id = "example" class=" examples.ExampleBean">  
      <constructor-args><ref bean="BeanId1"/></constructor-args>  
      <constructor-args><ref bean="BeanId2"/></constructor-args>  
      <constructor-args><value>1</value></constructor-args>  
</bean>  

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

单态模式singleton

Bean可以定义是否为单态模式,在非单态模式下,每次请求该Bean都会生成一个新的对象,像数据源等一般配置为单态模式。spring默认为单态模式,如果要使用非单态模式(prototype原型模式),需要把singleton属性设置为false:

<bean id="exampleBean" class="examples.ExamleBean" singleton="false"/>  
property属性
<property name="examProperty" value="pValue" />  

等价于

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

注意:

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

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

<property name="password">  
      <null/>  
</property>  
设置对象属性

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

<bean id="dao" class = "com.lmb.DaoImpl"></bean>  
<bean id="serviceImpl" class="com.lmb.serviceImpl">  
      <property name="dao">  
             <ref bean="dao"/>  
      </property>  
</bean>  
//ref的bean属性为目标<bean>的id属性

等价于内部配置

<property name="dao">  
      <bean class="com.lmb.DaoImpl"/>  
</property>  

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

list属性
<property name="propName">  
      <list>  
             <value>stringinteger、double等类型数据</value>  
             <ref bean="dataSource"/>  
      </list>  
</property>  
set属性
<property name="propName">  
      <set>  
             <value>stringinteger、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/lmb</prop>  
             <prop key="name">lmb</prop>  
      </ props >  
</property>  
destroy-method和init-method属性

destroy-method属性配置关闭方法,如果有配置,在丢弃Java对象时会调用该方法,某些数据源、SessionFactory对象都需要用destroy-method配置关闭方法。Spring在注销这些资源时会调用下面的close方法:

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

有些对象在实例化之后需要执行某些初始化代码,但是这些代码不能写进构造函数,这时候可以把初始化代码写进某个方法中,并用<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.lmb.A" depends-on="b"></bean>  
<bean id="b" clas="com.lmb.B"></bean>  
idref与ref的区别

<idref><ref>的作用是一样的,都是配置Java对象的,不同的是,<idref>只有bean与local属性,没有parent属性
Spring加载XML配置文件时,会检查<idref>配置的Bean在不在,而<ref>只会在第一次调用时才会检查,换句话说,如果Bean不存在,<idref>在启动程序时就会抛出错误,而<ref>只会在运行中抛出错误。

autowire

可以通过Bean的autowire属性设置自动装配规则。使用autowire后不需要再用<property name="" 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的高级特性

spring程序中,Java bean一般与spring是非耦合的,不会依赖于spring类库。这也是spring的优点。但有时候Java bean需要知道自己在spring框架中的一些属性。spring提供了一些接口,实例化Java bean对象后spring会调用接口的方法。

BeanNameAware接口

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

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

}  
BeanFactoryAware接口

BeanFactoryAware接口帮助Bean知道哪个BeanFactory实例化了自己,BeanFactoryAware接口中有一个回调方法setBeanFactory,初始化该对象后,会回调该方法,将BeanFactory传进来。用法同BeanNameAware。

public interface  BeanFactoryAware{  
      void setBeanFactory(BeanFactory beanFactory) throws BeanException;  
}  

如果Java bean实现了BeanFacoryAware接口,就能够获得BeanFactory对象。BeanFactory还有下面几个常用方法

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

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

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

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

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

InitializingBean接口

InitializingBean接口执行初始化方法。InitializingBean接口会在Bean实例化后、所有属性被设置后调用初始化方法。但是使用该接口会与Spring代码发生耦合,因此不推荐使用,Spring推荐使用init-method配置。有关spring的InitializingBean的afterPropertiesSet方法和init-method区别和应用参看本人博客。

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

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

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

属性覆盖器

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

PropertyPlaceholderConfigurer属性覆盖器

PropertyPlaceholderConfigurer允许把XML配置的某些参数配置到properties文件中。这在数据库配置中很常用,配置时需要配置一个PropertyPlaceholderConfigurer对象,指定properties文件的位置,然后把替换的属性用形如${jdbc.url}的字符串替代:

<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="location" value="classpath:jdbc.properties">  
</bean>  

jdbc.properties

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

总结:core模块是spring最核心、最基本的模块,实现加载配置文件,管理,初始化bean等功能。spring配置文件中可以配置各种各样构造函数的类,不过最常见还是使用不带参数的构造函数。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值