所以我的春季教育仍在繼續。目前,我正在嘗試學習一些註釋和它們爲Spring 3帶來的東西。因此,我有一個迷你web應用程序,可以連接到數據庫並通過表單輸入內容並顯示記錄等。一切正常。我決定嘗試讓Spring自動檢測我標記爲@Transactional的服務bean,但這樣做會阻止應用程序保存到數據庫。所以:Spring @Transactional不能與其他註釋一起使用?
@Transactional
public class ReservationServiceImpl implements ReservationService {
有效。我在springcourt-data.xml文件中有這個類的bean聲明。沒問題。當我這樣做雖然:
@Transacational
@Service("reservationService")
public class ReservationServiceImpl implements ReservationService {
它不再起作用。我確實在springcourt-servlet.xml文件中有
<context:component-scan base-package="com.springcourt" />
。那麼誰能告訴我我搞砸了什麼?我所做的只是爲該類添加另一個註釋,並從xml文件中移除該bean定義,並且不再將數據保存到數據庫。我仍然可以從數據庫查詢記錄和內容,但顯然它使用自動檢測的服務bean。
下面是配置文件:
springcourt-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.springcourt" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.springcourt.web.ReservationBindingInitializer" />
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
和:
springcourt-data.xml
<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:p="http://www.springframework.org/schema/p"
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-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="admin" />
<property name="initialSize" value="5" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven />
<bean id="reservationService" class="com.springcourt.service.ReservationServiceImpl"/>
</beans>
幾件事情你爲什麼要在上下文中聲明reservationService並標註它@服務,也是上下文組件掃描添加到第二個方面在您發佈的「And」後列出的文件。 –
您在「@Transacational」中有錯字。將它改正爲「@Transactional」 – buddamus