任何比 PetClinic 更大的开源 Spring 示例项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2604655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any open source Spring sample project that's bigger than PetClinic?
提问by Tong Wang
I've finished reading the spring doc and the PetClinic sample project. Just like to see some bigger real world project that's done with Spring. Thanks.
我已经阅读了 spring 文档和 PetClinic 示例项目。就像看到一些使用 Spring 完成的更大的现实世界项目一样。谢谢。
回答by Harsha Hulageri
回答by Arthur Ronald
I work for a big health insurance company where we heavily use Spring in backend. I will show you how a modularizedapplication is built.
我在一家大型健康保险公司工作,我们在后台大量使用 Spring。我将向您展示如何构建模块化应用程序。
SkeletonWEB-INFwithout classes directory
没有类目录的骨架WEB-INF
ar
WEB-INF
web.xml
/**
* Spring related settings file
*/
ar-servlet.xml
web
moduleA
account
form.jsp
moduleB
order
form.jsp
Skeletonclassesdirectory
骨架类目录
classes
/**
* Spring related settings file
*/
ar-persistence.xml
ar-security.xml
ar-service.xml
messages.properties
br
com
ar
web
moduleA
AccountController.class
moduleB
OrderController.class
br
com
ar
moduleA
model
domain
Account.class
repository
moduleA.hbm.xml
service
br
com
ar
moduleB
model
domain
Order.class
repository
moduleB.hbm.xml
service
...
Notice how each package under br.com.ar.webmatchsWEB-INF/viewdirectory. It is The key needed To run convention-over-configuration in Spring MVC. How to ??? rely on ControllerClassNameHandlerMapping
注意br.com.ar.web下的每个包如何匹配WEB-INF/view目录。在 Spring MVC 中运行约定优于配置是需要的关键。如何 ???依赖ControllerClassNameHandlerMapping
WEB-INF/ar-servlet.xmlNotice basePackage property which means look for any @Controllerclass under br.com.ar.viewpackage. This property allows you build modularized @Controller's
WEB-INF/ar-servlet.xml注意 basePackage 属性,这意味着查找br.com.ar.view包下的任何@Controller类。此属性允许您构建模块化的@Controller
<!--Scans the classpath for annotated components at br.com.ar.web package-->
<context:component-scan base-package="br.com.ar.web"/>
<!--registers the HandlerMapping and HandlerAdapter required to dispatch requests to your @Controllers-->
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="basePackage" value="br.com.ar.web"/>
<property name="caseSensitive" value="true"/>
<property name="defaultHandler">
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
Now let's see, for instance, AccountController
现在让我们看看,例如 AccountController
package br.com.ar.web;
@Controller
public class AccountController {
@Qualifier("categoryRepository")
private @Autowired Repository<Category, Category, Integer> categoryRepository;
@Qualifier("accountRepository")
private @Autowired Repository<Account, Accout, Integer> accountRepository;
/**
* mapped To /account/form
*/
@RequestMapping(method=RequesMethod.GET)
public void form(Model model) {
model.add(categoryRepository().getCategoryList());
}
/**
* mapped To account/form
*/
@RequestMapping(method=RequesMethod.POST)
public void form(Account account, Errors errors) {
accountRepository.add(account);
}
}
How does it work ???
它是如何工作的 ???
Suppose you make a request for http://127.0.0.1:8080/ar/moduleA/account/form.html
假定你的请求http://127.0.0.1:8080/ar/ moduleA /帐号/形式的.html
Spring will remove the path betweencontext path and file extension - highlighted above. Let's read the extracted path from the right To the left
Spring 将删除上下文路径和文件扩展名之间的路径 - 上面突出显示。让我们从右向左阅读提取的路径
- formmethod name
- accountunqualified class name withoutController suffix
- moduleApackage which will be addedto basePackageproperty
- 表单方法名
- 不带Controller 后缀的帐户非限定类名
- moduleA包,其中将被添加到basePackage属性
which is translated to
这被翻译成
br.com.ar.web.moduleA.AccountController.form
Ok. But how does Spring know which view to show ??? See here
好的。但是 Spring 如何知道要显示哪个视图???看这里
And about persistencerelated issues ???
关于持久性相关的问题???
First of all, see herehow we implement repository. Notice each related module query is stored at its related repository package. See skeleton above. Here is shown ar-persistence.xml Notice mappingLocationsand packagesToScanproperty
首先,看这里我们如何实现存储库。请注意,每个相关的模块查询都存储在其相关的存储库包中。见上面的骨架。这里显示了 ar-persistence.xml 注意mappingLocations和packagesToScan属性
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/dataSource" resource-ref="true">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingLocations">
<util:list>
<value>classpath:br/com/ar/model/repository/hql.moduleA.hbm.xml</value>
<value>classpath:br/com/ar/model/repository/hql.moduleB.hbm.xml</value>
</util:list>
</property>
<property name="packagesToScan">
<util:list>
<value>br.com.ar.moduleA.model.domain</value>
<value>br.com.ar.moduleB.model.domain</value>
</util:list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.validator.autoregister_listeners">false</prop>
</props>
</property>
</bean>
</beans>
Notice i am using Hibernate. JPA should be properly configured.
请注意,我正在使用 Hibernate。JPA 应该正确配置。
Transaction management and component scanningar-service.xml Notice Two dotsafter br.com.arin aop:pointcut's expression attribute which means
交易管理和组件扫描AR-service.xml的通知两点之后br.com.ar在AOP:切入点的表达属性该装置
Any package and sub-package under br.com.ar package
br.com.ar 包下的任意包和子包
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="br.com.ar.model">
<!--Transaction manager - It takes care of calling begin and commit in the underlying resource - here a Hibernate Transaction -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="repositoryTransactionManagementAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="remove" propagation="REQUIRED"/>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<tx:advice id="serviceTransactionManagementAdvice" transaction-manager="transactionManager">
<!--Any method - * - in service layer should have an active Transaction - REQUIRED - -->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="servicePointcut" expression="execution(* br.com.ar..service.*Service.*(..))"/>
<aop:pointcut id="repositoryPointcut" expression="execution(* br.com.ar..repository.*Repository.*(..))"/>
<aop:advisor advice-ref="serviceTransactionManagementAdvice" pointcut-ref="servicePointcut"/>
<aop:advisor advice-ref="repositoryTransactionManagementAdvice" pointcut-ref="repositoryPointcut"/>
</aop:config>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
</beans>
Testing
测试
To test annotated @Controller method, see herehow to
要测试带注释的 @Controller 方法,请参阅此处如何
Other than web layer. Notice how i configure a JNDI dataSource in @Before method
除了网络层。注意我如何在 @Before 方法中配置 JNDI 数据源
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:ar-service.xml", "classpath:ar-persistence.xml"})
public class AccountRepositoryIntegrationTest {
@Autowired
@Qualifier("accountRepository")
private Repository<Account, Account, Integer> repository;
private Integer id;
@Before
public void setUp() {
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
DataSource ds = new SimpleDriverDataSource(new oracle.jdbc.driver.OracleDriver(), "jdbc:oracle:thin:@127.0.0.1:1521:ar", "#$%#", "#$%#");
builder.bind("/jdbc/dataSource", ds);
builder.activate();
/**
* Save an Account and set up id field
*/
}
@Test
public void assertSavedAccount() {
Account account = repository.findById(id);
assertNotNull(account);
}
}
If you need a suite of tests, do as follows
如果您需要一套测试,请执行以下操作
@RunWith(Suite.class)
@Suite.SuiteClasses(value={AccountRepositoryIntegrationTest.class})
public void ModuleASuiteTest {}
web.xml is shown as follows
web.xml 如下所示
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:ar-persistence.xml
classpath:ar-service.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>ar</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ar</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<resource-ref>
<description>datasource</description>
<res-ref-name>jdbc/dataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
I hope it can be useful. Update schema To Spring 3.0. See Spring reference documentation. mvc schema, As far As i know, is supported just in Spring 3.0. Keep this in mind
我希望它有用。将架构更新到 Spring 3.0。请参阅 Spring 参考文档。mvc 模式,据我所知,仅在 Spring 3.0 中受支持。记住这一点
回答by Pascal Thivent
Some candidates:
部分候选人:
AppFuse- In AppFuse, the Spring Framework is used throughout for its Hibernate/iBATIS support, declarative transactions, dependency binding and layer decoupling.
Equinox(a.k.a. AppFuse Light) - a simple CRUD app created as part of Spring Live.
Spring by Example- Various Spring examples plus some downloadable libraries.
Tudu Lists- Tudu Lists is a J2EE application for managing todo lists. It's based on JDK 5.0, Spring, Hibernate, and an AJAX interface (using the DWR framework).
AppFuse- 在 AppFuse 中,Spring 框架始终用于其 Hibernate/iBATIS 支持、声明性事务、依赖绑定和层解耦。
Equinox(又名 AppFuse Light) - 作为 Spring Live 的一部分创建的简单 CRUD 应用程序。
Spring by Example- 各种 Spring 示例以及一些可下载的库。
Tudu Lists- Tudu Lists 是一个用于管理待办事项列表的 J2EE 应用程序。它基于 JDK 5.0、Spring、Hibernate 和 AJAX 接口(使用 DWR 框架)。
回答by bmargulies
Look at Apache CXF. It uses Spring.
看看Apache CXF。它使用弹簧。

