私は春の教育を続けています。現在私は、Spring 3にもたらすアノテーションやいくつかのことを学びたいと思っています。そのため、DBに接続してフォームを通してデータを入れたり、レコードを表示できるミニWebアプリケーションがあります。すべてうまく動作します。私は@TransactionalとしてマークしたサービスBeanを自動的に検出するようにSpringを試してみることにしましたが、これを実行するとアプリケーションがDBに保存されなくなります。したがって: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定義を削除するだけで、データはDBに保存されなくなります。私はまだDBからレコードとデータを照会できますが、明らかに自動検出されたサービス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"に入力ミスがあります。それを "@トランザクション"に修正してください – buddamus