Spring全面解析 大复习

来源:互联网 发布:怎么批量注册淘宝小号 编辑:程序博客网 时间:2024/06/03 12:41

                                                                             Spring全面解析                                                                                   

 

Spring不是真正的所谓结构框架,而是Spring相当于一个管理容器

首先明确我们用Spring解决什么样的问题:
复杂软件依赖管理问题,所谓动一发,而动全身。Spring正是解决这样的问题而产生的。

例如:
业务关联问题:会随着业务模块的增多而变化,业务复杂度(动态变化),会造成编译时的确定关系的混乱。


Spring可以让我们的代码脱离这样的硬编码状态,从而解决以上的动态变化造成的复杂问题。

解决以上情况的方案:
      1 对象的创建由其他程序代码统一负责(提供一个类似对象工厂)
      2 对象关系应采取运行时的搭建方式--解决硬编码,不要总是通过new方法
     
Spring-IOC思想(Inversion of Control)控制反转
     提供对象创建和对象关系搭建的容器(就是解决以上的方案)
----------------------------------------------
     对象关系搭建依靠“依赖注入技术"~!
     依赖注入的方式:
           1 接口注入:---基于依赖倒转原则,强调引用为接口类型,提高依赖对象的可替换性
           2 set注入:---基于set方法,将依赖关系传入到对象内部。
           3 构造子注入:基于构造方法,将依赖关系传入到对象内部。
---------------------------------------------------------------------
Spring是一个从实际项目开发经验中抽取的,可高度重用的框架技术。
Spring基于IOC思想,提供了对应的对象管理机制。--轻量级的容器
          并同时提供了解决各类问题的通用API ---框架

解释下轻量级的含义:
    对于被管理对象无实现规范,不带有侵略性的API,自由自在的(不依赖特定的API,如依赖EJB)
    不对容器的依赖,对运行环境无特定的要求
    具有配置能力
    轻量级可以接受任何JavaBean
---------------------------------
struts 渗透到页面和底层的操作 
hibernate 渗透到持久化操作
spring 渗透到整个项目程序的方方面面
-----------------------------------
Spring 简单小测试(利用xml配置+反射机制实现)

1 配置:applicationContext.xml(可自定义)
<beans>

<bean id/name="在spring容器内标识的对象,id字母文字等通用字符,name可包含"/","/"等特殊字符
      class="对象完整类型名称"
      scope="对象管理方式 (默认单例Singleton,多例Prototype) 两种"
      init-method="init"// 初始化方法名
      destory-method="close"//Spring卸载对象时调用销毁方法
      autowire="自动装载 ,根据参数,类型,名称自动寻找合适的对象搭建关系1 byName,2 byType 3 Constructor根据构造函数操作类型 4 Autodetect(自动的,先用构造,再用类型)  等寻找<bean>元素"
>   尽量不要使用自动装载。容易出现不必要的麻烦。

<property name="根据属性名找对应名称的set方法">
<value>"设置的定值"
<ref="1 local文档内部找  2 bean在当前配置的applicationContext中找<bean> 3 parent 在它的父applicationContext中寻找 <bean>             :引用某一特定<bean>实例来填充属性名/>

<list></list>
<set></set>
等许多其他属性,不懂的查API吧
</property>
</bean>
</beans>
-----------------
2 BeanFactory --定义了JavaBean配置,创建,关系的维护,基于applicationContext.xml来创建对象,是接口
 BeanFactory beanfactory=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));     
daoAction action=(daoAction) beanfactory.getBean("action");//基于getBean获得<bean>的对象
其他接口方法自己学着看吧
-----------------
3 ApplicationContext是BeanFactory的子类,在BeanFactory基础之上还加入了对象事件机制和国际化支持

ApplicationContext applicationcontext=new ClassPathXmlApplicationContext(String 数组-多个xml文档 ,String对象,一个xml文档);
daoAction action=(daoAction) applicationcontext.getBean("action");//基于getBean获得<bean>的对象

----------
ApplicationContext 和 BeanFactory一样,还有新加入的对象事件机制和国际化支持
如在<bean中 init-method="..."  destory-method="..  ">  是可以的

监听器要求实现ApplicationListener  
 

=================
事件类要继承ApplicationEvent

=================
action类 要implements ApplicationContextAware
通过setApplicationContext 设置ApplicationContext对象  applicationcontext

public void setApplicationContext(ApplicationContext arg0) throws BeansException {
  this.applicationContext = arg0;
  
 }
通过applicationcontext对象的发布
public void executeEvent() {
  TestApplicationEvent event = new TestApplicationEvent("TesetEvent");
  applicationContext.publishEvent(event);
 }

======================
将监听对像配置在applicationContext.xml中
<bean id="listener" class="cn.com.csuinfosoft.action.TestApplicationListener"></bean>

 

------------------------------------------------------------------------------------------------------------------

不错,继续加油~!

------------------------------------------------------------------------------------------------------------------

SS:  Struts+Spring的整合。

Sping 除了过去的core包,因为是写web代码,还需另外一个Spring 2.0 Web-Libraries包

webApplicationContext---》是继承 ApplicationContext,与web环境兼容

org.springframework.web.struts.ContextLoadPlugin(spring基于struts提供插件的接口-->在struts初始化时创建webApplicationContext对象,并将ApplicationContext保存在Application会话中
contextConfigLocation 是用来加载多个xml ,整合。
首先要在struts-config.xml中加入:(contextConfigLocation是ContextLoaderPlugIn一个属性)
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
  <set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml"/>
</plug-in>

调用ApplicationContext的setParent方法,总之所有的application对象都可以保存在一个对象中,这一个对象有他们的所有引用
1.DelegatoryActionProxy---为action设置代理
2 DelegatoryRequestProsessor---替换默认的requestProsessor
以上两种都可以通过Application会话访问,获得ApplcationContext引用,该引用的getBean()方法
故struts.xml中得action这样写:
<action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" scope="request"  parameter="login">//调用login方法。
    <forward name="ok" path="showResume.jsp"></forward>
</action>

------------------------------------------------------------------------
最后在applicationContext.xml中
<bean id="dao" class="com.dao.impl.ImplDao"/>
<bean name="/login" class="com.action.DaoAction">
    <property name="dao">//这个对应DaoAction中得属性名,其实是和setDao方法对应,如是name="user" 那么action中得方法要为setUser
    <ref local="dao"/>
    </property>
</bean>

在xml文档中,
有多个bean 但是class相同,可以通过abstract--若为true,则该<bean>元素不会创建对象,就是在ApplicationContext.getBean(...)方法中得不到对象                          

若设置parent-引用其他<bean>元素作为自己的基本配置
<bean name=".."和struts-config.xml中的<action path=".."是相同的  >
<property name=" ">
--------如下:
<bean id="blogAction" abstract="true" class="cn.com.csuinfosoft.action.BlogAction">
  <property name="userDAO">
   <ref local="userDAO"></ref>
  </property>
  <property name="blogDAO">
   <ref local="blogDAO"></ref>
  </property>
 </bean>
 
 <bean name="/writeBlog" parent="blogAction">

 </bean>

 <bean name="/blogList" parent="blogAction">
 </bean>
   
 <bean name="/showBlog" parent="blogAction">
 </bean> 

------------------------------------------------------------------------------------------
Spring+Hibernate 整合:
今天讲下基本配置,会引出一个问题:事务问题~!
Configuration
SessionFactory ----》 spring可以创建,那么session的管理也得由Spring来管理,那么关闭SessionFactory 、Session自然也由Spring
Session

Dao---引用Spring所提供的Session资源

首先加载Hibernate环境
然后加载Spring环境 注意加入 Spring 2.0Aop和Sping 2.0 orm/dao/Hibernate这两个包
在Spring的applicationContext.xml中
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<!--hibernateProperties  将hibernate.cfg.xml中对应的hiernate属性值设置到到该集合属性中-->
<propert name="hibernateProperties">
  <props>这个是集合属性
           <prop key="hibernate.connection.username">scott</pro>//这些属性由spring来管理
           <prop key="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:oracle</pro>
            …………
 </props>
</property>
<property name="mappingResources">  这个使用数组设置值的
  <list>
     <value>cn/com/vo/dept.xml</value>
     <value>cn/com/vo/emp.xml</value>
  </list>
</property>
----------------以上是将Hibetnate所有配置放在Spring中

另外在一种方法,只是配置个索引:
<property name="configLocation">
<value>/web-inf/classes/hibernate.cfg.xml</value>
</property>
-----------------------------
<!--hibernate模板类,提供了hibernate持久化操作的封装-->
<bean id="hibernateTemplate" class="org.springframework......类名"> //模板类,提供了许多常用的hibernate操作模板
<constructor=arg index="">
<ref local="seesionFactory>
</constructor-arg>
</bean>

<bean id="empDAO" class="类名">
 <property name="hibernateTemplate">
   <ref local="hibernateTemplate"/>
  </property>
</bean>
----------------------------------------以下为applicationContext.xml的一个例子:
<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="
http://www.springframework.org/schema/beans"
 xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <!-- hibernateProperties
   将hibernate.cfg.xml中对应的Hibernate属性值设置到该集合属性中
  -->
   <!--
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.connection.username">scott</prop>
    <prop key="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:oracle</prop>
    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
    <prop key="hibernate.connection.password">tiger</prop>
    <prop key="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</prop>
    <prop key="hibernate.show_sql">true</prop>    
   </props>
  </property>
  <property name="mappingResources">
   <list>
    <value>cn/com/vo/Emp.hbm.xml</value>
    <value>cn/com/vo/Dept.hbm.xml</value>
   </list>
  </property>
  -->
   <property name="configLocation">
    <value>hibernate.cfg.xml</value>
   </property>
   
 </bean>
 
 <!--  
  Hibernate模板类,提供了Hibernate通用持久操作的封装
  -->
 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
  <constructor-arg index="0">
   <ref local="sessionFactory"/>
  </constructor-arg>
 </bean>
 
 <bean id="DaoImpl" class="cn.com.dao.impl.">
  <property name="hibernateTemplate">
   <ref local="hibernateTemplate"/>
  </property>
 </bean>

</beans>
-------------------------------------------------------------------------------------

dao类继承HibernateDaoSupport
分页查询:
Session session=this.getSession();
Query query=session.createQuery();
quert.setFirstResult(1);
query.setMaxResults(10);
return query.list();

-------------------------------------------------------

ssh  spring+hibernate+struts
hibernate3 和spring在整合在struts上时会有asm包的冲突,这是用到asm版本不同,删除这样一个高级点的spring 2下的包


总结:
Hibernate+DAo
LocalSesssionFactory Bean   负责创建sessionFactory 
Hibernate Template 提供常用的Hibernate的持久化操作
----------这两个是要配置在applicationContext.xml的

DAo层
1 dao 类直接持有HibernateTemplate属性
2 dao 类继承HibernateDaoSupport   
这两种无论哪种方式,都要在applicationContext.xml中设置<bean><property name="hibernateTemplate>……
-----------------------------------------------------------------------

----------------------
在 web工程下,session资源的管理:
启用:OpenSessionInViewFilter: org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
当请求的最末端,关闭session  ,关闭它需要webApplicationContext,创建该对象通过ContextLoaderLister:类名
读取:web.xml
<filter>
  <filter-name>hibernateFilter</filter-name>
  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  <!-- 定义LocalSessionFactoryBean在Spring中的名称 -->
  <init-param>
   <param-name>sessionFactoryBeanName</param-name>
   <param-value>mySessionFactory</param-value>
  </init-param>
  <init-param>
   <param-name>singleSession</param-name>
   <param-value>true</param-value>//将其设这为true 是这样个可以保持在一个完整的request中只存在一个session
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>hibernateFilter</filter-name>
  <servlet-name>action</servlet-name>
 </filter-mapping>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 

 

原创粉丝点击