Spring之Core模块

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 = new ClassPathXmlApplicationContext(new String [] {"applicationContext.xml","applicationContext-part2.xml"}); BeanFactory factory = (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配置关闭方法

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

<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">: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方法

人若勇敢就是自己最好的朋友

Spring之Core模块

相关文章:

你感兴趣的文章:

标签云: