春季4,MVC和JPA集成

时间:2018-10-14 19:51:29

标签: spring spring-mvc jpa

我在春天有点生锈。 我有一个使用JPA persitence的proyect,我需要这样做,以便它在rest 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>

applicationContext.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的工作原理上。

在此应用程序中包括rest servlet的最佳方法是什么? 谢谢

1 个答案:

答案 0 :(得分:0)

好吧,我最终弄清楚了,我将applicationContext.xml放在了资源文件夹的外面,所以我从来没有安装过服务bean。 我花了很多时间来解决这个问题,所以我认为我可能缺少一些重要的日志输出。我只是在看tomcat的输出。因此,在这方面的任何帮助将不胜感激。