spring学习笔记

来源:互联网 发布:富士康 java 开发工资 编辑:程序博客网 时间:2024/05/16 05:27


spring -------------------------------
 
   轻量级容器框架

   ioc(最核心的东西),它把关系都封装好了;
   aop 面向切面编程;代理技术;轻量级会服务;
   ejb 也提供了这项服务,但是它是重量级服务;

   比如:hibernate 中的session就交给spring来管了,spring就向一个大总管,什么郚它都管了。


spring 架构图:所有的服务都依赖于容器;

ssh  主要是对hiternate和struts的集成;

------------------spring---ioc 学习--------------------------------------

1。  建立相应的dao和manager(前台页面业务逻辑)

      dao层接口、实现
     
       mangage层(业务逻辑层)接口、实现

       关于接口层与实现层我们可以人为写死的,但一个业务逻辑依赖好几个dao呢,我们就得把全部用得上的全部装配上,
由此我们引出 ioc (控制反转)。
      
       IOC 控制反转:你别做这个信赖关系了,交给spring,由它去处理;可是使用这种服务是要有前提的:必需给相应dao接口
给相应的构造函数,也可以生成得到、set方法;这种形式这称为依赖注入,控制反转。

       注入:是主动给的,假设:你要追尔一个女孩,你是主动,你依赖于它;你发起的,你就依赖于别人,反过来,她追你;主动给的你,我们并没有信赖于它
它没有琴入性,你离开了,他不受什么影响。


-------ioc-----spring----相关配置----------------------------------

spring相关配置相关jar spring最核心的jar 是相关dist文件夹的下的  spring。jar
  和两个日志包:common:log4j.jar  logg-common。jar

  在sspring解压过文件中复制applicationContext.xml/和log4j.jar

  配置dao:

     *************《关于构造函数的注入在applicationContext中的<bean>

   <contr

</bean>


客户端加载: BeanFactory factory=newClassPathApplication("相关spring配置");
             factory。getBean("取出你要的产品"); spring在给你的时候它把所有实例都给我们创建好了。

 使用spring它为我们做了很多事,使我们更专注业务。

  用set方法注入和构造函数注入之区别:使用构造函数它被调用的时机比较早,一在类加载的时候就把dao加载进来一,而set是在实例化之后,才来注入的.
  它们区别就在时机上面已,具体情况而定

   spring ioc应用的是di依赖注入,依来注入是别人创建好了来给你。   但并不是说所有的实例都由它来创建军。

   使用spring ioc 会使我们的程序比较清晰。spring没有侵入性,这是一个轻量级框架

    为什么都使用轻量级的,因为它不信依赖于容器。

-------------属性注入------------------------
只要我们给这个类提供构造函数和set方法,我们就可以把它注入进去。

日志目录,如果没有明确给出,就会在你相应的工程下,建立一个web-inf的文件,里面就是程序运行时的日志文件。

属性注入方法:在applicationContext。xml中

    《bean id="合法的名称" class=“该属性名所在的类(action)”>

           因为是set注入
                   《property  name===“在类中和action中的属性名称set后面的属性名称” value="123"/》//这样的话就注入进去了。

                     别一种方式;(int 型)<property name="在类中和action中的属性名称set后面的属性名称">

                                   <value>123</value>
                                   </property>

 

     </bean>

        map在bean中注入用的是《map 》
                           <entriy key="k1" value="v1">
                           <entriy key="k2" value="v2">

                   《/map>


如工程比较大,都气配置文件放在一个文件中,会有点太乱,spring是可以分文件来管理的(也就是分文件)。

对于多文件的我们可以给所有spring一个约定:然后用通配匹配:如所的spring文件都 是applicationContext头的,我们可以这样来事实定义

applicationContext_*;
例如:applicationContext_beans。xml
      applicationCOntext_beans1.xml
--------------------------日期注入----------如果按照上面的形式来注入,那么注入就会有问题的,在struts中它提供了conterter,在spring中提供了自定义属性编辑器


作用spring属性编缉器,在类继乘的时候,要继成javabean下面的东西.然后把相应属性转换注入进来 ----方法:

  属性编辑器,把相应的字符串转换成相应的对象注入。spring有内制的属性编辑器,我们右以自己定义,继成一个类,propertyeditsupport然后复写AsText参见
 
  spring类型转换原理:把相应的类型交给弹跳去管理。关于日期的有点特别:需要运用人为调用一下弹跳的属性编辑器方法格式化一下该日期。

 spring 关于日期转换:事例:


   定义一个属性转换器:com.test.property.UtildateProperty.java

   
public class UtildateProperty extends PropertyEditorSupport {

    private String format="yyyy-MM-dd";
 public void setAsText(String text) throws IllegalArgumentException {
 
    System.out.println("UtildateProperty 属性编辑器text="+text);
   SimpleDateFormat  sdf=new SimpleDateFormat(format);
    try {
   Date d=sdf.parse(text);
   this.setValue(d);
  } catch (ParseException e) {
   e.printStackTrace();
  }
  
 }
 public void setFormat(String format) {
  this.format = format;
 }


 
 
 
}
在applicationContext-beans.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"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
   
   
   <bean id="bean1" class="com.test.property.Bean1">
   
        <property name="mapValue">
     
           <map>
              <entry key="k1" value="map1"/>
              <entry key="k2" value="map2"/>
           </map>
     
      </property>   
     
      <property name="strValue" value="string"/>
     
      <property name="dateValue">
           <value>2008/09/09</value>
     
      </property>
     
   
   </bean>

</beans>

针对日期转换相关xml文件applicationContext-utilDate.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"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 <!-- 需要把它注入到一个spring的类里 定义一个属性编辑器-->
 <bean id="customEditorConfigurer"
  class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  <property name="customEditors">//这个属性来自CustomEditorConfigurer类中的一个map属性
   <map>
    <entry key="java.util.Date">

     <bean class="com.test.property.UtildateProperty">
        <property name="format" value="yyyy/MM/dd"></property>
     </bean>//为了适应各类时间格式,我们利用注入的方式把日期格式注入一下,然后在bean中指定一下就可以用了。这就是spring IOC 的强大之处。

    </entry>

   </map>
  </property>
 </bean>

</beans>
-----------------------------------------spring公共策略---------------------------

把几个类中的相同属性拿出来,定义成一个抽象的《bean》如:

   <bean id="Abstract" abstract="true">
     把相同属性配制在这里如:
    写的方法和来的属性方法一致《用property》
   </bean>
当相你关类加载属性时可以这样来做:

      《bean id="bean2" class="com.test.beans.bean2" parent="Abstract"》
            然后在里面加上它的扩展属性,就是在本类中有的,在人为定义的抽象类中没有有

     《/bean>

**依赖注入有好几种方式:


  ref属性
  《ref》标签
  内部bean
----------------------------spring bean的作用域(scope)-----------------------------

作用域就是,当我们每次getBean的时候,spring每次给我们创建一个实例呢,还是怎么的呢?

请看下面的例了:

    比如:Bean1 bean1=new ClassPathApplicationContext("applicationContext.xml");
          Bean1 bean2=new ClassPathApplicationContect("applicationContext.xml");
           if (bean1==bean2)
               {
                     System.out.println("bean1==bean2");
               }
            else{
                        System.out.println("bean1!=bean2");
             }
所以对spring对bean的初始化只有一次,初始化一次,就不在初始化了。


  每个bean都有有它的相关属性: <bean id="bean1" class="com.test.property.Bean1" scope=”“>
   如果不写,它的默认值是singleton,作用域就是spring对bean只初始化一次,每次调用的时候返回相同的实例。另一属性:prototype;它的意思就是每次getBean的时候它就会给我们重新创建军一次。
   每次调用的时候返回不同的实例。
-----------------------spring自动装配=-----------------------------

它的主要作用就是:简化代码编写,有利于提高开发效率
  根据名称的自动装配:主要是开发效率:写法:在applicationContext。xml:尖括号内加一个属性:default-autowier=”byName“它指对本文件生效
  前提,约定要一定。

根据类型装配:如果按类型装配,那和无论相应id的值怎么变都不会影响class的加载
--------------------------ioc这一块结束-------------------------------------------
ioc就向一个大型的抽象的工厂,但它比抽象工厂更复杂。它有关系注入。


-------------------------------------------spring aop 学习笔记--------------------------------

回顾:和第三方框架集成,ioc控制反转,大部分它采的用依赖注入。ioc使用带来的好处:比工厂复杂好多,使层次非常分明,防之factory冗杂。

spring非常庞大,也有相应的配置文件

通配符的使用:比如:applicationContext-beans.xml/applicationContext-action.xml 在装配的时候可以用applicationContext-*来装配。

spring的相关配置很灵活,需要配的文件很多。他可以拆分配制,在维扩的时候最好把自动装配的全加上,对于实例我们是可以控制的,我们可以设它的属性:一个是就产生一个实例,一个是每次装载就创建军一个。

------------------------aop----------------------------------------------

spring提供的管理事务;aop可以取代ejb中的事务。aop这种思想早就有了,并不是一种新的技术。它提供声明是事务,它可以对普通的java对象进行事务管理。

轻量级会的东西,

spring提出一套理论。

通过代理很好来理解aop,管的是在这些对象中是有横切性的问题

静态代理、动态代理。代理,要有和原来一样的模式,。只是在其中加上了安全检查。

静态代理例子:


接口:UserManager.java

package com.spring_static;

public interface UserManager {
  public void addUser(String usename,String password);
 
  public void deleteUser(int id);
 
 
}

实现:UserMangerImpl.java

package com.spring_static;

public class UserMangerImpl implements UserManager{

 @Override
 public void addUser(String usename, String password) {
 System.out.println("-----------------UserMangerImpl---addUser---");
 
 }

 @Override
 public void deleteUser(int id) {
  System.out.println("-----------------UserMangerImpl---deleteUser---");
  
 }

 


}

代理:UserMangerProxy.java

package com.spring_static;

public class UserMangerProxy implements UserManager {

 private UserManager userManager;

 // 用构造 函数加载接中
 public UserMangerProxy(UserManager userManager) {

  this.userManager = userManager;

 }

 @Override
 public void addUser(String usename, String password) {
  checkSecutity();
  userManager.addUser(usename, password);

 }

 @Override
 public void deleteUser(int id) {

  userManager.deleteUser(id);

 }

 private void checkSecutity() {
  System.out.println("------checkSecutity()--------");

 }

}

客户验证:client
package com.spring_static;

public class client {

 /**
  * @param args
  */
 public static void main(String[] args) {
  
  UserManager userManager=new UserMangerProxy(new UserMangerImpl());
  //userManager.addUser("123", "456");
    userManager.deleteUser(2);
 }

}

动态代理:

接口:UserManager.java

package com.spring_static;

public interface UserManager {
  public void addUser(String usename,String password);
 
  public void deleteUser(int id);
 
 
}

实现:UserMangerImpl.java

package com.spring_static;

public class UserMangerImpl implements UserManager{

 @Override
 public void addUser(String usename, String password) {
 System.out.println("-----------------UserMangerImpl---addUser---");
 
 }

 @Override
 public void deleteUser(int id) {
  System.out.println("-----------------UserMangerImpl---deleteUser---");
  
 }

 


}

代理:springDanaicProxy.java

package com.spring_static;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import sun.org.mozilla.javascript.internal.JavaScriptException;

//使用动 态代理的时候要实现InvocationHandler

public class springDanaicProxy implements InvocationHandler {

 private Object targerObject;
     //要代理必须实现接口,我们已经实现。
 //返回代理
 public Object newProxy(Object targerObject)
 {
     this.targerObject=targerObject;
     return Proxy.newProxyInstance(targerObject.getClass().getClassLoader(),
       
                             targerObject.getClass().getInterfaces()
                             , this);
 }
 
 //当我们调用代理的时候,它会先执行invoke方法
 public Object invoke(Object proxy, Method method, Object[] args)
   throws Throwable {
  checkSecutity();
  Object re=null;
  try {
   re=method.invoke(targerObject, args);
  } catch (Exception e) {
   e.printStackTrace();
   throw new java.lang.RuntimeException(e);
  }
  
  return null;
 }
 private void checkSecutity() {
  System.out.println("------checkSecutity()--------");

 }

}

客户验证:client.java
package com.spring_static;

public class client {

 public static void main(String[] args) {
 
  springDanaicProxy  hander=new springDanaicProxy();
  UserManager userManagaer=(UserManager) hander.newProxy(new UserMangerImpl());
  userManagaer.addUser("123", "123");
  
 }

}

代理总结:就是把分散在java类中的安全检查,模块化。
----------------------aop 相关术语----------------------------

关于安全检查的具体实现称为advice

advice有分类:before advice/after advice

应用有个范围指定。pointcut(add*表示把有的add方法都上安全性检查,而且是before)中有一些表达式。

spring把切面应用到targer目标这个过程,叫支入(Weave)。

在spring中联接点只支持方法,这个方法称为联接点()

-------------aop---事例--------------------
采用antotation的方式:

相关jar包引入。使用aop要在加两个包。astecp--*
使用aop要指定切面
   采用注释的方式定义切面:@Aspect ;定义完切面把它应用到目标;接下来定义切面。
   定义一个要切入的方法,切面定义方法:@Pointcut("execution(* add*(..))")
   Pointcut是一个表达式,描述哪些对象的哪些方法,在这说的是方法,实际上是订阅joinpoint

        Pointcut的名称:private void addALl(){}此方法不能有返回值和参数。这个方法只是一个标识。
       @Pointcut("execution(* add*(..))")  antotation它也支持与或非。
       如:private void addALl(){}

       解释:第一个星表示匹配不管有没有返回值都匹配,add*表示应用到所有的add方法中,(。。)不管它有没有参数;

***使用antotation必要启用:在applicationContext。xml中容器中启用:antotation

在spring  aspect中不存在代理,我说的是只在spring中,在进行程序编译的时候它就加入进去 了


*********************Aop##########################事例:

接口:UserManager.java


package com.spring_static;

public interface UserManager {
  public void addUser(String usename,String password);
 
  public void deleteUser(int id);
 
 
}

实现:UserMangerImpl.java

package com.spring_static;

public class UserMangerImpl implements UserManager{

 @Override
 public void addUser(String usename, String password) {
 System.out.println("-----------------UserMangerImpl---addUser---");
 
 }

 @Override
 public void deleteUser(int id) {
  System.out.println("-----------------UserMangerImpl---deleteUser---");
  
 }

 


}

定义切面UserMangerProxy.java

package com.spring_static;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
//定义切面
@Aspect
public class UserMangerProxy  {

  @Pointcut("execution(* add*(..))")//它也解释:第一个星表示匹配不管有没有返回值都匹配,add*表示应用到所有的add方法中,(。。)
  private void addAll(){}//定义一个范围,这个方法我们不会调它,它中是一个标识
  //定义advice,标识在哪个切入点织入
  @Before("addAll()")
  private void checkSecutity() {
  System.out.println("------checkSecutity()--------");


     }
}


测试层:client.java
package com.spring_static;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class client {

 /**
  * @param args
  */
 public static void main(String[] args) {
  
  BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
  UserManager userManager=(UserManager)factory.getBean("userManager1");
  //userManager采用的是代理类
         userManager.addUser("123", "234");
 
 }

}

----------------------------静态文件配置aop-------------------------------

在文件中不用启用anototion

别的不用做任何修改:
把相应建军立的代理类中的annotation注释去掉


applicationContext。xml中做这样的修改:

  
  <!-- 以下采用文件配置的方式使用aop的 -->

 <bean id="secutityHander"
  class="com.spring_static.UserMangerProxy">
 </bean>
 <bean id="userManager1" class="com.spring_static.UserMangerImpl"></bean>
 
 <aop:config>
  <aop:aspect id="secuity" ref="secutityHander">
   <aop:pointcut id="addAll"
    expression="execution(* add*(..))" />
   <aop:before method="checkSecutity" pointcut-ref="addAll" />
  </aop:aspect>
 </aop:config>
-----****也可以说aop是一个拦截器

总结一下:使用aop应注意几个方面:

 第一个指定切面(aspect)
 第二。。。。。。。(切入点并指定要切入的方法/写好表达式)
 第三。。。。。。。(指定切入点方法/加上切入标识)这几项注前意到了,就没事了

------------------******JOINPOINT*********----------------------------------
通过JointPoint可以拿到提交的参数和相应的方法,拿到这些要在advice方法中加上JointPoint 对象就可以了。

说点jdk动态代理问题:jdk动态代理只支持实现接口的代理。

事例是这样的:

可以理解为它是servlet中的request

代代码如下:package com.spring_static;

import org.aspectj.lang.JoinPoint;

public class UserMangerProxy  {
 
  @SuppressWarnings("unused")
 private void checkSecutity(JoinPoint joinPoint) {
   Object[] args=joinPoint.getArgs();//得到参所传参数数组
   for(int i=0;i<args.length;i++)
   {
    System.out.println(args[i]);
   }
   System.out.println(joinPoint.getSignature().getName());得到切面切入到的方法
  System.out.println("------checkSecutity()--------");

     }
}

-----Aspact本身不用实现接口,但目标对象在默认情况下必须实现接口
 如果没有没有实现接口,必须引入CGLIB库

 如果目标对象实现了接口,默认情况下会采用jdk的动态代理来实现aop

 如何强制使用CGLIB来实现aop类
 
  增加cglib库

  spring可以在jdk动态代理和cglib代理转换。
 
  强制使用cglib
 
  一般情况下我们都会使用jdk动态代理。

  spring引入cglib有这样一好处,如果对代理的对象没有实现接口,那么它就会用cglib代理,如果实现了接口,那么它会默认使用jdk的动态代理。

  <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> 这个标记就是强制使用cglib字节码代理。

  jdk动态代理和cglib字符码生成的区别?


     ***jdk只能对实现接口的类生成代理,不能针对单类。
     *** cglib 针对类的实现代理,因为是继成,主要是对指定的类生成一个子类,覆盖其中的方法,所以该类最好不要声明成final的

------------------------ --hibernate手动编辑事务-------------------------------------------

事例:记录用户的操作,安全操作,这些日志存在数据库,采用两个表,要不全成功,要不全失败

       记录用户什么时候登陆公平,尝试登陆过哪些页面。


 hibernate 中openSession和getCurrentSession的区别?

  ** 1 表示openSession 2表示getCurrentSession

  *** 使用 2 不用手动关闭session 它会在事务提交或回滚事务时自动关闭,使用2 会把session绑定到一个线程上,而openSession则不会。

       而使用 1 时必须手动关闭。

   注意的是:我们使用getCurrentSession要在hibernate.cfg.xml指定一个属性。 
     //如果你使用的是本地(jdbc)事务。
    <property name="hibernate.current_session_context_class">thread</property> 
    //如果使用的是全局事务(jta):就应该这样写:
   <property name="hibernate.current_session_context_class">jta</property>


 手动式编程事事务:

   重点在getCurrentSession上呢。在这里我只把关于两个类的实现粘过来。

   UserManagerIMpl.java

    package com.hibernate_spring_manager;

 

import java.util.Date;

import org.hibernate.Session;

import com.hibernate_spring_model.Log;
import com.hibernate_spring_model.User;
import com.hibernate_spring_util.HibernateHelp;

public class UserMangageImpl implements UserManager {


 public void addUser(User user) {
 
   Session session=null;
  
   try {
    //session=HibernateHelp.getSession();
    //绑定到一个session中当前线程上...全使用getCurrentsession不用手动关闭,它在提交事务和回滚事务时会自动关闭
     session= HibernateHelp.getSessionFactory().getCurrentSession();
     //hibernate开手动开启事务
     session.beginTransaction();
    
     session.save(user);
    
     Log log=new Log();
     log.setType("安全进入!");
     log.setDatelMes("小高进入系统");
     log.setCurrentdate(new Date());
     LogManager logManager=new LogManagerImpl();
     logManager.addLog(log);
    
    
     //手动提交事务
     session.getTransaction().commit();
  } catch (Exception e) {
   //有异常回滚事务
   session.getTransaction().rollback();
  }
     
 }

}

  LogManagerIMpl.java


package com.hibernate_spring_manager;

import com.hibernate_spring_model.Log;

import com.hibernate_spring_util.*;

public class LogManagerImpl implements LogManager {

 @Override
 public void addLog(Log log) {
   HibernateHelp.getSessionFactory().getCurrentSession().save(log);

 }

}

//此例模拟的是:用户操作会补跟踪记录到相应的表中去。它们的操作是同步的。

//我们可以在session保存完user信息时,做个假异常,那么它就会回滚,控制台会打印一语句,可是当我们去表里看的时候,哪个表里也没有数据的。

//因为我们把它绑定到一个线程上了,一个操作失败,另一个也会失败。

------------------------------spring_hibernate声明式事务----------------------------

首先我们要了解事务的传播特性:

  
隔离级会别:


声明式事务的配置:在application.xml中:


 <!-- 把hibernate注进去 classpath是spring的一个协议.创建sessionFactory-->

     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="configLocation">
             <value>classpath:hibernate.cfg.xml</value>
       </property>
     </bean>
     <!-- 配置事务管理器;注入sessionFactory -->
     <bean id="transManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
       <property name="sessionFactory">
              <ref bean="sessionFactory"/>
       </property>
     </bean>
    
     <!-- 配置事务的传播特性 -->
    
     <tx:advice id="txAdvice" transaction-manager="transManager">
        <tx:attributes>
          <tx:method  name="add*" propagation="REQUIRED"/>
          <tx:method name="del*" propagation="REQUIRED"/>
          <tx:method name="modify*" propagation="REQUIRED"/>
          <tx:method name="*" read-only="true"/><!-- 把不用事务管理的配置成只读事务,这样会提高性能 -->
        </tx:attributes>
      </tx:advice>
     
      <!-- 哪些类的哪些方法参与事务 -->
     
      <aop:config>
     
      <aop:pointcut id="allManagerMethod" expression="* com.*.*(..)"/>
     
      <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
      </aop:config>
    

------------------------------------------spring-hibernate ----管理session-----------------

运行时异常通常是不可恢复的,而一般性异常是可恢复的,我们可以看到。在胆子ejb中它的回滚。。异常,都是死的,是我们人为不可控的

,而spring中提供了,我们可以人为控制出现什么问题就抛出什么异常。

spring 管理接收hibernate事务配置:

 事例如下:

applicationContext-common.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"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  <!-- 把hibernate注进去 classpath是spring的一个协议.创建sessionFactory-->

     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="configLocation">
             <value>classpath:hibernate.cfg.xml</value>
       </property>
     </bean>
     <!-- 配置事务管理器;注入sessionFactory -->
     <bean id="transManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
       <property name="sessionFactory">
              <ref bean="sessionFactory"/>
       </property>
     </bean>
    
     <!-- 配置事务的传播特性 -->
    
     <tx:advice id="txAdvice" transaction-manager="transManager">
        <tx:attributes>
          <tx:method  name="add*" propagation="REQUIRED"/>
          <tx:method name="del*" propagation="REQUIRED"/>
          <tx:method name="modify*" propagation="REQUIRED"/>
          <tx:method name="*" read-only="true"/><!-- 把不用事务管理的配置成只读事务,这样会提高性能 -->
        </tx:attributes>
      </tx:advice>
     
      <!-- 哪些类的哪些方法参与事务 -->
     
      <aop:config>
     
      <aop:pointcut id="allManagerMethod" expression="execution(* com.hibernate_spring_manager.123*.*(..))"/>
     
      <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
      </aop:config>
    
</beans>

ioc注入层(分支);
applicationContext-beans.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"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 
   <bean id="userManager" class="com.hibernate_spring_manager.UserMangageImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
    <property name="logManager" ref="logManager"></property>
   
   </bean>  
   <bean id="logManager" class="com.hibernate_spring_manager.LogManagerImpl">
   <!-- 它要一个数据源 -->
   <property name="sessionFactory" ref="sessionFactory"/>  
   </bean>  
    
</beans>

持久层定写法:

UserMangageImpl.java
  


package com.hibernate_spring_manager;

 

import java.util.Date;

import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hibernate_spring_model.Log;
import com.hibernate_spring_model.User;
import com.hibernate_spring_util.HibernateHelp;

public class UserMangageImpl extends HibernateDaoSupport implements UserManager {

 LogManager logManager;
 
 public void addUser(User user) {
 
   try {
     this.getHibernateTemplate().save(user);
    
     Log log=new Log();
     log.setType("安全进入!");
     log.setDatelMes("小高进入系统");
     log.setCurrentdate(new Date());
     logManager.addLog(log);
  } catch (Exception e) {
   
   e.printStackTrace();
  }
     
 }

 public void setLogManager(LogManager logManager) {
  this.logManager = logManager;
 }

}


 LogManagerImpl.java


     package com.hibernate_spring_manager;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hibernate_spring_model.Log;

public class LogManagerImpl extends HibernateDaoSupport implements LogManager {

 @Override
 public void addLog(Log log) {
         this.getHibernateTemplate().save(log);
 }

}

测试层:
client。java

package com.hibernate_spring_client;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import net.sf.cglib.proxy.Factory;

import com.hibernate_spring_manager.UserManager;
import com.hibernate_spring_manager.UserMangageImpl;
import com.hibernate_spring_model.User;

public class Client {

 public static void main(String[] args) {
  
   User user=new User();
   user.setName("gao");
     
   BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext-*.xml");
  
   UserManager userManager=(UserManager)factory.getBean("userManager");
   userManager.addUser(user);

 }

}
------------------------------------------------ssh------------------框架集成----------------------------


spring与struts2集成:提供两种集成方案。

 第一种方案:struts前端控制器:截取请求然后纷发。struts是通过actionServlet来实现前端控制器的。它能过struts.config来纷发。
 
  先来了解一下struts的流程“  brows----*request*-----actionServlet----返回----*struts.xml*-------action-----返回----model
                               -                          =
                               -                          =
                               ----------response------------- 返回web组件

    
spring管理事务的重要标记是:<bean>

spring 对struts进行管理是从action介入的.取得beanFactoty,从ioc容器中获取业务对象,调用业务方法。

第一种方案,无法离开spring配置加载,而且,beanFactory是重量级会的东西,不宜在action出现。它用的是依赖查找。在action发生了直接代码查找。


第二种方案:就是不把读取applicationContext。xml文件factory重量级的东西放到代码里,采用spring注入的方式。
 
一般就是接口注入。

 

-----------------------------jsp高级版本和普通版本的区别-------------------------

高级版本拿到了根,也就是webRoot,放到了base标签,作用就是从根开始,使用根目录,就回既退。