mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:15:50 +00:00
Replace XML to Java configuration for business layer
This commit is contained in:
parent
00339371a1
commit
0a646c4267
11 changed files with 164 additions and 101 deletions
|
@ -0,0 +1,22 @@
|
|||
package org.springframework.samples.petclinic.config;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@Configuration
|
||||
// import the dataSource definition
|
||||
@ImportResource("classpath:spring/datasource-config.xml")
|
||||
@ComponentScan("org.springframework.samples.petclinic.service")
|
||||
// Configurer that replaces ${...} placeholders with values from a properties file
|
||||
// (in this case, JDBC-related settings for the JPA EntityManager definition below)
|
||||
@PropertySource("classpath:spring/data-access.properties")
|
||||
@EnableTransactionManagement
|
||||
@Import({JdbcConfig.class, SharedJpaConfig.class, JpaConfig.class, SpringDataJpaConfig.class})
|
||||
public class BusinessConfig {
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package org.springframework.samples.petclinic.config;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
|
||||
@Configuration
|
||||
@Profile("jdbc")
|
||||
@ComponentScan("org.springframework.samples.petclinic.repository.jdbc")
|
||||
public class JdbcConfig {
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
|
||||
@Bean
|
||||
public DataSourceTransactionManager dataSourceTransactionManager() {
|
||||
return new DataSourceTransactionManager(dataSource);
|
||||
}
|
||||
|
||||
@Bean(name="transactionManager")
|
||||
public JdbcTemplate jdbcTemplate() {
|
||||
return new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
public NamedParameterJdbcTemplate namedParameterJdbcTemplate() {
|
||||
return new NamedParameterJdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package org.springframework.samples.petclinic.config;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
@Configuration
|
||||
@Profile("jpa")
|
||||
@ComponentScan("org.springframework.samples.petclinic.repository.jpa")
|
||||
public class JpaConfig {
|
||||
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
package org.springframework.samples.petclinic.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
|
||||
@Configuration
|
||||
@ImportResource({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml"})
|
||||
@Import(BusinessConfig.class)
|
||||
@ImportResource("classpath:spring/tools-config.xml")
|
||||
public class RootApplicationContextConfig {
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
package org.springframework.samples.petclinic.config;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.JpaVendorAdapter;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.Database;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
|
||||
@Configuration
|
||||
@Profile({"jpa", "spring-data-jpa"})
|
||||
public class SharedJpaConfig {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Bean
|
||||
public EntityManagerFactory entityManagerFactory() {
|
||||
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource);
|
||||
// gDickens: BOTH Persistence Unit and Packages to Scan are NOT compatible, persistenceUnit will win
|
||||
em.setPersistenceUnitName("petclinic");
|
||||
em.setPackagesToScan("org.springframework.samples.petclinic");
|
||||
em.setJpaVendorAdapter(jpaVendorAdaper());
|
||||
em.afterPropertiesSet();
|
||||
return em.getObject();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JpaVendorAdapter jpaVendorAdaper() {
|
||||
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
// the 'database' parameter refers to the database dialect being used.
|
||||
// By default, Hibernate will use a 'HSQL' dialect because 'jpa.database' has been set to 'HSQL'
|
||||
// inside file spring/data-access.properties
|
||||
vendorAdapter.setDatabase(env.getProperty("jpa.database", Database.class));
|
||||
vendorAdapter.setShowSql(env.getProperty("jpa.showSql", Boolean.class));
|
||||
return vendorAdapter;
|
||||
}
|
||||
|
||||
@Bean(name="transactionManager")
|
||||
public JpaTransactionManager jpaTransactionManager() {
|
||||
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
|
||||
jpaTransactionManager.setEntityManagerFactory(entityManagerFactory());
|
||||
return jpaTransactionManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
|
||||
return new PersistenceExceptionTranslationPostProcessor();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package org.springframework.samples.petclinic.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
@Configuration
|
||||
@Profile("spring-data-jpa")
|
||||
@EnableJpaRepositories("org.springframework.samples.petclinic.repository.springdatajpa")
|
||||
public class SpringDataJpaConfig {
|
||||
|
||||
}
|
|
@ -1,96 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
Repository and Service layers
|
||||
-->
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<!-- ========================= RESOURCE DEFINITIONS ========================= -->
|
||||
|
||||
<!-- import the dataSource definition -->
|
||||
<import resource="datasource-config.xml"/>
|
||||
|
||||
<context:component-scan
|
||||
base-package="org.springframework.samples.petclinic.service"/>
|
||||
|
||||
<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
|
||||
<!-- (in this case, JDBC-related settings for the JPA EntityManager definition below) -->
|
||||
<context:property-placeholder location="classpath:spring/data-access.properties" system-properties-mode="OVERRIDE"/>
|
||||
|
||||
<!-- enables scanning for @Transactional annotations -->
|
||||
<tx:annotation-driven />
|
||||
|
||||
|
||||
<!-- ================== 3 Profiles to choose from ===================
|
||||
- jdbc (uses Spring" JdbcTemplate)
|
||||
- jpa
|
||||
- spring-data-jpa
|
||||
=============================================================================-->
|
||||
|
||||
<beans profile="jpa,spring-data-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}"/>
|
||||
<!-- the 'database' parameter refers to the database dialect being used.
|
||||
By default, Hibernate will use a 'HSQL' dialect because 'jpa.database' has been set to 'HSQL'
|
||||
inside file spring/data-access.properties
|
||||
|
||||
-->
|
||||
</property>
|
||||
<!-- gDickens: BOTH Persistence Unit and Packages to Scan are NOT compatible, persistenceUnit will win -->
|
||||
<property name="persistenceUnitName" value="petclinic"/>
|
||||
<property name="packagesToScan" value="org.springframework.samples.petclinic"/>
|
||||
</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"/>
|
||||
|
||||
</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"/>
|
||||
|
||||
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
|
||||
<constructor-arg ref="dataSource"/>
|
||||
</bean>
|
||||
|
||||
<bean id="namedParameterJdbcTemplate"
|
||||
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
|
||||
<constructor-arg ref="dataSource"/>
|
||||
</bean>
|
||||
|
||||
<context:component-scan base-package="org.springframework.samples.petclinic.repository.jdbc"/>
|
||||
</beans>
|
||||
|
||||
<beans profile="jpa">
|
||||
<!--
|
||||
Loads JPA beans
|
||||
Will automatically be transactional due to @Transactional.
|
||||
EntityManager will be auto-injected due to @PersistenceContext.
|
||||
PersistenceExceptions will be auto-translated due to @Repository.
|
||||
-->
|
||||
<context:component-scan base-package="org.springframework.samples.petclinic.repository.jpa"/>
|
||||
</beans>
|
||||
|
||||
<beans profile="spring-data-jpa">
|
||||
<jpa:repositories base-package="org.springframework.samples.petclinic.repository.springdatajpa"/>
|
||||
</beans>
|
||||
</beans>
|
|
@ -16,6 +16,7 @@
|
|||
package org.springframework.samples.petclinic.service;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.samples.petclinic.config.BusinessConfig;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
@ -27,7 +28,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||
* @author Thomas Risberg
|
||||
* @author Michael Isvy
|
||||
*/
|
||||
@ContextConfiguration(locations = {"classpath:spring/business-config.xml"})
|
||||
@ContextConfiguration(classes = BusinessConfig.class)
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ActiveProfiles("jdbc")
|
||||
public class ClinicServiceJdbcTests extends AbstractClinicServiceTests {
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
package org.springframework.samples.petclinic.service;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.samples.petclinic.config.BusinessConfig;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
@ -15,7 +16,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||
* @author Michael Isvy
|
||||
*/
|
||||
|
||||
@ContextConfiguration(locations = {"classpath:spring/business-config.xml"})
|
||||
@ContextConfiguration(classes = BusinessConfig.class)
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ActiveProfiles("jpa")
|
||||
public class ClinicServiceJpaTests extends AbstractClinicServiceTests {
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
package org.springframework.samples.petclinic.service;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.samples.petclinic.config.BusinessConfig;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
@ -12,7 +13,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||
* @author Michael Isvy
|
||||
*/
|
||||
|
||||
@ContextConfiguration(locations = {"classpath:spring/business-config.xml"})
|
||||
@ContextConfiguration(classes = BusinessConfig.class)
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ActiveProfiles("spring-data-jpa")
|
||||
public class ClinicServiceSpringDataJpaTests extends AbstractClinicServiceTests {
|
||||
|
|
|
@ -10,8 +10,10 @@ import org.junit.Test;
|
|||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.samples.petclinic.config.BusinessConfig;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.ContextHierarchy;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
@ -25,8 +27,10 @@ import org.springframework.web.context.WebApplicationContext;
|
|||
* @see UserResource
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"})
|
||||
@WebAppConfiguration
|
||||
@ContextHierarchy({
|
||||
@ContextConfiguration(classes = BusinessConfig.class),
|
||||
@ContextConfiguration(locations = {"classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"})})
|
||||
@ActiveProfiles("spring-data-jpa")
|
||||
public class VetControllerTests {
|
||||
|
||||
|
|
Loading…
Reference in a new issue