だから私の春の教育は続きます。現在、私はいくつかの注釈とそれらが Spring 3 にもたらすものを学ぼうとしています。そのため、DB に接続し、フォームを介して情報を入力し、レコードなどを表示できるミニ webapp を取得しました。すべて正常に動作します。@Transactional としてマークしたサービス Bean を Spring に自動検出させることにしましたが、そうするとアプリが DB に保存されなくなります。そう:
@Transactional
public class ReservationServiceImpl implements ReservationService {
それは機能します。springcourt-data.xml ファイルに、このクラスの Bean 宣言があります。問題はありません。私がこれを行うとき:
@Transacational
@Service("reservationService")
public class ReservationServiceImpl implements ReservationService {
それはもはや機能しません。そして、私は持っています
<context:component-scan base-package="com.springcourt" />
springcourt-servlet.xml ファイル内。それで、誰かが私が台無しにしていることを教えてもらえますか? このクラスに別の注釈を追加し、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>