Skip to content
Snippets Groups Projects
Commit 0c2665f1 authored by Mic's avatar Mic
Browse files

added bean profiles for jdbc/jpa

parent b459ca33
No related branches found
No related tags found
No related merge requests found
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
<enableImports><![CDATA[false]]></enableImports> <enableImports><![CDATA[false]]></enableImports>
<configs> <configs>
<config>src/main/resources/spring/applicationContext-dataSource.xml</config> <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> </configs>
<configSets> <configSets>
</configSets> </configSets>
......
...@@ -21,25 +21,6 @@ ...@@ -21,25 +21,6 @@
<!-- (in this case, JDBC-related settings for the JPA EntityManager definition below) --> <!-- (in this case, JDBC-related settings for the JPA EntityManager definition below) -->
<context:property-placeholder location="classpath:spring/jdbc.properties"/> <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 ========================= --> <!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
<!-- <!--
...@@ -65,20 +46,62 @@ ...@@ -65,20 +46,62 @@
--> -->
<aop:aspectj-autoproxy> <aop:aspectj-autoproxy>
<aop:include name="usageLogAspect"/> <aop:include name="usageLogAspect"/>
<aop:include name="callMonitor"/>
</aop:aspectj-autoproxy> </aop:aspectj-autoproxy>
<bean id="usageLogAspect" class="org.springframework.samples.petclinic.aspects.UsageLogAspect" p:historySize="300"/> <bean id="usageLogAspect" class="org.springframework.samples.petclinic.aspects.UsageLogAspect" p:historySize="300"/>
<!-- <!-- Call monitoring aspect that monitors call count and call invocation time -->
Post-processor to perform exception translation on @Repository classes (from native <bean id="callMonitor" class="org.springframework.samples.petclinic.aspects.CallMonitoringAspect"/>
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 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> </beans>
\ No newline at end of file
<?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
...@@ -15,7 +15,10 @@ ...@@ -15,7 +15,10 @@
<param-value>petclinic.root</param-value> <param-value>petclinic.root</param-value>
</context-param> </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. Location of the Log4J config file, for initialization and refresh checks.
...@@ -37,7 +40,7 @@ ...@@ -37,7 +40,7 @@
--> -->
<context-param> <context-param>
<param-name>contextConfigLocation</param-name> <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-hibernate.xml</param-value>
<param-value>/WEB-INF/spring/applicationContext-jpa.xml</param-value> <param-value>/WEB-INF/spring/applicationContext-jpa.xml</param-value>
......
...@@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.Clinic; import org.springframework.samples.petclinic.Clinic;
import org.springframework.samples.petclinic.aspects.UsageLogAspect; import org.springframework.samples.petclinic.aspects.UsageLogAspect;
import org.springframework.samples.petclinic.jpa.JpaClinicTests; import org.springframework.samples.petclinic.jpa.JpaClinicTests;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.assertTrue;
...@@ -26,8 +27,9 @@ import static junit.framework.Assert.assertFalse; ...@@ -26,8 +27,9 @@ import static junit.framework.Assert.assertFalse;
* @author Rod Johnson * @author Rod Johnson
* @author Juergen Hoeller * @author Juergen Hoeller
*/ */
@ContextConfiguration(locations={"classpath:spring/applicationContext-jpa.xml"}) @ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("jpa")
public class UsageLogAspectTests { public class UsageLogAspectTests {
@Autowired @Autowired
......
...@@ -3,6 +3,7 @@ package org.springframework.samples.petclinic.jdbc; ...@@ -3,6 +3,7 @@ package org.springframework.samples.petclinic.jdbc;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractClinicTests; import org.springframework.samples.petclinic.AbstractClinicTests;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
...@@ -11,14 +12,15 @@ 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. * Integration tests for the {@link JdbcClinic} implementation.
* </p> * </p>
* <p> * <p>
* "JdbcClinicTests-context.xml" determines the actual beans to test.
* </p> * </p>
* *
* @author Thomas Risberg * @author Thomas Risberg
* @author Michael Isvy
*/ */
@ContextConfiguration(locations={"classpath:spring/applicationContext-jdbc.xml"}) @ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext @DirtiesContext
@ActiveProfiles("jdbc")
public class JdbcClinicTests extends AbstractClinicTests { public class JdbcClinicTests extends AbstractClinicTests {
......
package org.springframework.samples.petclinic.jpa; 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 static junit.framework.Assert.fail;
import java.util.Collection;
import java.util.Date;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
...@@ -16,15 +11,9 @@ import org.junit.runner.RunWith; ...@@ -16,15 +11,9 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.AbstractClinicTests; import org.springframework.samples.petclinic.AbstractClinicTests;
import org.springframework.samples.petclinic.Clinic; import org.springframework.samples.petclinic.Clinic;
import org.springframework.samples.petclinic.Owner; import org.springframework.test.context.ActiveProfiles;
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.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/** /**
* <p> * <p>
...@@ -45,8 +34,9 @@ import org.springframework.transaction.annotation.Transactional; ...@@ -45,8 +34,9 @@ import org.springframework.transaction.annotation.Transactional;
* @author Michael Isvy * @author Michael Isvy
*/ */
@ContextConfiguration(locations={"classpath:spring/applicationContext-jpa.xml"}) @ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("jpa")
public class JpaClinicTests extends AbstractClinicTests { public class JpaClinicTests extends AbstractClinicTests {
@PersistenceContext @PersistenceContext
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment