0
点赞
收藏
分享

微信扫一扫

[Java web]-- spring3(2)


1.复杂对象的创建:不能直接new的对象,需要一个相对复杂的创建过程。

  :FactoryBean

===============================================================================================

2.复杂对象的创建流程:

  2.1 定义类:

implements FactoryBean<SessionFactory> {

/**

 * 主体逻辑:复杂对象的创建流程

 */

public SessionFactory getObject() throws Exception {

//1.Configuration

Configuration cfg=new Configuration().configure();

//2.SessionFactory

SessionFactory sf=cfg.buildSessionFactory();

return sf;

}

/**

 * 返回复杂对象的类对象

 */

public Class<?> getObjectType() {

return SessionFactory.class;

}

/**

 * 设置,创建的复杂对象是否是单例的:

 * return true;单例  全局唯一

 * return false;非单例

 */

public boolean isSingleton() {

return true;

}

}

   2.2 在配置中声明对应的bean

<!-- MySessionFactory -->

<bean id="mySF" class="com.c35.fu_za_Object.MySessionFactory"></bean>

   2.3 细节:当从工厂中获取bean时,如果spring发现此bean是FactoryBean,则不会返回其本身的对象

             而是返回其中getObejct()方法的返回值。

   2.4 获取复杂对象

ApplicationContext context= new ClassPathXmlApplicationContext("/com/c35/fu_za_Object/applicationContext.xml");

SessionFactory sf=(SessionFactory)context.getBean("mySF");

=============================================================================================

3.spring+hibernate 初步整合

  思路:

    1.将DAO纳入工厂

2.将Service纳入工厂

3.将SessionFactory纳入工厂

4.通过IOC满足依赖,保证组件正常运行

=============================================================================================

4.bean的生命周期

  *在工厂创建时,工厂中所有的单例的bean随之创建

  *在工厂关闭时,bean销毁

  *细节:

  bean的初始化方法和销毁方法:init-method  destroy-method

============================================================================================

5.bean的创建模式:

  *singleton  单例模式

  *prototype  非单例模式  此种模式的bean不会再工厂创建时创建,而是直到用户使用时才创建

<bean scope="prototype" id="user" class="com.c35.bean.lifecycle.User">

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

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

</bean>

============================================================================================

6.类型转换器:在spring为bean注入值时,一些简单转换如:String->Integer Boolean,但是

              更复杂的转换,需要单独定制类型转换器,来支持spring完成类型转换。

    6.1 定义转换器类:

extends PropertyEditorSupport{

/**

 * param :text  即spring需要转换的字符串   2015-03-24

 */

@Override

public void setAsText(String text) throws IllegalArgumentException {

SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");

Date date=null;

try {

date=format.parse(text);

//将转换好的数据,交还给spring

this.setValue(date);

} catch (ParseException e) {

e.printStackTrace();

}

}

}

6.2 配置

  *声明转换器:

<bean id="dateEditor" class="com.c35.propertyEditor.MyDateEditor"></bean>

  *将转换器交给spring,并指明转换器用途

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">

<property name="customEditors">

<map>

<!-- 转化器:dateEditor  的转换类型:java.util.Date -->

<entry key="java.util.Date" value-ref="dateEditor"></entry>

</map>

</property>

</bean>

============================================================================================

*细节:

   事务中遇到RuntimeException 则会自动回滚

   事务中遇到Exception事务不会自动回滚,依然提交,此时才需要捕捉异常回滚事务。

举报

相关推荐

0 条评论