spring - 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 的最佳方法是什么?谢谢
解决方案
好吧,我最终想通了,我将 applicationContext.xml 放在资源文件夹之外,所以我从来没有安装过服务 bean。我花了很多时间才弄清楚这一点,所以我想我可能会遗漏一些重要的日志输出。我只看这个的tomcat输出。因此,我们将不胜感激这方面的任何帮助。
推荐阅读
- apache-spark - How do i pass Env variales to a spark job?
- c# - Unable to read file in Hololens
- c# - Azure 函数在 Linux 上出现“无法启动的侦听器”错误
- kubernetes - How to list microk8s' containers with microk8s.ctr
- python - 如何用函数计算python中的唯一单词?
- c# - DatePicker 中日期的垂直对齐方式
- machine-learning - 如何限制 LSTM 模型中的序列预测以匹配特定模式?
- php - How to use ajax for saving image with php
- xpath - 如何更新具有最大子属性的 XML 属性节点?
- visual-studio-code - Keyboard shortcut to switch from source control "diff" mode back into normal editing?