SSH使用Annotation整合问题

1.org.hibernate.AnnotationException: No identifier specified for entity: com.yangsang.is.User

明明我有加了Annotation @Id了啊。并且也有让Spring管理这些Model了啊。

解决方案:自己傻逼了。@Id写在setter方法上了,@Id应该写在getter方法上。Spring的@Autowired才应该写在setter上。

2.Hibernate纯采用Annotation开发。并且,整合Spring时无Hibernate.cfg.xml与XXX.hbm.xml文件。可采用如下模板:

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" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	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/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd 
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">
		<!-- 注意上面的default-autowire="byName",如果没有这个声明那么HibernateDaoSupport中的sessionFactory不会被注入 -->

	
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
		<property name="username" value="scott"></property>
		<property name="password" value="tiger"></property>
	</bean>
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		
		<property name="dataSource" ref="dataSource"/>
		
       	<property name="packagesToScan" value="com.yangsang.is.model"></property>
       
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>
	
	
	<context:annotation-config></context:annotation-config>
	<context:component-scan base-package="com.yangsang.is"></context:component-scan>
	
	
	
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<aop:config>
		<aop:advisor pointcut="execution(* com.yangsang.spring.service.*Service.*(..))" advice-ref="txAdvice"/>
	</aop:config>

	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true"/>
			<tx:method name="query*" read-only="true"/>
			<tx:method name="find*" read-only="true"/>
			<tx:method name="load*" read-only="true"/>
			<tx:method name="*" rollback-for="Exception"/>
		</tx:attributes>
	</tx:advice>
	
	
	
</beans>
 

3.struts2的配置路径中默认是在/WEB-INF/classes。

方法一:

更改struts.xml的位置要注意还要补充struts-default.xml,struts-plugin.xml。不能只写自己自定义的 struts.xml的位置。

web.xml

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
        	<param-name>config</param-name>
        	<param-value>struts-default.xml,struts-plugin.xml,../struts-xml/struts.xml</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

方法二:

web.xml文档

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
        	<param-name>config</param-name>
        	<param-value>../struts-xml/struts.xml</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

 struts.xml文档

<struts>
<include file="struts-default.xml" />
.....
</struts>

 注意:用<include>引用的xml文件也必须是完成的struts2的配置。实际上<include>在引用时是单独解析的xml文件,而不是将被引用的文件插入到struts.xml文件中。

4.更改spring配置文件的位置方法:

web.xml文档

	<context-param>
   	 	<param-name>contextConfigLocation</param-name>
    	<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

5.java.lang.OutOfMemeryError:PerGen space。今天可是遇到太多次了啊!!!

CSDN的Jerry_Ran 写道
这一段时间,Eclipse总是死掉,几乎是稍微操作快一点就会死掉,几分钟一次,搞得人郁闷至极。浪费了不少时间,在网上搜了下,看到很多朋友也出现类似的情况,在网上求救,但是网上的办法都只是说通过修改eclipse.ini文件来加大Eclipse的内存。

自己试了下,发现不管用。今天,又死掉了,不过爆出一个经常出现的错误,大意是:permgen space导致内存溢出。实在忍无可忍,上网一搜,发现网络真是个好东西,尤其是对我们这种自学者而言,无疑是授业解惑的良师益友。

这里总结一下自己的经验和网友的经验,希望能够对受此问题折磨的朋友有所参考价值。

解决办法如下:

在eclipse.ini配置文件中加上以下两行
-XX:PermSize=128M
-XX:MaxPermSize=128M

加上上2行后,我的eclipse.ini文件如下所示:

-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
512m
-vmargs
-Xms256m
-Xmx512m
-XX:PermSize=128M
-XX:MaxPermSize=128M

这里的内存大小根据自己的物理内存情况来决定吧。

从网上的资料看PermSize大一点肯定更好,而且最好是设置PermSize和MaxPermSize一样大。理由如下:
PermSize 和MaxPermSize如果设置为相同还可以在一定程度上提高性能,因为,PermSize在不断的变化中会需要转移其中的数据。如果固定了以后,则可以减少每次扩大PermSize带来的性能损失。



1、PermGen space简介

PermGen space的全称是Permanent Generation space,是指内存的永久保存区域OutOfMemoryError: PermGen space从表面上看就是内存益出,解决方法也一定是加大内存。

说说为什么会内存益出:
(1)这一部分用于存放Class和Meta的信息,Class在被 Load的时候被放入PermGen space区域,它和和存放Instance的Heap区域不同。
(2) GC(Garbage Collection)不会在主程序运行期对PermGen space进行清理,所以如果你的APP会LOAD很多CLASS的话,就很可能出现PermGen space错误。这种错误常见在web服务器对JSP进行pre compile的时候。

如果你的WEB APP下都用了大量的第三方jar,其大小超过了jvm默认的大小(4M)那么就会产生此错误信息了。


解决方法: 手动设置MaxPermSize大小

修改TOMCAT_HOME/bin/catalina.sh,在echo "Using CATALINA_BASE: $CATALINA_BASE"上面加入以下行:
JAVA_OPTS="-server -XX:PermSize=64M -XX:MaxPermSize=128m
建议:将相同的第三方jar文件移置到tomcat/shared/lib目录下,这样可以减少jar 文档重复占用内存。
 

特别感谢:

基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发(2)

struts2设置加载非默认路径的struts.xml文件的方法

PermGen space简介




猜你喜欢

转载自jxyang.iteye.com/blog/1472085