Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / 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接口--------------------------------------分割线 --------------------------------------Spring中如何配置Hibernate事务 http://www.linuxidc.com/Linux/2013-12/93681.htmStruts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm基于 Spring 设计并实现 RESTful Web Services http://www.linuxidc.com/Linux/2013-10/91974.htmSpring-3.2.4 + Quartz-2.2.0集成实例 http://www.linuxidc.com/Linux/2013-10/91524.htm使用 Spring 进行单元测试 http://www.linuxidc.com/Linux/2013-09/89913.htm运用Spring注解实现Netty服务器端UDP应用程序 http://www.linuxidc.com/Linux/2013-09/89780.htmSpring 3.x 企业应用开发实战 PDF完整高清扫描版+源代码 http://www.linuxidc.com/Linux/2013-10/91357.htm--------------------------------------分割线 --------------------------------------

配置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">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>更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2015-03/114675p2.htm