From 0c2665f1b4585de0eaae2e6276d07a1fafe3a208 Mon Sep 17 00:00:00 2001
From: Mic <misvy@vmware.com>
Date: Mon, 14 Jan 2013 23:36:06 +0800
Subject: [PATCH] added bean profiles for jdbc/jpa

---
 .springBeans                                  |  2 +-
 ...ext-jpa.xml => applicationContext-dao.xml} | 83 ++++++++++++-------
 .../spring/applicationContext-jdbc.xml        | 64 --------------
 src/main/webapp/WEB-INF/web.xml               |  7 +-
 .../aspects/UsageLogAspectTests.java          |  4 +-
 .../petclinic/jdbc/JdbcClinicTests.java       |  6 +-
 .../samples/petclinic/jpa/JpaClinicTests.java | 16 +---
 7 files changed, 69 insertions(+), 113 deletions(-)
 rename src/main/resources/spring/{applicationContext-jpa.xml => applicationContext-dao.xml} (50%)
 delete mode 100644 src/main/resources/spring/applicationContext-jdbc.xml

diff --git a/.springBeans b/.springBeans
index f88ad9b..1a6ad9c 100644
--- a/.springBeans
+++ b/.springBeans
@@ -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>
diff --git a/src/main/resources/spring/applicationContext-jpa.xml b/src/main/resources/spring/applicationContext-dao.xml
similarity index 50%
rename from src/main/resources/spring/applicationContext-jpa.xml
rename to src/main/resources/spring/applicationContext-dao.xml
index 6a9c865..bcf2df6 100644
--- a/src/main/resources/spring/applicationContext-jpa.xml
+++ b/src/main/resources/spring/applicationContext-dao.xml
@@ -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>
\ No newline at end of file
diff --git a/src/main/resources/spring/applicationContext-jdbc.xml b/src/main/resources/spring/applicationContext-jdbc.xml
deleted file mode 100644
index 7456dbc..0000000
--- a/src/main/resources/spring/applicationContext-jdbc.xml
+++ /dev/null
@@ -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>
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml
index b8dad07..75ba1b9 100644
--- a/src/main/webapp/WEB-INF/web.xml
+++ b/src/main/webapp/WEB-INF/web.xml
@@ -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>
diff --git a/src/test/java/org/springframework/samples/petclinic/aspects/UsageLogAspectTests.java b/src/test/java/org/springframework/samples/petclinic/aspects/UsageLogAspectTests.java
index 1ec7689..901e766 100644
--- a/src/test/java/org/springframework/samples/petclinic/aspects/UsageLogAspectTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/aspects/UsageLogAspectTests.java
@@ -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
diff --git a/src/test/java/org/springframework/samples/petclinic/jdbc/JdbcClinicTests.java b/src/test/java/org/springframework/samples/petclinic/jdbc/JdbcClinicTests.java
index 138053b..1d26e24 100644
--- a/src/test/java/org/springframework/samples/petclinic/jdbc/JdbcClinicTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/jdbc/JdbcClinicTests.java
@@ -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 {
 	
 	
diff --git a/src/test/java/org/springframework/samples/petclinic/jpa/JpaClinicTests.java b/src/test/java/org/springframework/samples/petclinic/jpa/JpaClinicTests.java
index 2d0d6fa..fc50f6e 100644
--- a/src/test/java/org/springframework/samples/petclinic/jpa/JpaClinicTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/jpa/JpaClinicTests.java
@@ -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
-- 
GitLab