mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-05-28 14:19:38 +00:00
added bean profiles for jdbc/jpa
This commit is contained in:
parent
b459ca3377
commit
0c2665f1b4
7 changed files with 69 additions and 113 deletions
|
@ -8,7 +8,7 @@
|
|||
<enableImports><![CDATA[false]]></enableImports>
|
||||
<configs>
|
||||
<config>src/main/resources/spring/applicationContext-dataSource.xml</config>
|
||||
<config>src/main/resources/spring/applicationContext-jpa.xml</config>
|
||||
<config>src/main/resources/spring/applicationContext-dao.xml</config>
|
||||
</configs>
|
||||
<configSets>
|
||||
</configSets>
|
||||
|
|
|
@ -21,25 +21,6 @@
|
|||
<!-- (in this case, JDBC-related settings for the JPA EntityManager definition below) -->
|
||||
<context:property-placeholder location="classpath:spring/jdbc.properties"/>
|
||||
|
||||
<!-- JPA EntityManagerFactory -->
|
||||
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
|
||||
p:dataSource-ref="dataSource">
|
||||
<property name="jpaVendorAdapter">
|
||||
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
|
||||
p:database="${jpa.database}" p:showSql="${jpa.showSql}"/>
|
||||
</property>
|
||||
<property name="packagesToScan">
|
||||
<list>
|
||||
<value>org/springframework/samples/petclinic</value>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- Transaction manager for a single JPA EntityManagerFactory (alternative to JTA) -->
|
||||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
|
||||
p:entityManagerFactory-ref="entityManagerFactory"/>
|
||||
|
||||
|
||||
<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
|
||||
|
||||
<!--
|
||||
|
@ -65,20 +46,62 @@
|
|||
-->
|
||||
<aop:aspectj-autoproxy>
|
||||
<aop:include name="usageLogAspect"/>
|
||||
<aop:include name="callMonitor"/>
|
||||
</aop:aspectj-autoproxy>
|
||||
<bean id="usageLogAspect" class="org.springframework.samples.petclinic.aspects.UsageLogAspect" p:historySize="300"/>
|
||||
|
||||
<!--
|
||||
Post-processor to perform exception translation on @Repository classes (from native
|
||||
exceptions such as JPA PersistenceExceptions to Spring's DataAccessException hierarchy).
|
||||
-->
|
||||
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
|
||||
<!-- Call monitoring aspect that monitors call count and call invocation time -->
|
||||
<bean id="callMonitor" class="org.springframework.samples.petclinic.aspects.CallMonitoringAspect"/>
|
||||
|
||||
<!--
|
||||
Will automatically be transactional due to @Transactional.
|
||||
EntityManager will be auto-injected due to @PersistenceContext.
|
||||
PersistenceExceptions will be auto-translated due to @Repository.
|
||||
-->
|
||||
<bean id="clinic" class="org.springframework.samples.petclinic.jpa.JpaClinic"/>
|
||||
|
||||
<beans profile="jpa">
|
||||
<!-- JPA EntityManagerFactory -->
|
||||
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
|
||||
p:dataSource-ref="dataSource">
|
||||
<property name="jpaVendorAdapter">
|
||||
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
|
||||
p:database="${jpa.database}" p:showSql="${jpa.showSql}"/>
|
||||
</property>
|
||||
<property name="packagesToScan">
|
||||
<list>
|
||||
<value>org/springframework/samples/petclinic</value>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- Transaction manager for a single JPA EntityManagerFactory (alternative to JTA) -->
|
||||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
|
||||
p:entityManagerFactory-ref="entityManagerFactory"/>
|
||||
|
||||
|
||||
|
||||
<!--
|
||||
Post-processor to perform exception translation on @Repository classes (from native
|
||||
exceptions such as JPA PersistenceExceptions to Spring's DataAccessException hierarchy).
|
||||
-->
|
||||
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
|
||||
|
||||
<!--
|
||||
Will automatically be transactional due to @Transactional.
|
||||
EntityManager will be auto-injected due to @PersistenceContext.
|
||||
PersistenceExceptions will be auto-translated due to @Repository.
|
||||
-->
|
||||
<bean id="clinic" class="org.springframework.samples.petclinic.jpa.JpaClinic"/>
|
||||
</beans>
|
||||
|
||||
<beans profile="jdbc">
|
||||
<!-- Transaction manager for a single JDBC DataSource (alternative to JTA) -->
|
||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||
p:dataSource-ref="dataSource"/>
|
||||
|
||||
<!--
|
||||
Exporter that exposes the Clinic DAO and the CallMonitoringAspect via JMX,
|
||||
based on the @ManagedResource, @ManagedAttribute, and @ManagedOperation annotations.
|
||||
-->
|
||||
<context:mbean-export/>
|
||||
|
||||
<!-- PetClinic's central data access object using Spring's SimpleJdbcTemplate -->
|
||||
<bean id="clinic" class="org.springframework.samples.petclinic.jdbc.JdbcClinic"/>
|
||||
|
||||
</beans>
|
||||
</beans>
|
|
@ -1,64 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
Application context definition for PetClinic on JDBC.
|
||||
-->
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
|
||||
|
||||
<!-- ========================= RESOURCE DEFINITIONS ========================= -->
|
||||
|
||||
<!-- import the dataSource definition -->
|
||||
<import resource="applicationContext-dataSource.xml"/>
|
||||
|
||||
<!-- Transaction manager for a single JDBC DataSource (alternative to JTA) -->
|
||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||
p:dataSource-ref="dataSource"/>
|
||||
|
||||
<!-- Transaction manager that delegates to JTA (for a transactional JNDI DataSource) -->
|
||||
<!--
|
||||
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
|
||||
-->
|
||||
|
||||
|
||||
<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
|
||||
|
||||
<!--
|
||||
Activates various annotations to be detected in bean classes: Spring's
|
||||
@Required and @Autowired, as well as JSR 250's @PostConstruct,
|
||||
@PreDestroy and @Resource (if available) and JPA's @PersistenceContext
|
||||
and @PersistenceUnit (if available).
|
||||
-->
|
||||
<context:annotation-config/>
|
||||
|
||||
<!--
|
||||
Instruct Spring to retrieve and apply @AspectJ aspects which are defined
|
||||
as beans in this context (such as the CallMonitoringAspect below).
|
||||
-->
|
||||
<aop:aspectj-autoproxy/>
|
||||
|
||||
<!--
|
||||
Instruct Spring to perform automatic transaction management on annotated classes.
|
||||
The SimpleJdbcClinic implementation declares @Transactional annotations.
|
||||
"proxy-target-class" is set because of SimpleJdbcClinic's @ManagedOperation usage.
|
||||
-->
|
||||
<tx:annotation-driven/>
|
||||
|
||||
<!--
|
||||
Exporter that exposes the Clinic DAO and the CallMonitoringAspect via JMX,
|
||||
based on the @ManagedResource, @ManagedAttribute, and @ManagedOperation annotations.
|
||||
-->
|
||||
<context:mbean-export/>
|
||||
|
||||
<!-- PetClinic's central data access object using Spring's SimpleJdbcTemplate -->
|
||||
<bean id="clinic" class="org.springframework.samples.petclinic.jdbc.JdbcClinic"/>
|
||||
|
||||
<!-- Call monitoring aspect that monitors call count and call invocation time -->
|
||||
<bean id="callMonitor" class="org.springframework.samples.petclinic.aspects.CallMonitoringAspect"/>
|
||||
|
||||
</beans>
|
|
@ -15,7 +15,10 @@
|
|||
<param-value>petclinic.root</param-value>
|
||||
</context-param>
|
||||
|
||||
|
||||
<context-param>
|
||||
<param-name>spring.profiles.active</param-name>
|
||||
<param-value>jdbc</param-value>
|
||||
</context-param>
|
||||
|
||||
<!--
|
||||
Location of the Log4J config file, for initialization and refresh checks.
|
||||
|
@ -37,7 +40,7 @@
|
|||
-->
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>classpath:spring/applicationContext-jdbc.xml</param-value>
|
||||
<param-value>classpath:spring/applicationContext-dao.xml</param-value>
|
||||
<!--
|
||||
<param-value>/WEB-INF/spring/applicationContext-hibernate.xml</param-value>
|
||||
<param-value>/WEB-INF/spring/applicationContext-jpa.xml</param-value>
|
||||
|
|
|
@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.samples.petclinic.Clinic;
|
||||
import org.springframework.samples.petclinic.aspects.UsageLogAspect;
|
||||
import org.springframework.samples.petclinic.jpa.JpaClinicTests;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
|
@ -26,8 +27,9 @@ import static junit.framework.Assert.assertFalse;
|
|||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
@ContextConfiguration(locations={"classpath:spring/applicationContext-jpa.xml"})
|
||||
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ActiveProfiles("jpa")
|
||||
public class UsageLogAspectTests {
|
||||
|
||||
@Autowired
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.springframework.samples.petclinic.jdbc;
|
|||
import org.junit.runner.RunWith;
|
||||
import org.springframework.samples.petclinic.AbstractClinicTests;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
|
@ -11,14 +12,15 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||
* Integration tests for the {@link JdbcClinic} implementation.
|
||||
* </p>
|
||||
* <p>
|
||||
* "JdbcClinicTests-context.xml" determines the actual beans to test.
|
||||
* </p>
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @author Michael Isvy
|
||||
*/
|
||||
@ContextConfiguration(locations={"classpath:spring/applicationContext-jdbc.xml"})
|
||||
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@DirtiesContext
|
||||
@ActiveProfiles("jdbc")
|
||||
public class JdbcClinicTests extends AbstractClinicTests {
|
||||
|
||||
|
||||
|
|
|
@ -1,13 +1,8 @@
|
|||
|
||||
package org.springframework.samples.petclinic.jpa;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static junit.framework.Assert.fail;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
|
@ -16,15 +11,9 @@ import org.junit.runner.RunWith;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.samples.petclinic.AbstractClinicTests;
|
||||
import org.springframework.samples.petclinic.Clinic;
|
||||
import org.springframework.samples.petclinic.Owner;
|
||||
import org.springframework.samples.petclinic.Pet;
|
||||
import org.springframework.samples.petclinic.PetType;
|
||||
import org.springframework.samples.petclinic.Vet;
|
||||
import org.springframework.samples.petclinic.Visit;
|
||||
import org.springframework.samples.petclinic.util.EntityUtils;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -45,8 +34,9 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
* @author Michael Isvy
|
||||
*/
|
||||
|
||||
@ContextConfiguration(locations={"classpath:spring/applicationContext-jpa.xml"})
|
||||
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ActiveProfiles("jpa")
|
||||
public class JpaClinicTests extends AbstractClinicTests {
|
||||
|
||||
@PersistenceContext
|
||||
|
|
Loading…
Reference in a new issue