简体   繁体   中英

Spring 4, MVC and JPA integration

I'm kinda rusty in spring. I have a proyect using JPA persitence, and i need to make it so it return JSON on a rest API.

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 Class

@Service
public class CalculatorService {

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

    @Autowired
    RubroRepository rubroRepo;
 ...

I created a RestController following many guides in internet, but always got problems, like, the autowired to the class CalculatorService dosnt work.

Whats is the best way to include a rest servlet in this application? Thanks

Well, i figured it out in the end, i was putting the applicationContext.xml outside of the resource folder, so i never had the services bean in place. it took me a lot of time to figure this out, so i think i may be missing some important log output. I'm only watching tomcat output on this. so any help in this regard would be appreciated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM