3

所以我的春季教育还在继续。目前我正在尝试学习一些注释以及它们为 Spring 3 带来的东西。所以我有一个迷你 web 应用程序,它可以连接到数据库并通过表单放入内容并显示记录等。一切正常。我决定尝试让 Spring 自动检测我标记为 @Transactional 的服务 bean,但这样做会阻止应用程序保存到数据库。所以:

@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 定义,它不再将数据保存到数据库中。我仍然可以从数据库中查询记录和东西,但很明显它正在使用自动检测的服务 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>
4

5 回答 5

3

当您使用@Service 和组件扫描时,bean 是由调度程序 servlet (mvc) 创建的上下文创建的。由于事务:注释驱动是在根应用程序上下文中定义的,因此它不适用于 servlet 上下文中的 bean。您可以通过删除 @Service 并将 bean 定义移动到 servlet 上下文文件来验证这一点——您应该会看到相同的结果。

当您不使用组件扫描时 - bean 是在根应用程序上下文的 XML 中定义的。

修复方法是将 Web 层中的组件扫描标记更改为仅包含 Web 层类 - 通过使用不同的基本包或使用包含/排除过滤器。在根应用程序上下文中为其他 bean 添加另一个组件扫描。

查询可能有效,因为您可能配置了 OpenEntityManagerInViewInterceptor / Filter。

于 2011-10-14T20:29:41.703 回答
1

由于您可以通过同一个 bean 查询 DB,因此您的@Transactional工作或您经常会遇到异常(至少使用 Hibernate)。很可能在保存操作中,您会遇到一些导致事务回滚的运行时异常。尝试找出异常是什么,然后从那里开始。


更新

要查看是否@Transactional已应用,请从方法内部打印堆栈跟踪。如果您看到带有大量事务拦截器的长堆栈跟踪,则意味着事务方面有效。

于 2011-10-14T18:58:42.233 回答
1

我有同样的问题并解决它。

首先,您必须将您的 context:component-scan 分离到 web 和数据级别,如下所示:

<!--in springcourt-servlet.xml -->
<context:component-scan base-package="com.springcourt.web" />

<!--in springcourt-data.xml -->
<context:component-scan base-package="com.springcourt.dao" />

第二次添加到 springcourt-data.xml

<aop:aspectj-autoproxy/>

我希望它会有所帮助

于 2013-07-30T06:51:59.670 回答
0

试试这个

  • 将上下文组件扫描添加到 spring-court-data.xml

      <context:component-scan base-package="com.springcourt" />
    
  • 单独测试服务,创建一个 JUNIT 测试类似这样的东西

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"classpath:springcourt-data.xml"})
    @Transactional
    public class ReservationServiceImplTest()
    {
         @Autowired
         ReservationServiceImpl service;
    
         @Test
          public void validateContext()
          {
              Assert.assertNotNull(service); 
          }
    
          @Test
          @Rollback(false)
          public void save()
          {
             service.save(data);
          }
    
    }
    
于 2011-10-14T20:21:28.217 回答
0

您所拥有的一个非常好的替代方法是使用以下内容:`

 @Configuration
 @EnableAspectJAutoProxy(proxyTargetClass=true)
 public class AppConfig {
     (...)
 }

您需要做的是将所有内容注入同一范围内。一种方法是更改​​ ApplicationContext xml,如前所述,另一种方法是像 Spring 代理一样使用 CGLIB,以便您获得基于子类的代理并在其中编写您的 bean 实现和定义。

进一步阅读:

于 2016-05-09T18:52:19.967 回答