[英]EclipseLink and Spring 3 Web MVC: how should I integrate both technologies?
I'm currently learning EclipseLink and Spring 3.0 MVC: 我目前正在学习EclipseLink和Spring 3.0 MVC:
I wrote a simple standalone application using EclipseLink : it uses a META-INF/persistence.xml file and it reads and writes data from a mysql database. 我使用EclipseLink编写了一个简单的独立应用程序:它使用META-INF / persistence.xml文件,并从mysql数据库读取和写入数据。
I also wrote a simple 'HelloWorld' application using Spring3 MVC under apache tomcat . 我还在apache tomcat下使用Spring3 MVC编写了一个简单的“ HelloWorld”应用程序。 It is based on the following tutorial: http://www.mkyong.com/spring-mvc/spring-3-rest-hello-world-example 它基于以下教程: http : //www.mkyong.com/spring-mvc/spring-3-rest-hello-world-example
now, how should I link both technologies ? 现在,我应该如何链接这两种技术? As far as I understand from http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch14s06.html I need to create a LocalContainerEntityManagerFactoryBean: 据我从http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch14s06.html了解到,我需要创建一个LocalContainerEntityManagerManagerFactoryBean:
<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>
but what is "someDataSource" ? 但是什么是“ someDataSource”? should I use another library like http://commons.apache.org/dbcp/ ? 我应该使用其他库,例如http://commons.apache.org/dbcp/吗? then what should I do with eclipselink ? 那我该如何使用eclipselink呢?
And once the JPA will be configured, how should I access it from my spring Controller ? 一旦配置了JPA,我应该如何从spring Controller中访问它?
Thank you 谢谢
someDataSource would just be another bean, for example: 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>
Here I'm also using a property replacement for the properties, this is designated by the following bean: 在这里,我还在使用属性替换属性,由以下bean指定:
<context:property-placeholder location="classpath:properties/runtime.properties" />
Since context is under a different xml namespace add: 由于上下文位于不同的xml名称空间下,因此添加:
xmlns:context="http://www.springframework.org/schema/context"
to your XML namespaces, and 到您的XML名称空间,以及
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
to your schemaLocation string within the bean xml document. 到bean xml文档中的schemaLocation字符串。
classpath:properties/runtime.properties
This property file would be found at the following location in a standard Maven setup: 在标准Maven设置中的以下位置可以找到此属性文件:
src/main/resources/properties/runtime.properties
Within this runtime.properties file you'd need to define values for each of the properties that have placeholders (ie jdbc.driverClassName, jdbc.url, etc.). 在这个runtime.properties文件中,您需要为每个具有占位符的属性(即jdbc.driverClassName,jdbc.url等)定义值。 An example of this would be: 例如:
jdbc.driverClassName=com.mysql.jdbc.Driver
If you're using MySQL for your database. 如果您将MySQL用于数据库。
Generally speaking, you might want a service layer between your controller and repository (DAO). 一般来说,您可能希望在控制器和存储库(DAO)之间建立服务层。 The reason being is controller should concentrate only on handling requests, and responding. 原因是控制器应仅专注于处理请求和响应。 Any business logic should be done in a different layer, through business objects, etc. Service layer acts as an intermediary between web layer, and repository layer. 任何业务逻辑都应通过业务对象等在不同的层中完成。服务层充当Web层与存储库层之间的中介。 This not only separates concerns, but also makes things infinitely more unit testable, a huge part of any Spring application (at least it should be!). 这不仅分离了关注点,而且使事物可以无限地进行单元测试,这在任何Spring应用程序中都是很大的一部分(至少应该如此!)。
As to how to wire things up, try inserting this bean into your Context as well: 至于如何连接,请尝试将此bean也插入到Context中:
<context:component-scan base-package="xx.yy..." />
Where xx and yy are your package names, this will tell Spring to scan your packages for "components" or @Controller objects, @Service, and @Repository (there are a few other ones but those are the main things to know about). 其中xx和yy是您的软件包名称,这将告诉Spring扫描您的软件包中的“组件”或@Controller对象,@ Service和@Repository(还有其他一些,但这些是要了解的主要内容)。 With this information provided to Spring, you can @Autowire beans (dependency inject) into other beans. 有了提供给Spring的信息,您可以@Autowire bean(依赖注入)到其他bean中。
For example: 例如:
@Service
public class SomeServiceImpl implements SomeService
{
private SomeRepository someRepository;
@Autowired
public void setSomeRepository(SomeRepository someRepository)
{
this.someRepository = someRepository;
}
...
}
This will inject the repository into the service allowing you to access that repositories methods. 这会将存储库注入服务中,从而允许您访问该存储库方法。
Another design tip, always program for interfaces in Spring. 另一个设计技巧,始终在Spring中为接口编程。 This is especially true when it comes to repositories due to the fact that early in development, you might want to implement a Hibernate repository, but down the line DBAs might scoff at that, and require a JDBC repository using straight SQL. 对于存储库,尤其如此,因为在开发的早期,您可能想要实现一个Hibernate存储库,但是DBA可能会sc之以鼻,并要求使用直接SQL的JDBC存储库。 This way, you just need to implement the Repository using a JDBC approach rather than Hibernate, inject the JDBC repository instead of Hibernate, and you're done. 这样,您只需要使用JDBC方法而不是Hibernate来实现Repository,注入JDBC库而不是Hibernate,就可以了。
Hope this gets you started! 希望这可以帮助您入门!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.