首页 > 解决方案 > Spring 4、MVC 和 JPA 集成

问题描述

春天我有点生疏了。我有一个使用 JPA 持久性的项目,我需要让它在休息 API 上返回 JSON。

web.xml

<display-name>display name</display-name>

 <!-- Spring -->
<context-param>
    <description>Spring context file</description>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:/META-INF/spring/applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

应用程序上下文.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


    <context:annotation-config/>
    <context:load-time-weaver/>
    <!-- escanea las clases del package services buscando componentes -->
    <context:component-scan base-package="cl"/>

    <tx:annotation-driven transaction-manager="transactionManager"/>
    <!-- MYSQL -->
    <bean id="datasource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    </bean>

    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect">
        <!--  <property name="databasePlatform" value="org.eclipse.persistence.platform.database.H2Platform" /> -->
        <!-- <property name="showSql" value="true" />  -->
    </bean>
    <!--  Entity Manager Factory -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="datasource"></property>
        <property name="persistenceUnitName" value="persistenceUnit"></property>
        <property name="jpaDialect" ref="jpaDialect"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"/>
        </property>
        <!--
       <property name="jpaPropertyMap">
           <map>
               <entry key="eclipselink.weaving" value="false" />
           </map>
       </property>
        -->
    </bean>

    <!--  Transaccion manager -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"></property>
    </bean>

    <!-- JPA Repositories -->
    <jpa:repositories base-package="cl.repositories"></jpa:repositories>
    <!-- traductor de excepciones de repo -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>


    <!-- Velocity -->

</beans>

服务等级

@Service
public class CalculatorService {

    private transient Logger logger = LoggerFactory.getLogger(CalculatorService.class);

    @Autowired
    RubroRepository rubroRepo;
 ...

我按照互联网上的许多指南创建了一个 RestController,但总是遇到问题,比如自动连接到类 CalculatorService dosnt 工作。

在这个应用程序中包含一个 rest servlet 的最佳方法是什么?谢谢

标签: springspring-mvcjpa

解决方案


好吧,我最终想通了,我将 applicationContext.xml 放在资源文件夹之外,所以我从来没有安装过服务 bean。我花了很多时间才弄清楚这一点,所以我想我可能会遗漏一些重要的日志输出。我只看这个的tomcat输出。因此,我们将不胜感激这方面的任何帮助。


推荐阅读