From 521155c3a64f50609767616213bf7d37906da954 Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Tue, 1 Jul 2014 08:00:09 +0200 Subject: [PATCH 1/8] Setup Java configuration into the web.xml --- .../petclinic/config/MvcCoreConfig.java | 10 +++++++ .../config/RootApplicationContextConfig.java | 10 +++++++ src/main/webapp/WEB-INF/web.xml | 27 ++++++++++++------- 3 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/config/MvcCoreConfig.java create mode 100644 src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java diff --git a/src/main/java/org/springframework/samples/petclinic/config/MvcCoreConfig.java b/src/main/java/org/springframework/samples/petclinic/config/MvcCoreConfig.java new file mode 100644 index 000000000..69ddf94e7 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/MvcCoreConfig.java @@ -0,0 +1,10 @@ +package org.springframework.samples.petclinic.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; + +@Configuration +@ImportResource("classpath:spring/mvc-core-config.xml") +public class MvcCoreConfig { + +} diff --git a/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java b/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java new file mode 100644 index 000000000..9a55d3605 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java @@ -0,0 +1,10 @@ +package org.springframework.samples.petclinic.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; + +@Configuration +@ImportResource({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml"}) +public class RootApplicationContextConfig { + +} diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 360e3698a..2fcd59eba 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -28,13 +28,18 @@ http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" --> - - contextConfigLocation - classpath:spring/business-config.xml, classpath:spring/tools-config.xml - + + contextClass + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + contextConfigLocation + org.springframework.samples.petclinic.config.RootApplicationContextConfig + + org.springframework.web.context.ContextLoaderListener @@ -46,10 +51,14 @@ http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" petclinic org.springframework.web.servlet.DispatcherServlet - - contextConfigLocation - classpath:spring/mvc-core-config.xml - + + contextClass + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + contextConfigLocation + org.springframework.samples.petclinic.config.MvcCoreConfig + 1 From 03f9ccc72d66fefe73f6f28271eb294da2db490a Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Tue, 1 Jul 2014 19:00:31 +0200 Subject: [PATCH 2/8] Replace XML to Java configuration for business layer --- .../petclinic/config/BusinessConfig.java | 22 +++++ .../samples/petclinic/config/JdbcConfig.java | 40 ++++++++ .../samples/petclinic/config/JpaConfig.java | 12 +++ .../config/RootApplicationContextConfig.java | 4 +- .../petclinic/config/SharedJpaConfig.java | 64 +++++++++++++ .../petclinic/config/SpringDataJpaConfig.java | 12 +++ src/main/resources/spring/business-config.xml | 96 ------------------- .../service/ClinicServiceJdbcTests.java | 3 +- .../service/ClinicServiceJpaTests.java | 3 +- .../ClinicServiceSpringDataJpaTests.java | 3 +- .../petclinic/web/VisitsViewTests-config.xml | 13 --- .../petclinic/web/VisitsViewTests.java | 6 +- 12 files changed, 164 insertions(+), 114 deletions(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/config/BusinessConfig.java create mode 100644 src/main/java/org/springframework/samples/petclinic/config/JdbcConfig.java create mode 100644 src/main/java/org/springframework/samples/petclinic/config/JpaConfig.java create mode 100644 src/main/java/org/springframework/samples/petclinic/config/SharedJpaConfig.java create mode 100644 src/main/java/org/springframework/samples/petclinic/config/SpringDataJpaConfig.java delete mode 100644 src/main/resources/spring/business-config.xml delete mode 100644 src/test/java/org/springframework/samples/petclinic/web/VisitsViewTests-config.xml diff --git a/src/main/java/org/springframework/samples/petclinic/config/BusinessConfig.java b/src/main/java/org/springframework/samples/petclinic/config/BusinessConfig.java new file mode 100644 index 000000000..fd258f7e6 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/BusinessConfig.java @@ -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 { + + +} diff --git a/src/main/java/org/springframework/samples/petclinic/config/JdbcConfig.java b/src/main/java/org/springframework/samples/petclinic/config/JdbcConfig.java new file mode 100644 index 000000000..34f5786f5 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/JdbcConfig.java @@ -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); + } + + +} diff --git a/src/main/java/org/springframework/samples/petclinic/config/JpaConfig.java b/src/main/java/org/springframework/samples/petclinic/config/JpaConfig.java new file mode 100644 index 000000000..a8c18e74a --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/JpaConfig.java @@ -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 { + +} diff --git a/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java b/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java index 9a55d3605..254b3209c 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java @@ -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 { } diff --git a/src/main/java/org/springframework/samples/petclinic/config/SharedJpaConfig.java b/src/main/java/org/springframework/samples/petclinic/config/SharedJpaConfig.java new file mode 100644 index 000000000..0d454c977 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/SharedJpaConfig.java @@ -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(); + } + + +} diff --git a/src/main/java/org/springframework/samples/petclinic/config/SpringDataJpaConfig.java b/src/main/java/org/springframework/samples/petclinic/config/SpringDataJpaConfig.java new file mode 100644 index 000000000..c10b99bcf --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/SpringDataJpaConfig.java @@ -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 { + +} diff --git a/src/main/resources/spring/business-config.xml b/src/main/resources/spring/business-config.xml deleted file mode 100644 index 99cf4c1d3..000000000 --- a/src/main/resources/spring/business-config.xml +++ /dev/null @@ -1,96 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceJdbcTests.java b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceJdbcTests.java index 49e57ea40..1543596b0 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceJdbcTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceJdbcTests.java @@ -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 { diff --git a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceJpaTests.java b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceJpaTests.java index e024f21fd..2f5c9e3fc 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceJpaTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceJpaTests.java @@ -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 { diff --git a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceSpringDataJpaTests.java b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceSpringDataJpaTests.java index e01dda551..2cbd73c9e 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceSpringDataJpaTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceSpringDataJpaTests.java @@ -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 { diff --git a/src/test/java/org/springframework/samples/petclinic/web/VisitsViewTests-config.xml b/src/test/java/org/springframework/samples/petclinic/web/VisitsViewTests-config.xml deleted file mode 100644 index 8458ba2ec..000000000 --- a/src/test/java/org/springframework/samples/petclinic/web/VisitsViewTests-config.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - diff --git a/src/test/java/org/springframework/samples/petclinic/web/VisitsViewTests.java b/src/test/java/org/springframework/samples/petclinic/web/VisitsViewTests.java index ace89a019..2f1e17fa3 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/VisitsViewTests.java +++ b/src/test/java/org/springframework/samples/petclinic/web/VisitsViewTests.java @@ -28,8 +28,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; @@ -44,7 +46,9 @@ import org.springframework.web.context.WebApplicationContext; */ @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration -@ContextConfiguration("VisitsViewTests-config.xml") +@ContextHierarchy({ + @ContextConfiguration(classes = BusinessConfig.class), + @ContextConfiguration(locations = "classpath:spring/mvc-core-config.xml")}) @ActiveProfiles("jdbc") public class VisitsViewTests { From 40c3c6aa46ac717e4a9b3b9811cb219fca1324d7 Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Wed, 2 Jul 2014 17:57:17 +0200 Subject: [PATCH 3/8] Migrate the tools-config.xml XML context configuration file to ToolsConfig.java --- .../config/RootApplicationContextConfig.java | 4 +- .../samples/petclinic/config/ToolsConfig.java | 43 ++++++++++++++++ src/main/resources/spring/tools-config.xml | 49 ------------------- 3 files changed, 44 insertions(+), 52 deletions(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/config/ToolsConfig.java delete mode 100644 src/main/resources/spring/tools-config.xml diff --git a/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java b/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java index 254b3209c..da0add1b3 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java @@ -2,11 +2,9 @@ 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 -@Import(BusinessConfig.class) -@ImportResource("classpath:spring/tools-config.xml") +@Import({BusinessConfig.class, ToolsConfig.class}) public class RootApplicationContextConfig { } diff --git a/src/main/java/org/springframework/samples/petclinic/config/ToolsConfig.java b/src/main/java/org/springframework/samples/petclinic/config/ToolsConfig.java new file mode 100644 index 000000000..c0cccb195 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/ToolsConfig.java @@ -0,0 +1,43 @@ +package org.springframework.samples.petclinic.config; + +import net.sf.ehcache.CacheManager; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.ehcache.EhCacheCacheManager; +import org.springframework.cache.ehcache.EhCacheManagerFactoryBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Description; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.context.annotation.EnableMBeanExport; +import org.springframework.core.io.ClassPathResource; +import org.springframework.samples.petclinic.util.CallMonitoringAspect; + +@Configuration +@EnableCaching // enables scanning for @Cacheable annotation +@EnableMBeanExport +@EnableAspectJAutoProxy +public class ToolsConfig { + + @Bean + @Description("Call monitoring aspect that monitors call count and call invocation time") + public CallMonitoringAspect callMonitor() { + return new CallMonitoringAspect(); + } + + @Bean + @Autowired + public EhCacheCacheManager ehCacheCacheManager(CacheManager cacheManager) { + EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager(); + ehCacheCacheManager.setCacheManager(cacheManager); + return ehCacheCacheManager; + } + + @Bean + public EhCacheManagerFactoryBean cacheManager() { + EhCacheManagerFactoryBean ehCacheManager = new EhCacheManagerFactoryBean(); + ehCacheManager.setConfigLocation(new ClassPathResource("cache/ehcache.xml")); + return ehCacheManager; + } +} diff --git a/src/main/resources/spring/tools-config.xml b/src/main/resources/spring/tools-config.xml deleted file mode 100644 index 1be7e3b6c..000000000 --- a/src/main/resources/spring/tools-config.xml +++ /dev/null @@ -1,49 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From f92c31c31a1570bf8fc6d8f91ccafbde4cf3cd6e Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Fri, 4 Jul 2014 08:17:23 +0200 Subject: [PATCH 4/8] Display at startup Spring active profiles --- .../config/RootApplicationContextConfig.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java b/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java index da0add1b3..c7f6d5ec1 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java @@ -1,10 +1,41 @@ package org.springframework.samples.petclinic.config; +import javax.annotation.PostConstruct; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.core.env.Environment; @Configuration @Import({BusinessConfig.class, ToolsConfig.class}) public class RootApplicationContextConfig { + + private static final Logger LOG = LoggerFactory.getLogger(RootApplicationContextConfig.class); + + @Autowired + private Environment env; + + /** + * Application custom initialization code. + *

+ * Spring profiles can be configured with a system property + * -Dspring.profiles.active=javaee + *

+ */ + @PostConstruct + public void initApp() { + LOG.debug("Looking for Spring profiles..."); + if (env.getActiveProfiles().length == 0) { + LOG.info("No Spring profile configured, running with default configuration."); + } else { + for (String profile : env.getActiveProfiles()) { + LOG.info("Detected Spring profile: {}", profile); + } + } + } + } From 05955cbc1b83ed34d0e771f0c4f2668fdce4c61b Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Fri, 4 Jul 2014 08:38:24 +0200 Subject: [PATCH 5/8] Migrate XML datasource definition beans to Java configuration --- pom.xml | 1 - .../petclinic/config/BusinessConfig.java | 4 +- .../petclinic/config/DataSourceConfig.java | 46 +++++++++++++++++++ .../config/InitDataSourceConfig.java | 31 +++++++++++++ .../samples/petclinic/config/NotProfile.java | 29 ++++++++++++ .../petclinic/config/NotProfileCondition.java | 32 +++++++++++++ .../resources/spring/data-access.properties | 4 +- .../resources/spring/datasource-config.xml | 43 ----------------- 8 files changed, 141 insertions(+), 49 deletions(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/config/DataSourceConfig.java create mode 100644 src/main/java/org/springframework/samples/petclinic/config/InitDataSourceConfig.java create mode 100644 src/main/java/org/springframework/samples/petclinic/config/NotProfile.java create mode 100644 src/main/java/org/springframework/samples/petclinic/config/NotProfileCondition.java delete mode 100644 src/main/resources/spring/datasource-config.xml diff --git a/pom.xml b/pom.xml index cbc3501bf..4e6d08005 100644 --- a/pom.xml +++ b/pom.xml @@ -168,7 +168,6 @@ org.apache.tomcat tomcat-jdbc ${tomcat-jdbc.version} - runtime diff --git a/src/main/java/org/springframework/samples/petclinic/config/BusinessConfig.java b/src/main/java/org/springframework/samples/petclinic/config/BusinessConfig.java index fd258f7e6..190318edb 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/BusinessConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/config/BusinessConfig.java @@ -8,14 +8,12 @@ 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}) +@Import({DataSourceConfig.class, InitDataSourceConfig.class, JdbcConfig.class, SharedJpaConfig.class, JpaConfig.class, SpringDataJpaConfig.class}) public class BusinessConfig { diff --git a/src/main/java/org/springframework/samples/petclinic/config/DataSourceConfig.java b/src/main/java/org/springframework/samples/petclinic/config/DataSourceConfig.java new file mode 100644 index 000000000..5082bd8d6 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/DataSourceConfig.java @@ -0,0 +1,46 @@ +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.Configuration; +import org.springframework.context.annotation.Description; +import org.springframework.context.annotation.Profile; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.jndi.JndiObjectFactoryBean; + +@Configuration +@PropertySource("classpath:spring/data-access.properties") +public class DataSourceConfig { + + @Autowired + private Environment env; + + @Bean(name = "dataSource") + @Description("DataSource configuration for the tomcat jdbc connection pool") + @NotProfile("javaee") + public DataSource dataSource() { + // See here for more details on commons-dbcp versus tomcat-jdbc: + // http://blog.ippon.fr/2013/03/13/improving-the-performance-of-the-spring-petclinic-sample-application-part-3-of-5/--> + org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource(); + dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); + dataSource.setUrl(env.getProperty("jdbc.url")); + dataSource.setUsername(env.getProperty("jdbc.username")); + dataSource.setPassword(env.getProperty("jdbc.password")); + return dataSource; + } + + @Bean(name = "dataSource") + @Description("JNDI DataSource for JEE environments") + @Profile("javaee") + public JndiObjectFactoryBean jndiDataSource() + throws IllegalArgumentException { + JndiObjectFactoryBean dataSource = new JndiObjectFactoryBean(); + dataSource.setExpectedType(DataSource.class); + dataSource.setJndiName(env.getProperty("java:comp/env/jdbc/petclinic")); + return dataSource; + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/config/InitDataSourceConfig.java b/src/main/java/org/springframework/samples/petclinic/config/InitDataSourceConfig.java new file mode 100644 index 000000000..7ecae6c09 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/InitDataSourceConfig.java @@ -0,0 +1,31 @@ +package org.springframework.samples.petclinic.config; + +import javax.annotation.PostConstruct; +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.core.io.ClassPathResource; +import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; +import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; + +@Configuration +public class InitDataSourceConfig { + + @Autowired + private Environment env; + + @Autowired + private DataSource dataSource; + + @PostConstruct + public void init() { + ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); + databasePopulator.addScript(new ClassPathResource(env.getProperty("jdbc.initLocation"))); + databasePopulator.addScript(new ClassPathResource(env.getProperty("jdbc.dataLocation"))); + DatabasePopulatorUtils.execute(databasePopulator, dataSource); + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/config/NotProfile.java b/src/main/java/org/springframework/samples/petclinic/config/NotProfile.java new file mode 100644 index 000000000..111c1f1a6 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/NotProfile.java @@ -0,0 +1,29 @@ +package org.springframework.samples.petclinic.config; + + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.context.annotation.Conditional; + + +/** + * Indicates that a component is eligible for registration when none of the {@linkplain + * #value specified profiles} is active. +*/ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +@Documented +@Conditional(NotProfileCondition.class) +public @interface NotProfile { + + /** + * The set of profiles for which the annotated component should not be registered. + */ + String[] value(); + + +} diff --git a/src/main/java/org/springframework/samples/petclinic/config/NotProfileCondition.java b/src/main/java/org/springframework/samples/petclinic/config/NotProfileCondition.java new file mode 100644 index 000000000..097a47aca --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/NotProfileCondition.java @@ -0,0 +1,32 @@ +package org.springframework.samples.petclinic.config; + +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; +import org.springframework.util.MultiValueMap; + +/** + * {@link Condition} that matches based on the value of a {@link NotProfile @NotProfile} + * annotation. + * + */ +class NotProfileCondition implements Condition { + + @Override + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + if (context.getEnvironment() != null) { + MultiValueMap attrs = metadata.getAllAnnotationAttributes(NotProfile.class.getName()); + if (attrs != null) { + for (Object value : attrs.get("value")) { + if (context.getEnvironment().acceptsProfiles(((String[]) value))) { + return false; + } + } + return true; + } + } + return true; + } + +} + diff --git a/src/main/resources/spring/data-access.properties b/src/main/resources/spring/data-access.properties index c1cc3cefd..e22ce1df6 100644 --- a/src/main/resources/spring/data-access.properties +++ b/src/main/resources/spring/data-access.properties @@ -13,8 +13,8 @@ jdbc.username=sa jdbc.password= # Properties that control the population of schema and data for a new data source -jdbc.initLocation=classpath:db/hsqldb/initDB.sql -jdbc.dataLocation=classpath:db/hsqldb/populateDB.sql +jdbc.initLocation=db/hsqldb/initDB.sql +jdbc.dataLocation=db/hsqldb/populateDB.sql # Property that determines which database to use with an AbstractJpaVendorAdapter jpa.database=HSQL diff --git a/src/main/resources/spring/datasource-config.xml b/src/main/resources/spring/datasource-config.xml deleted file mode 100644 index f74129963..000000000 --- a/src/main/resources/spring/datasource-config.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From 8ffd295196713c8837d83c02ad15b1fda00971ec Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Sat, 5 Jul 2014 09:42:37 +0200 Subject: [PATCH 6/8] Add class header with Apache License and Copyright --- .../petclinic/config/BusinessConfig.java | 30 +++++++++++++++++++ .../petclinic/config/DataSourceConfig.java | 30 +++++++++++++++++++ .../config/InitDataSourceConfig.java | 30 +++++++++++++++++++ .../samples/petclinic/config/JdbcConfig.java | 30 +++++++++++++++++++ .../samples/petclinic/config/JpaConfig.java | 30 +++++++++++++++++++ .../samples/petclinic/config/NotProfile.java | 30 +++++++++++++++++++ .../petclinic/config/NotProfileCondition.java | 30 +++++++++++++++++++ .../config/RootApplicationContextConfig.java | 30 +++++++++++++++++++ .../petclinic/config/SharedJpaConfig.java | 30 +++++++++++++++++++ .../petclinic/config/SpringDataJpaConfig.java | 30 +++++++++++++++++++ .../samples/petclinic/config/ToolsConfig.java | 30 +++++++++++++++++++ 11 files changed, 330 insertions(+) diff --git a/src/main/java/org/springframework/samples/petclinic/config/BusinessConfig.java b/src/main/java/org/springframework/samples/petclinic/config/BusinessConfig.java index 190318edb..e41c16597 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/BusinessConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/config/BusinessConfig.java @@ -1,3 +1,33 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.samples.petclinic.config; import org.springframework.context.annotation.ComponentScan; diff --git a/src/main/java/org/springframework/samples/petclinic/config/DataSourceConfig.java b/src/main/java/org/springframework/samples/petclinic/config/DataSourceConfig.java index 5082bd8d6..d974bbb1c 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/DataSourceConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/config/DataSourceConfig.java @@ -1,3 +1,33 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.samples.petclinic.config; import javax.sql.DataSource; diff --git a/src/main/java/org/springframework/samples/petclinic/config/InitDataSourceConfig.java b/src/main/java/org/springframework/samples/petclinic/config/InitDataSourceConfig.java index 7ecae6c09..211a8c06d 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/InitDataSourceConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/config/InitDataSourceConfig.java @@ -1,3 +1,33 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.samples.petclinic.config; import javax.annotation.PostConstruct; diff --git a/src/main/java/org/springframework/samples/petclinic/config/JdbcConfig.java b/src/main/java/org/springframework/samples/petclinic/config/JdbcConfig.java index 34f5786f5..2b28cc973 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/JdbcConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/config/JdbcConfig.java @@ -1,3 +1,33 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.samples.petclinic.config; import javax.sql.DataSource; diff --git a/src/main/java/org/springframework/samples/petclinic/config/JpaConfig.java b/src/main/java/org/springframework/samples/petclinic/config/JpaConfig.java index a8c18e74a..f447ae5b2 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/JpaConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/config/JpaConfig.java @@ -1,3 +1,33 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.samples.petclinic.config; import org.springframework.context.annotation.ComponentScan; diff --git a/src/main/java/org/springframework/samples/petclinic/config/NotProfile.java b/src/main/java/org/springframework/samples/petclinic/config/NotProfile.java index 111c1f1a6..a63a40824 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/NotProfile.java +++ b/src/main/java/org/springframework/samples/petclinic/config/NotProfile.java @@ -1,3 +1,33 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.samples.petclinic.config; diff --git a/src/main/java/org/springframework/samples/petclinic/config/NotProfileCondition.java b/src/main/java/org/springframework/samples/petclinic/config/NotProfileCondition.java index 097a47aca..b45376112 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/NotProfileCondition.java +++ b/src/main/java/org/springframework/samples/petclinic/config/NotProfileCondition.java @@ -1,3 +1,33 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.samples.petclinic.config; import org.springframework.context.annotation.Condition; diff --git a/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java b/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java index c7f6d5ec1..a0f598be7 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/config/RootApplicationContextConfig.java @@ -1,3 +1,33 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.samples.petclinic.config; import javax.annotation.PostConstruct; diff --git a/src/main/java/org/springframework/samples/petclinic/config/SharedJpaConfig.java b/src/main/java/org/springframework/samples/petclinic/config/SharedJpaConfig.java index 0d454c977..893a222be 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/SharedJpaConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/config/SharedJpaConfig.java @@ -1,3 +1,33 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.samples.petclinic.config; import javax.persistence.EntityManagerFactory; diff --git a/src/main/java/org/springframework/samples/petclinic/config/SpringDataJpaConfig.java b/src/main/java/org/springframework/samples/petclinic/config/SpringDataJpaConfig.java index c10b99bcf..dedd5f179 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/SpringDataJpaConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/config/SpringDataJpaConfig.java @@ -1,3 +1,33 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.samples.petclinic.config; import org.springframework.context.annotation.Configuration; diff --git a/src/main/java/org/springframework/samples/petclinic/config/ToolsConfig.java b/src/main/java/org/springframework/samples/petclinic/config/ToolsConfig.java index c0cccb195..e69da090e 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/ToolsConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/config/ToolsConfig.java @@ -1,3 +1,33 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.samples.petclinic.config; import net.sf.ehcache.CacheManager; From b0a2dc69b1157c96a11fa1faf74b5c4d29d24834 Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Sat, 5 Jul 2014 10:20:34 +0200 Subject: [PATCH 7/8] Migrate to the Spring MVC Java Configuration --- .../petclinic/config/MvcCoreConfig.java | 146 +++++++++++++++++- .../petclinic/config/MvcViewConfig.java | 103 ++++++++++++ src/main/resources/spring/mvc-core-config.xml | 66 -------- src/main/resources/spring/mvc-view-config.xml | 64 -------- .../petclinic/web/VisitsViewTests.java | 3 +- 5 files changed, 247 insertions(+), 135 deletions(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/config/MvcViewConfig.java delete mode 100644 src/main/resources/spring/mvc-core-config.xml delete mode 100644 src/main/resources/spring/mvc-view-config.xml diff --git a/src/main/java/org/springframework/samples/petclinic/config/MvcCoreConfig.java b/src/main/java/org/springframework/samples/petclinic/config/MvcCoreConfig.java index 69ddf94e7..e96af0043 100644 --- a/src/main/java/org/springframework/samples/petclinic/config/MvcCoreConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/config/MvcCoreConfig.java @@ -1,10 +1,148 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.samples.petclinic.config; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.ImportResource; +import java.util.List; +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.Description; +import org.springframework.context.annotation.Import; +import org.springframework.context.support.ReloadableResourceBundleMessageSource; +import org.springframework.format.FormatterRegistry; +import org.springframework.http.MediaType; +import org.springframework.samples.petclinic.service.ClinicService; +import org.springframework.samples.petclinic.web.PetTypeFormatter; +import org.springframework.web.servlet.HandlerExceptionResolver; +import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; +import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; + +/** + *

+ * The ContentNegotiatingViewResolver delegates to the + * InternalResourceViewResolver and BeanNameViewResolver, and uses the requested + * media type (determined by the path extension) to pick a matching view. When + * the media type is 'text/html', it will delegate to the + * InternalResourceViewResolver's JstlView, otherwise to the + * BeanNameViewResolver. + * + */ @Configuration -@ImportResource("classpath:spring/mvc-core-config.xml") -public class MvcCoreConfig { +@EnableWebMvc +@Import(MvcViewConfig.class) +// POJOs labeled with the @Controller and @Service annotations are +// auto-detected. +@ComponentScan(basePackages = { "org.springframework.samples.petclinic.web" }) +public class MvcCoreConfig extends WebMvcConfigurerAdapter { + + @Autowired + private ClinicService clinicService; + + @Override + public void configureContentNegotiation( + ContentNegotiationConfigurer configurer) { + configurer.ignoreAcceptHeader(true); + configurer.defaultContentType(MediaType.TEXT_HTML); + configurer.mediaType("html", MediaType.TEXT_HTML); + configurer.mediaType("xml", MediaType.APPLICATION_XML); + configurer.mediaType("atom", MediaType.APPLICATION_ATOM_XML); + } + + @Override + public void configureDefaultServletHandling( + DefaultServletHandlerConfigurer configurer) { + // Serve static resources (*.html, ...) from src/main/webapp/ + configurer.enable(); + } + + @Override + public void addFormatters(FormatterRegistry formatterRegistry) { + formatterRegistry.addFormatter(petTypeFormatter()); + } + + @Bean + public PetTypeFormatter petTypeFormatter() { + return new PetTypeFormatter(clinicService); + } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + // all resources inside folder src/main/webapp/resources are mapped so + // they can be refered to inside JSP files (see header.jsp for more + // details) + registry.addResourceHandler("/resources/**").addResourceLocations( + "/resources/"); + // uses WebJars so Javascript and CSS libs can be declared as Maven dependencies (Bootstrap, jQuery...) + registry.addResourceHandler("/webjars/**").addResourceLocations( + "classpath:/META-INF/resources/webjars/"); + } + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/").setViewName("welcome"); + } + + @Bean(name = "messageSource") + @Description("Message source for this context, loaded from localized 'messages_xx' files.") + public ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource() { + // Files are stored inside src/main/resources + ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); + messageSource.setBasenames("classpath:messages/messages"); + return messageSource; + } + + /** + * Resolves specific types of exceptions to corresponding logical view names + * for error views. + * + *

+ * View name resolved using bean of type InternalResourceViewResolver + * (declared in {@link MvcViewConfig}). + */ + @Override + public void configureHandlerExceptionResolvers( + List exceptionResolvers) { + SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver(); + // results into 'WEB-INF/jsp/exception.jsp' + exceptionResolver.setDefaultErrorView("exception"); + // needed otherwise exceptions won't be logged anywhere + exceptionResolver.setWarnLogCategory("warn"); + exceptionResolvers.add(exceptionResolver); + } } diff --git a/src/main/java/org/springframework/samples/petclinic/config/MvcViewConfig.java b/src/main/java/org/springframework/samples/petclinic/config/MvcViewConfig.java new file mode 100644 index 000000000..3b16448c5 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/MvcViewConfig.java @@ -0,0 +1,103 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.samples.petclinic.config; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Description; +import org.springframework.oxm.Marshaller; +import org.springframework.oxm.jaxb.Jaxb2Marshaller; +import org.springframework.samples.petclinic.model.Vets; +import org.springframework.samples.petclinic.web.VetsAtomView; +import org.springframework.web.accept.ContentNegotiationManager; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.view.BeanNameViewResolver; +import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; +import org.springframework.web.servlet.view.xml.MarshallingView; + +@Configuration +public class MvcViewConfig { + + @Bean + public ContentNegotiatingViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) { + ContentNegotiatingViewResolver contentNegotiatingViewResolver = new ContentNegotiatingViewResolver(); + List viewResolvers = new ArrayList(); + viewResolvers.add(internalResourceViewResolver()); + viewResolvers.add(beanNameViewResolver()); + contentNegotiatingViewResolver.setViewResolvers(viewResolvers ); + contentNegotiatingViewResolver.setContentNegotiationManager(manager); + return contentNegotiatingViewResolver; + } + + @Bean + @Description("Default viewClass: JSTL view (JSP with html output)") + public ViewResolver internalResourceViewResolver() { + // Example: a logical view name of 'vets' is mapped to + // '/WEB-INF/jsp/vets.jsp' + InternalResourceViewResolver bean = new InternalResourceViewResolver(); + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/jsp/"); + bean.setSuffix(".jsp"); + return bean; + } + + @Bean + @Description("Used for 'xml' and 'atom' views") + public ViewResolver beanNameViewResolver() { + return new BeanNameViewResolver(); + } + + @Bean(name = "vets/vetList.atom") + @Description("Renders an Atom feed of the visits. Used by the BeanNameViewResolver") + public VetsAtomView vetsAtomView() { + return new VetsAtomView(); + } + + @Bean(name = "vets/vetList.xml") + @Description("Renders an XML view. Used by the BeanNameViewResolver") + public MarshallingView marshallingView() { + return new MarshallingView(marshaller()); + } + + @Bean + @Description("Object-XML mapping declared using annotations inside 'Vets'") + public Marshaller marshaller() { + Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); + marshaller.setClassesToBeBound(Vets.class); + return marshaller; + } + +} diff --git a/src/main/resources/spring/mvc-core-config.xml b/src/main/resources/spring/mvc-core-config.xml deleted file mode 100644 index 170fbdba1..000000000 --- a/src/main/resources/spring/mvc-core-config.xml +++ /dev/null @@ -1,66 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/resources/spring/mvc-view-config.xml b/src/main/resources/spring/mvc-view-config.xml deleted file mode 100644 index 4413f0195..000000000 --- a/src/main/resources/spring/mvc-view-config.xml +++ /dev/null @@ -1,64 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/test/java/org/springframework/samples/petclinic/web/VisitsViewTests.java b/src/test/java/org/springframework/samples/petclinic/web/VisitsViewTests.java index 2f1e17fa3..8372f60af 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/VisitsViewTests.java +++ b/src/test/java/org/springframework/samples/petclinic/web/VisitsViewTests.java @@ -29,6 +29,7 @@ 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.samples.petclinic.config.MvcCoreConfig; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextHierarchy; @@ -48,7 +49,7 @@ import org.springframework.web.context.WebApplicationContext; @WebAppConfiguration @ContextHierarchy({ @ContextConfiguration(classes = BusinessConfig.class), - @ContextConfiguration(locations = "classpath:spring/mvc-core-config.xml")}) + @ContextConfiguration(classes = MvcCoreConfig.class)}) @ActiveProfiles("jdbc") public class VisitsViewTests { From 8230b2a6fe515dc2727b0acb4025e80d98aef44e Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Sat, 5 Jul 2014 10:35:14 +0200 Subject: [PATCH 8/8] Update readme.md with Java Configuration classes --- readme.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/readme.md b/readme.md index 8f9cb72d1..fe8ee53f7 100644 --- a/readme.md +++ b/readme.md @@ -52,16 +52,16 @@ File -> Import -> Maven -> Existing Maven project Spring MVC- Atom integration VetsAtomView.java - mvc-view-config.xml + MvcViewConfig.java Spring MVC - XML integration - mvc-view-config.xml + MvcViewConfig.java Spring MVC - ContentNegotiatingViewResolver - mvc-view-config.xml + MvcViewConfig.java Spring MVC Test Framework @@ -77,7 +77,7 @@ File -> Import -> Maven -> Existing Maven project webjars webjars declaration inside pom.xml
- Resource mapping in Spring configuration
+ Resource mapping in Spring configuration
sample usage in JSP @@ -109,21 +109,21 @@ File -> Import -> Maven -> Existing Maven project Transactions - business-config.xml + BusinessConfig.java ClinicServiceImpl.java Cache - tools-config.xml + ToolsConfig.java ClinicServiceImpl.java Bean Profiles - business-config.xml + JdbcConfig.java ClinicServiceJdbcTests.java web.xml @@ -131,19 +131,21 @@ File -> Import -> Maven -> Existing Maven project JdbcTemplate - business-config.xml + JdbcConfig.java jdbc folder JPA - business-config.xml + SharedJpaConfig.java + JpaConfig.java jpa folder Spring Data JPA - business-config.xml + SharedJpaConfig.java + SpringDataJpaConfig.java springdatajpa folder