【问题标题】:EclipseLink and Spring 3 Web MVC: how should I integrate both technologies?EclipseLink 和 Spring 3 Web MVC:我应该如何集成这两种技术?
【发布时间】:2012-05-30 20:55:57
【问题描述】:

我目前正在学习 EclipseLinkSpring 3.0 MVC:

  • 我使用 EclipseLink 编写了一个简单的独立应用程序:它使用 META-INF/persistence.xml 文件并从 mysql 数据库读取和写入数据。

  • 我还在 apache tomcat 下使用 Spring3 MVC 编写了一个简单的“HelloWorld”应用程序。它基于以下教程:http://www.mkyong.com/spring-mvc/spring-3-rest-hello-world-example

现在,我应该如何链接这两种技术?据我了解http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch14s06.html 我需要创建一个 LocalContainerEntityManagerFactoryBean:

 <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="someDataSource"/>
  <property name="loadTimeWeaver">
    <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
  </property>
 </bean>

但是什么是“someDataSource”?我应该使用像 http://commons.apache.org/dbcp/ 这样的另一个库吗?那么eclipselink应该怎么做呢?

一旦 JPA 配置完成,我应该如何从我的 spring Controller 访问它?

谢谢

【问题讨论】:

  • Here 有 Spring + JPA (Hibernate) 示例,您可能会觉得它很有帮助。

标签: java jpa spring-mvc configuration eclipselink


【解决方案1】:

someDataSource 只是另一个 bean,例如:

<bean id="someDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

这里我也使用了属性替换,这是由以下 bean 指定的:

<context:property-placeholder location="classpath:properties/runtime.properties" />

由于上下文在不同的 xml 命名空间下添加:

xmlns:context="http://www.springframework.org/schema/context"

到您的 XML 命名空间,并且

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd

到 bean xml 文档中的 schemaLocation 字符串。

classpath:properties/runtime.properties

此属性文件可以在标准 Maven 设置中的以下位置找到:

src/main/resources/properties/runtime.properties

在这个 runtime.properties 文件中,您需要为每个具有占位符的属性定义值(即 jdbc.driverClassName、jdbc.url 等)。这方面的一个例子是:

jdbc.driverClassName=com.mysql.jdbc.Driver

如果您的数据库使用 MySQL。

一般来说,您可能需要在控制器和存储库 (DAO) 之间建立一个服务层。原因是控制器应该只专注于处理请求和响应。任何业务逻辑都应该通过业务对象等在不同的层中完成。服务层充当 Web 层和存储库层之间的中介。这不仅分离了关注点,而且使事情变得更加可单元测试,这是任何 Spring 应用程序的重要组成部分(至少它应该是!)。

至于如何连接,尝试将这个 bean 也插入到你的 Context 中:

<context:component-scan base-package="xx.yy..." />

其中 xx 和 yy 是您的包名,这将告诉 Spring 扫描您的包中的“组件”或 @Controller 对象、@Service 和 @Repository(还有一些其他的,但这些是需要了解的主要内容关于)。使用提供给 Spring 的这些信息,您可以将 @Autowire bean(依赖注入)到其他 bean。

例如:

@Service
public class SomeServiceImpl implements SomeService
{
  private SomeRepository someRepository;

  @Autowired
  public void setSomeRepository(SomeRepository someRepository)
  {
    this.someRepository = someRepository;
  }
  ...
}

这会将存储库注入服务,允许您访问该存储库方法。

另一个设计技巧,总是在 Spring 中为接口编程。对于存储库来说尤其如此,因为在开发的早期,您可能想要实现一个 Hibernate 存储库,但是 DBA 可能会对此嗤之以鼻,并需要使用直接 SQL 的 JDBC 存储库。这样,您只需要使用 JDBC 方法而不是 Hibernate 来实现 Repository,注入 JDBC 存储库而不是 Hibernate,就完成了。

希望这能让你开始!

【讨论】:

    猜你喜欢
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 2012-12-17
    相关资源
    最近更新 更多