20120321spring

Spring

1,spring和hibernate的整合

  1. 利用Spring IoC容器创建SessionFactory

Applicationcontext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

(添加aop和tx名称空间)

<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-3.0.xsd

    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

 

      <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

            <property name="jndiName" value="java:OracleDS" />

      </bean>

(声明sessionFactory)

      <bean id="sessionFactory"

            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

            <property name="dataSource" ref="dataSource"></property>

            <property name="mappingResources">

                  <list>

                        <value>org/wed/domain/LeaveInfo.hbm.xml</value>

                        <value>org/wed/domain/Person.hbm.xml</value>

                        <value>org/wed/domain/LoginInfo.hbm.xml</value>

                        <value>org/wed/domain/LeaveHistory.hbm.xml</value>

                  </list>

            </property>

            <property name="hibernateProperties">

                  <value>

                        hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

                        hibernate.hbm2ddl.auto=update

                        hibernate.show_sql=true

                        hibernate.format_sql=true

                  </value>

            </property>

      </bean>

(声明事务管理器,注入sessionFactory属性)

      <bean id="transactionManager"

            class="org.springframework.orm.hibernate4.HibernateTransactionManager">

            <property name="sessionFactory" ref="sessionFactory" />

      </bean>

(通过 <tx:advice>定义事务通知)

      <tx:advice id="txAdvice" transaction-manager="transactionManager">

            <tx:attributes>

                  <tx:method name="get*" read-only="true"/>

                  <tx:method name="*" />

            </tx:attributes>

      </tx:advice>

      <aop:config>

            <aop:pointcut id="myPointcut" expression="bean(epService)||bean(hrService)||bean(olService)||bean(dmService)" />

            <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />

      </aop:config>

 

      <bean id="myDao" class="org.wed.dao.MyDao" abstract="true">

            <property name="sessionFactory" ref="sessionFactory"/>

      </bean>

      <bean id="personDao" class="org.wed.dao.PersonDaoImpl" parent="myDao"/>

      <bean id="loginInfoDao" class="org.wed.dao.LoginInfoDaoImpl" parent="myDao"/>

      <bean id="leaveInfoDao" class="org.wed.dao.LeaveInfoDaoImpl" parent="myDao"/>

      <bean id="leaveHistoryDao" class="org.wed.dao.LeaveHistoryDaoImpl" parent="myDao"/>

     

      <bean id="myService" class="org.wed.service.MyService">

            <property name="personDao" ref="personDao"></property>

            <property name="leaveInfoDao" ref="leaveInfoDao"></property>

            <property name="loginInfoDao" ref="loginInfoDao"></property>

            <property name="leaveHistoryDao" ref="leaveHistoryDao"></property>

      </bean>

      <bean id="epService" class="org.wed.service.EpServiceImpl" parent="myService"/>

      <bean id="hrService" class="org.wed.service.HrServiceImpl" parent="myService"/>

      <bean id="olService" class="org.wed.service.OlServiceImpl" parent="myService"/>

      <bean id="dmService" class="org.wed.service.DmServiceImpl" parent="myService"/>

</beans>

  1. Hibernate DAO开发

不要使用spring的HibernateDaoSupport,而是直接使用hibernate来配置DAO。

作为父类的MyDao:

public class MyDao {

      protected SessionFactory sessionFactory;

 

      public void setSessionFactory(SessionFactory sessionFactory) {

            this.sessionFactory = sessionFactory;

      }    

}

每个类的Dao配置接口类:

public interface PersonDao {

      public Person get(String id);

      public String save(Person person);

      public void update(Person person);

      public void delete(String id);

      public void delete(Person person);

      public List<Person> find(String hql, String value);

      public List<Person> search(DetachedCriteria dc, int pageNow);

      public List search(DetachedCriteria dc);

}

和实现类:

public class PersonDaoImpl extends MyDao implements PersonDao {

public Person get(String id) {

            return (Person) this.sessionFactory.getCurrentSession().get(

                        Person.class, id);

   }

}

  1. 使用Spring声明式事务管理(见applicationContext.xml中注释)

在applicationContext.xml中添加aop和tx名称空间(文件头部声明);

声明事务管理器,注入sessionFactory属性

 

2,下载spring需要的第三方包:http://search.maven.org/

 

 

3,

错误:
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

原因:缺少aopalliance.jar包。

 错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0': Cannot resolve reference to bean 'myPointcut' while setting bean property 'pointcut'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myPointcut': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
原因:缺少aspectjtools.jar包。

错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.transaction.spi.TransactionFactory]

错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.service.jdbc.connections.spi.ConnectionProvider]
原因:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
因为使用的是hibernate4,所以应该改为:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
同样,transactionManager的class也要改成hibernate4:
<bean id="transactionManager"
 class="org.springframework.orm.hibernate4.HibernateTransactionManager">
 <property name="sessionFactory" ref="sessionFactory" />
</bean>

错误:

org.springframework.orm.hibernate4.SessionHolder cannot be cast to org.springframework.orm.hibernate3.SessionHolder
原因:
org.springframework.orm.hibernate4.support中没有HibernateDaoSupport类,即用了hibernate4之后,不能再用HibernateDaoSupportHibernateTemplate类了
改用原生的hibernate语句。

错误:
运行到List pl = leaveInfoDao.findByRequestorId(uId);时就出如下错误:
java.lang.NullPointerException
原因:bean的某些错误导致未能注入。

 

3,当类A,B既是父子类又是SPRING配置文件中的继承关系的好处:
在配置xml文件中,让父类接受bean注入,子类就不必在配置文件中写相同的bean注入了;
在父类java文件中,将相应的注入属性设定为protected,写入setter方法,
在子类java文件中,不必再声明父类中声明的属性及写相应的setter方法了,而是可以直接调用。

 

4,

springaop代理默认是JDK动态代理,该代理的对象必须是实现了接口的对象,如果没有实现接口(会采用CGLIB代理),而在类路径中又没有CGLIB包,将出错。可以通过使用abstract="true"来解决这个问题(有什么隐患?)。

<bean id="myService" class="org.wed.service.MyService" abstract="true">

            <property name="personDao" ref="personDao"></property>

            <property name="leaveInfoDao" ref="leaveInfoDao"></property>

            <property name="loginInfoDao" ref="loginInfoDao"></property>

      </bean>

 

 

 

posted @ 2012-03-21 20:44  lastren  阅读(1332)  评论(1编辑  收藏  举报