Xml @在我的Spring工具套装中不工作
我在Spring工具套装中的项目有问题@事务性批注不起作用 以下是我的项目结构:- web.xml:-Xml @在我的Spring工具套装中不工作,xml,spring,hibernate,spring-mvc,Xml,Spring,Hibernate,Spring Mvc,我在Spring工具套装中的项目有问题@事务性批注不起作用 以下是我的项目结构:- web.xml:- <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creat
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<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/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.project.dateandcrud" />
<!-- Root Context: defines shared resources visible to all other web components -->
<context:property-placeholder location="classpath:datasource-cfg.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${ds.driverClassName}" />
<property name="url" value="${ds.url}" />
<property name="username" value="${ds.username}" />
<property name="password" value="${ds.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.project.dateandcrud.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>
</bean>
<!-- Transaction Manager to make Transaction Support-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
当我不使用@Transactional注释时,我的项目工作正常。但是当使用注释时,它不起作用
我的servlet-context.xml或root-context.xml文件中是否存在任何问题
我已经试过搬家了
这将从selvlet context.xml转换到root context.xml,但这也不起作用
请告诉我可能的故障。不要试图自己打开/关闭hibernate
会话
,让spring tx
来处理这个问题:
@Transactional
public void addProfile(Profile p) {
final Session session = sessionFactory.getCurrentSession();
session.save(p);
}
并将
从servlet context.xml
移动到根context.xml
配置
这应该可以解决您的问题。这是我找到的解决方案……只需将
从根context.xml移动到servlet context.xml。它不起作用意味着什么?是否有异常弹出?@MaciejKowalski没有错误弹出窗口,只是它没有将数据插入数据库…但是当我这样做时,出现以下错误弹出窗口:-无法获取当前的事务同步会话thread@Space是的,你说得对。我只是仔细查看了你的配置并更新了我的答案。经过一些调整后,终于成功了…请查看我的答案…谢谢Bodhdan
@Transactional
public void addProfile(Profile p) {
final Session session = sessionFactory.getCurrentSession();
session.save(p);
}