常见问题及解决方案
现象1:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name
'org.springframework.aop.support.DefaultPointcutAdvisor' defined in
null: Can't resolve reference to bean 'txAdvice' while setting property
'advice'; nested exception is
原因:缺少Spring开头的各种包,到Spring2.0 src中编译新的dist目录出来,拷贝全部的springxxx.jar到jpetstore/WEB-INF/lib下。OK。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
现象2:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sessionFactory' defined in resource
[/WEB-INF/dataAccessContext-hibernate.xml] of ServletContext:
Instantiation of bean failed; nested exception is
java.lang.NoClassDefFoundError: javax/transaction/TransactionManager
java.lang.NoClassDefFoundError: javax/transaction/TransactionManager
原因:缺少jta.jar,OK
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
现象3:
java.lang.NoClassDefFoundError: org/dom4j/Attribute
缺dom4j.jar
java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
缺ehcache.jar
java.lang.NoClassDefFoundError: net/sf/cglib/core/KeyFactory
缺cglib-full.jar
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
现象4:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sessionFactory' defined in resource
[/WEB-INF/dataAccessContext-hibernate.xml] of ServletContext:
Initialization of bean failed; nested exception is
net.sf.hibernate.MappingException: could not instantiate id generator
net.sf.hibernate.MappingException: could not instantiate id generator
原因:
hbm中id字段的定义为没有定义的sequesce,修改为inc
<id
name="id"
type="string"
column="userid"
>
<generator class="net.sf.hibernate.id.IncrementGenerator"/> // 原来是sequence.
</id>
这样就可以了。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
现象6:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name '/shop/addItemToCart.do' defined in
ServletContext resource [/WEB-INF/petstore-servlet.xml]: Can't resolve
reference to bean 'petStore' while setting property 'petStore'; nested
exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
bean named 'petStore' is defined
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'petStore' is defined
原因:
我使用Struts作为action,当然就不需要使用Spring WEB framework,因此也不需要与之配套的配置文件例如,petstore-servlet.xml,
还有remote-servlet.xml ,但是Spring仍旧加载此两个文件,没办法,只好将此两个文件挪动到别的地方,但是又出现找不到这两个
文件的IO异常,没办法,只好又拷贝回来,然后把文件内容晴空。
终于好用了。
原
因就是,只要你在web.xml中定义了这个servlet,他就要去找此servlet名字对应的配置文件(servletname-
servlet.xml),找不到就出错。ok,现在注释掉web.xml中名字为petstore的servlet定义和影色,删除petstore-
servlet.xml文件,哈哈,这下不报告错误了。
同理,注释名字为remote的servlet,删除remote-servlet.xml后,重新启动tomcat,这下不报告任何错误了。呵呵。原来如此啊。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
现象:
java.lang.NullPointerException
srx.test.testhibernate.UserDAO.getAll(UserDAO.java:9)
srx.test.struts.action.UserAction.execute(UserAction.java:20)
原因:
Action原来
UserDAO // 这个类OK
public class UserDAO extends HibernateDaoSupport {
public List getAll() {
return this.getHibernateTemplate().find("select aa from Account aa");
}
}
//这个类需要修改
public class UserAction extends ActionSupport {
UserDAO dao = new UserDAO();
List accounts = dao.getAll();
。。。
}
修改:
//UserDAO dao = new UserDAO();
WebApplicationContext wac = this.getWebApplicationContext();
UserDAO dao = (UserDAO) wac.getBean("userDAO");
OK
现象:
UserAction中修改:
//UserDAO dao = (UserDAO) wac.getBean("userDAO");
UserDAO dao = (UserDAO) wac.getBean("userDAOProxy"); //@1
配置文件中增加:
<bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target"><ref local="userDAO"/></property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
这样运行时@1处抛出ClassCastException.
===================================================================================================
原因:
仔细看指南0.8版本145页所描述的事情.UserDAO虽然只继承了HibernateDAOSupport,看来没有事先任何接口,但是
HibernateDAOSupport实现了InitiralBean接口,所以UserDAO也相当于实现了这个接口。
因此本例按照实现了某个接口的DAO来处理,处理过程如下:
因为他实现了接口A,所以getBean得到一个实现了A的实例,并非继承UserDAO。所以用UserDAO转型会失败。
如果UserDAO没有实现接口A,那么getBean会得到一个UserDAO的子类实例,这时可以用UserDAO来转型。
综上所诉,UserDAO必须实现IUserDAO接口,并且用IUserDAO转型才行.
因为UserDAO必须继承HibernateDAOSupport,所以没有办法不实现任何接口.
本问题和AOP有关。因为对AOP还不熟悉,所以需要看看。