我是Spring和hibernate的新手,我遇到了这个问题。我一直在寻找修复但是虽然有很多问题,但它们似乎并没有解决我的问题。我正在使用带有hibernate 3.6.9的spring 3.1.0并使用spring mvc制作一个web应用程序。经过大量的环顾四周,我设法用以下配置来解决它
的web.xml
<listener> <description>Spring context loader</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
的applicationContext.xml
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<context:annotation-config/>
<!-- Scans within the base package of the application for @Components to
configure as beans -->
<!-- @Controller, @Service, @Configuration, etc. -->
<context:component-scan base-package="com.emumba.cricketcalendar" />
<import resource="hibernate-context.xml"/>
冬眠-context.xml中
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.emumba.cricketcalendar.domain.Match</value>
<value>com.emumba.cricketcalendar.domain.Ground</value>
<value>com.emumba.cricketcalendar.domain.Umpire</value>
<value>com.emumba.cricketcalendar.domain.Country</value>
<value>com.emumba.cricketcalendar.domain.CricketStatus</value>
<value>com.emumba.cricketcalendar.domain.Series</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
然后我把@Transactional放在我的服务上,这个例外消失了。但随后我的自动装配停止工作,自动装配的注释不起作用,具有自动装配属性的bean开始抛出异常。我从我的服务中删除@transactional注释,它再次开始工作,但“没有hibernate会话绑定到线程”异常返回
所以我真的很困惑,任何帮助都会非常感激
编辑服务代码
@服务(值= “calendarManager”) 公共类CalendarMangerImpl实现CalendarManager {
@Autowired
@Qualifier("matchDao")
public MatchDaoHibernate matchDao;
@Override
public List<Match> getAllMatches() {
List<Match> matches=new ArrayList<Match>();
matches=matchDao.findAll();
return matches;
}
}
答案 0 :(得分:2)
使用服务类参考其接口而不是实际类,因为spring默认使用基于接口的代理
Spring AOP defaults to using standard J2SE dynamic proxies for AOP proxies. This enables
any interface (or set of interfaces) to be proxied.
答案 1 :(得分:1)
我将建议首先go through this简单示例清除所有概念。它也将在未来帮助你。